Spaces:
Sleeping
Sleeping
Upload utils.py
Browse files
utils.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import List
|
2 |
+
|
3 |
+
|
4 |
+
def format_as_chat(message: str, history: List[List[str]]) -> str:
|
5 |
+
"""
|
6 |
+
Given a message and a history of previous messages, returns a string that formats the conversation as a chat.
|
7 |
+
Uses the format expected by Meta Llama 3 Instruct.
|
8 |
+
|
9 |
+
:param message: A string containing the user's most recent message
|
10 |
+
:param history: A list of lists of previous messages, where each sublist is a conversation turn:
|
11 |
+
[[user_message1, assistant_reply1], [user_message2, assistant_reply2], ...]
|
12 |
+
"""
|
13 |
+
preprocessed_history_lis = ['<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n'.join(sub_lis) +
|
14 |
+
'<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n' for sub_lis in history]
|
15 |
+
|
16 |
+
output_message = '<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n' + ''.join(preprocessed_history_lis) + message + '<|eot_id|>'
|
17 |
+
|
18 |
+
return output_message
|
19 |
+
|