File size: 931 Bytes
beed95a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from typing import List


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.
    Uses the format expected by Meta Llama 3 Instruct.

    :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], ...]
    """
    preprocessed_history_lis = ['<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n'.join(sub_lis) +
                          '<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n' for sub_lis in history]
    
    output_message = '<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n' + ''.join(preprocessed_history_lis) + message + '<|eot_id|>'

    return output_message