利用jinja2,对提示词进行格式输出。以下是qwen2中tokenizer_config.json文件中的chat_template模块定义的提示词转换方式。
(1)查看qwen2的chat_template
{"add_prefix_space": false,"added_tokens_decoder": {"151643": {"content": "<|endoftext|>","lstrip": false,"normalized": false,"rstrip": false,"single_word": false,"special": true},"151644": {"content": "<|im_start|>","lstrip": false,"normalized": false,"rstrip": false,"single_word": false,"special": true},"151645": {"content": "<|im_end|>","lstrip": false,"normalized": false,"rstrip": false,"single_word": false,"special": true}},"additional_special_tokens": ["<|im_start|>", "<|im_end|>"],"bos_token": null,"chat_template": "{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '<|im_start|>system\nYou are a helpful assistant.<|im_end|
>\n' }}{% endif %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>ass
istant\n' }}{% endif %}","clean_up_tokenization_spaces": false,"eos_token": "<|im_end|>","errors": "replace","model_max_length": 32768,"pad_token": "<|endoftext|>","split_special_tokens": false,"tokenizer_class": "Qwen2Tokenizer","unk_token": null
}
(2)提示词格式话
from jinja2 import Template# 定义模板字符串
template_str = """
{% for message in messages %}
{% if loop.first and messages[0]['role'] != 'system' %}
{{ '<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n' }}
{% endif %}
{{ '<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n' }}
{% endfor %}
{% if add_generation_prompt %}
{{ '<|im_start|>assistant\n' }}
{% endif %}
"""# 创建模板对象
template = Template(template_str)# 定义数据
messages = [{'role': 'user', 'content': '你好,我需要帮助。'},{'role': 'assistant', 'content': '你好!我能帮你什么吗?'},{'role': 'user', 'content': '我想了解一下天气。'},{'role': 'assistant', 'content': '今天的天气是晴朗的。'},
]
add_generation_prompt = True #一个控制标志,用于在需要时向用户或系统提供额外的提示信息,特别是在生成文本或对话的上下文中。它帮助区分不同的发言者或指示接下来的操作步骤# 渲染模板
output = template.render(messages=messages, add_generation_prompt=add_generation_prompt)# 打印输出结果
print(output)