Skip to content

Commit 00cc010

Browse files
author
xusenlin
committed
Update qwen2 template
1 parent 5592b80 commit 00cc010

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

api/adapter/template.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ def parse_assistant_response(
219219
class Qwen2Template(BaseTemplate):
220220

221221
name = "qwen2"
222-
system_prompt = ""
223222
allow_models = ["qwen2"]
224223
stop = {
225224
"strings": ["<|endoftext|>", "<|im_end|>"],
@@ -231,13 +230,17 @@ def template(self) -> str:
231230
https://github.com/openai/openai-python/blob/main/chatml.md
232231
"""
233232
return (
234-
"{{ system_prompt }}"
235233
"{% for message in messages %}"
236-
"{{ '<|im_start|>' + message['role'] + '\\n' + message['content'] + '<|im_end|>' + '\\n' }}"
237-
"{% endfor %}"
238-
"{% if add_generation_prompt %}"
239-
"{{ '<|im_start|>assistant\\n' }}"
234+
"{% if loop.first and messages[0]['role'] != 'system' %}"
235+
"{{ '<|im_start|>system\nYou are a helpful assistant<|im_end|>\n' }}"
236+
"{% endif %}"
237+
"{{'<|im_start|>' + message['role'] + '\n' + message['content']}}"
238+
"{% if (loop.last and add_generation_prompt) or not loop.last %}"
239+
"{{ '<|im_end|>' + '\n'}}"
240240
"{% endif %}"
241+
"{% endfor %}"
242+
"{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}"
243+
"{{ '<|im_start|>assistant\n' }}{% endif %}"
241244
)
242245

243246

@@ -1402,6 +1405,6 @@ def template(self) -> str:
14021405
{"role": "assistant", "content": "I'm doing great. How can I help you today?"},
14031406
{"role": "user", "content": "I'd like to show off how chat templating works!"},
14041407
]
1405-
template = get_prompt_adapter(prompt_name="internlm2")
1408+
template = get_prompt_adapter(prompt_name="qwen2")
14061409
messages = template.postprocess_messages(chat)
14071410
print(template.apply_chat_template(messages))

0 commit comments

Comments
 (0)