当前位置: 首页 > article >正文

提示词格式化

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

http://www.kler.cn/news/343481.html

相关文章:

  • 卡码网C++基础课 |20. 排队取奶茶
  • xmltodict 处理 XML 数据案例解析
  • 无人机在矿业领域的应用!
  • 探秘纯前端Excel表格:构建现金流量表的完整指南
  • 【大数据】数据采集工具sqoop介绍
  • 春意融融:Spring Boot技术在“衣依”服装销售平台的应用
  • swagger2.9.2 和 springboot3.3.4版本冲突问腿
  • 线控底盘技术介绍
  • Selenium WebDriver和Chrome对照表
  • 用AI构建小程序需要多久?效果如何?
  • Redis:set类型
  • 命令 首选项:打开用户设置(json) 导致错误 文件似乎是二进制文件,不能作为文本打开
  • Qt Qml Map-地图绘制点与圆的切线
  • 火山引擎边缘智能×扣子,拓展AI Agent物理边界
  • javascript 自定义多选框实现 ag-grid中没有原生多选框
  • 物理学基础精解【67】
  • 通知系统的设计方案
  • 项目 多人对话
  • 动态规划-路径问题——174.地下城游戏
  • bclinux安装minio和mc及从服务器上下载文件