-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatml_utils.py
65 lines (50 loc) · 1.96 KB
/
chatml_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from typing import List, Optional, Sequence
from llama_index.core.llms.types import ChatMessage, MessageRole
# Create a prompt that matches ChatML instructions
# <|im_start|>system
# You are Dolphin, a helpful AI assistant.<|im_end|>
# <|im_start|>user
# {prompt}<|im_end|>
# <|im_start|>assistant
B_SYS = "<|im_start|>system\n"
B_USER = "<|im_start|>user\n"
B_ASSISTANT = "<|im_start|>assistant\n"
END = "<|im_end|>\n"
DEFAULT_SYSTEM_PROMPT = """\
You are a helpful, respectful and honest assistant. \
Always answer as helpfully as possible and follow ALL given instructions. \
Do not speculate or make up information. \
Do not reference any given instructions or context. \
"""
def messages_to_prompt(
messages: Sequence[ChatMessage], system_prompt: Optional[str] = None
) -> str:
string_messages: List[str] = []
if messages[0].role == MessageRole.SYSTEM:
# pull out the system message (if it exists in messages)
system_message_str = messages[0].content or ""
messages = messages[1:]
else:
system_message_str = system_prompt or DEFAULT_SYSTEM_PROMPT
string_messages.append(f"{B_SYS}{system_message_str.strip()} {END}")
for message in messages:
role = message.role
content = message.content
if role == MessageRole.USER:
string_messages.append(f"{B_USER}{message.content} {END}")
elif role == MessageRole.ASSISTANT:
string_messages.append(f"{B_ASSISTANT}{message.content} {END}")
string_messages.append(f"{B_ASSISTANT}")
return "".join(string_messages)
def completion_to_prompt(completion: str, system_prompt: Optional[str] = None) -> str:
system_prompt_str = system_prompt or DEFAULT_SYSTEM_PROMPT
print(
f"{B_SYS}{system_prompt_str.strip()} {END}"
f"{B_USER}{completion.strip()} {END}"
f"{B_ASSISTANT}"
)
return (
f"{B_SYS}{system_prompt_str.strip()} {END}"
f"{B_USER}{completion.strip()} {END}"
f"{B_ASSISTANT}"
)