Newer
Older
def format_as_chat(message: str, history: List[List[str]]) -> str:
Given a message and a history of previous messages, returns a string that formats the conversation as a chat.
:param message: A string containing the user's most recent message
:param history: A list of lists of previous messages, where each sublist is a conversation turn:
[[user_message1, assistant_reply1], [user_message2, assistant_reply2], ...]
formatted_prompt = "<|begin_of_text|>"
if len(history) == 0:
return formatted_prompt + f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|>"
for conv_turn in history:
prompt = f"<|start_header_id|>user<|end_header_id|>\n\n{
conv_turn[0]}<|eot_id|>"
reply = f"<|start_header_id|>assistant<|end_header_id|>\n\n{
conv_turn[1]}<|eot_id|>"
# Add conversation turn to full prompt
formatted_prompt += prompt + reply
# Add last message
formatted_prompt += \
f"<|start_header_id|>user<|end_header_id|>\n\n{message}<|eot_id|>"
return formatted_prompt