MCPcopy Create free account
hub / github.com/SalesforceAIResearch/perfcodegen / format_chats

Method format_chats

src/model.py:171–225  ·  view source on GitHub ↗
(self, dialogs, pad = False)

Source from the content-addressed store, hash-verified

169
170
171 def format_chats(self, dialogs, pad = False):
172 B_INST, E_INST = "[INST]", "[/INST]"
173 B_SYS, E_SYS = "SYS\n", "\n<</SYS>>\n\n"
174
175
176 prompt_tokens = []
177
178
179 for dialog in dialogs:
180 if dialog[0]["role"] == "system":
181 dialog = [
182 {
183 "role": dialog[1]["role"],
184 "content": B_SYS
185 + dialog[0]["content"]
186 + E_SYS
187 + dialog[1]["content"],
188 }
189 ] + dialog[2:]
190
191 assert all([msg["role"] == "user" for msg in dialog[::2]]) and all(
192 [msg["role"] == "assistant" for msg in dialog[1::2]]
193 ), (
194 "model only supports 'system', 'user' and 'assistant' roles, "
195 "starting with 'system', then 'user' and alternating (u/a/u/a/u...)"
196 )
197
198 dialog_tokens = sum(
199 [
200 [
201 [self.tokenizer.bos_token_id]
202 + self.tokenizer(
203 f"{B_INST} {(prompt['content']).strip()} {E_INST} {(answer['content']).strip()} "
204 ).input_ids
205 + [self.tokenizer.eos_token_id]
206 ]
207 for prompt, answer in zip(
208 dialog[::2],
209 dialog[1::2],
210 )
211 ],
212 [],
213 )
214 assert (
215 dialog[-1]["role"] == "user"
216 ), f"Last message must be from user, got {dialog[-1]['role']}"
217 dialog_tokens += [
218 [self.tokenizer.bos_token_id]
219 + self.tokenizer(
220 f"{B_INST} {(dialog[-1]['content']).strip()} {E_INST}"
221 ).input_ids
222 ]
223 prompt_tokens.append(dialog_tokens)
224
225 return prompt_tokens
226
227 def infer_one(self, prompt, n = 10):
228 try:

Callers 2

infer_one_chatMethod · 0.95
infer_many_chatsMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected