欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 资讯 > 提示词格式化

提示词格式化

2024/10/23 23:23:27 来源:https://blog.csdn.net/zhouwenyuan1015/article/details/142759657  浏览:    关键词:提示词格式化

利用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)

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com