forked from AkariAsai/OpenScholar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_chat_formats.py
246 lines (203 loc) · 7.52 KB
/
_chat_formats.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from abc import ABC, abstractmethod
from typing import Dict, List, Tuple
from torchtune.data._messages import Message, Role
class ChatFormat(ABC):
"""
Interface for chat formats. Each chat format should include tags for system,
user, and assistant roles that are prepended or appended to the message
content.
"""
# Template should map role to a tuple containing the tag to prepend to the text
# and tag to append to the text. Leave as empty strings to not prepend or append
template: Dict[Role, Tuple[str, str]]
@classmethod
@abstractmethod
def format(
cls,
sample: List[Message],
) -> List[Message]:
"""
Format each role's message(s) according to the chat format
Args:
sample (List[Message]): a single conversation, structured as a list
of `Message` objects
Returns:
The formatted list of messages
"""
pass
class Llama3ChatFormat(ChatFormat):
@classmethod
def format(cls, sample: List[Message]) -> List[Message]:
# bp()
return sample
class Llama2ChatFormat(ChatFormat):
"""
Chat format that formats human and system prompts with appropriate tags
used in Llama2 pre-training. Taken from Meta's official `Llama inference
repository <https://github.com/meta-llama/llama/blob/main/llama/generation.py>`_.
.. code-block:: text
"[INST] <<SYS>>
You are a helpful, respectful and honest assistant.
<</SYS>>"
I am going to Paris, what should I see? [/INST] Paris, the capital of France, is known for its stunning architecture..."
"""
template = {
"system": ("<<SYS>>\n", "\n<</SYS>>\n\n"),
"user": ("[INST] ", " [/INST] "),
"assistant": ("", ""),
"ipython": ("", ""),
}
@classmethod
def format(
cls,
sample: List[Message],
) -> List[Message]:
"""
Format user and system messages with appropriate tags.
Args:
sample (List[Message]): a single conversation, structured as a list
of `Message` objects
Returns:
The formatted list of messages
"""
system_message = []
formatted_dialogue = []
for message in sample:
if message.role == "system":
system_message = (
[{"type": "text", "content": cls.template["system"][0]}]
+ message.content
+ [{"type": "text", "content": cls.template["system"][1]}]
)
# Incorporate the system message in the user message - Llama2 only
# looks for the <<SYS>> tags and not the explicit role so this will
# be treated the same as an actual system message. We do this because
# of the nesting of the system prompt in the user message.
continue
elif message.role == "user":
content = (
[{"type": "text", "content": cls.template["user"][0]}]
+ system_message
+ message.content
+ [{"type": "text", "content": cls.template["user"][1]}]
)
elif message.role == "assistant":
# No special formatting needed for assistant message
content = message.content
formatted_dialogue.append(
Message(
role=message.role,
content=content,
masked=message.masked,
ipython=message.ipython,
eot=message.eot,
),
)
return formatted_dialogue
class MistralChatFormat(ChatFormat):
"""
Formats according to `Mistral's instruct model <https://docs.mistral.ai/models/>`_.
It is identical to :class:`Llama2ChatFormat`, except it does not support system
prompts.
.. code-block:: text
"[INST] I am going to Paris, what should I see? [/INST] Paris, the capital
of France, is known for its stunning architecture..."
"""
template = {
"system": None,
"user": ("[INST] ", " [/INST] "),
"assistant": ("", ""),
"ipython": ("", ""),
}
@classmethod
def format(
cls,
sample: List[Message],
) -> List[Message]:
"""
Format user and system messages with appropriate tags.
Args:
sample (List[Message]): a single conversation, structured as a list
of `Message` objects
Returns:
The formatted list of messages
Raises:
ValueError: If system prompts are provided
"""
formatted_dialogue = []
for message in sample:
if message.role == "system":
raise ValueError(
"System prompts are not supported in MistralChatFormat"
)
else:
content = (
[{"type": "text", "content": cls.template[message.role][0]}]
+ message.content
+ [{"type": "text", "content": cls.template[message.role][1]}]
)
formatted_dialogue.append(
Message(
role=message.role,
content=content,
masked=message.masked,
ipython=message.ipython,
eot=message.eot,
),
)
return formatted_dialogue
class ChatMLFormat(ChatFormat):
"""
OpenAI's `Chat Markup Language
<https://github.com/MicrosoftDocs/azure-docs/blob/772c14eeabfa0c0c561d5c2d34ef19341f528b7b/articles/ai-services/openai/how-to/chat-markup-language.md>`_
used by their chat models.
It is the default chat format used by Hugging Face models.
.. code-block:: text
<|im_start|>system
Provide some context and/or instructions to the model.<|im_end|>
<|im_start|>user
The user’s message goes here<|im_end|>
<|im_start|>assistant
The assistant’s response goes here<|im_end|>
"""
template = {
"system": ("<|im_start|>system\n", "<|im_end|>\n"),
"user": ("<|im_start|>user\n", "<|im_end|>\n"),
"assistant": ("<|im_start|>assistant\n", "<|im_end|>"),
"ipython": ("", ""),
}
@classmethod
def format(
cls,
sample: List[Message],
) -> List[Message]:
"""
Format user and system messages with appropriate tags.
Args:
sample (List[Message]): a single conversation, structured as a list
of `Message` objects
Returns:
The formatted list of messages
"""
formatted_dialogue = []
for message in sample:
content = (
[{"type": "text", "content": cls.template[message.role][0]}]
+ message.content
+ [{"type": "text", "content": cls.template[message.role][1]}]
)
formatted_dialogue.append(
Message(
role=message.role,
content=content,
masked=message.masked,
ipython=message.ipython,
eot=message.eot,
),
)
return formatted_dialogue