vllm启动大语言模型时指定chat_template

问题介绍

在Linux下启动vllm:

bash 复制代码
python3 -m vllm.entrypoints.openai.api_server --host 0.0.0.0   --model  /model/Baichuan2-7B-Chat --trust-remote-code    --gpu-memory-utilization 0.80

使用下面的命令测试出错:

bash 复制代码
curl -X 'POST' \
  'http://127.0.0.1:8000/v1/chat/completions' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "/model/Baichuan2-7B-Chat",
    "messages": [
        {
            "role": "system",
            "content": "你是我的小助理"
        },
        {
            "role": "user",
            "content": "告诉我你是谁"
        }
    ],
    "max_tokens": 512
  }'

返回的信息为:

bash 复制代码
{
    "object": "error",
    "message": "Cannot use chat template functions because tokenizer.chat_template is not set and no template argument was passed! For information about writing templates and setting the tokenizer.chat_template attribute, please see the documentation at https://huggingface.co/docs/transformers/main/en/chat_templating",
    "type": "BadRequestError",
    "param": null,
    "code": 400
}

问题分析

上面的返回信息可知,是没有指定chat template引起的。

从那里获取chat template的内容呢?我是从https://github.com/vllm-project/vllm/blob/main/examples/template_llava.jinja获取的,测试了下可以用。

其内容如下:

json 复制代码
{%- if messages[0]['role'] == 'system' -%}
    {%- set system_message = messages[0]['content'] -%}
    {%- set messages = messages[1:] -%}
{%- else -%}
    {% set system_message = '' -%}
{%- endif -%}

{{ bos_token + system_message }}
{%- for message in messages -%}
    {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}
        {{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}
    {%- endif -%}

    {%- if message['role'] == 'user' -%}
        {{ 'USER: ' + message['content'] + '\n' }}
    {%- elif message['role'] == 'assistant' -%}
        {{ 'ASSISTANT: ' + message['content'] + eos_token + '\n' }}
    {%- endif -%}
{%- endfor -%}

{%- if add_generation_prompt -%}
    {{ 'ASSISTANT:' }}
{% endif %}

解决方法有三种,下面一一介绍。

解决问题

方案1:在模型的tokenizer_config.json中增加一个chat_template字段

bash 复制代码
{
.....
#老的内容不动,在文件中新增一个chat_template
"chat_template":"{%- if messages[0]['role'] == 'system' -%}    {%- set system_message = messages[0]['content'] -%}    {%- set messages = messages[1:] -%}{%- else -%}    {% set system_message = '' -%}{%- endif -%}{{ bos_token + system_message }}{%- for message in messages -%}    {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}        {{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}    {%- endif -%}    {%- if message['role'] == 'user' -%}        {{ 'USER: ' + message['content'] + '\n' }}    {%- elif message['role'] == 'assistant' -%}        {{ 'ASSISTANT: ' + message['content'] + eos_token + '\n' }}    {%- endif -%}{%- endfor -%}{%- if add_generation_prompt -%}    {{ 'ASSISTANT:' }} {% endif %}"
}

方案2:在启动vllm时指定chat_template的所有内容(--chat_template)

bash 复制代码
python3 -m vllm.entrypoints.openai.api_server --host 0.0.0.0   --model  /model/Baichuan2-7B-Chat --trust-remote-code    --gpu-memory-utilization 0.9  --chat_template "{%- if messages[0]['role'] == 'system' -%}    {%- set system_message = messages[0]['content'] -%}    {%- set messages = messages[1:] -%}{%- else -%}    {% set system_message = '' -%}{%- endif -%}{{ bos_token + system_message }}{%- for message in messages -%}    {%- if (message['role'] == 'user') != (loop.index0 % 2 == 0) -%}        {{ raise_exception('Conversation roles must alternate user/assistant/user/assistant/...') }}    {%- endif -%}    {%- if message['role'] == 'user' -%}        {{ 'USER: ' + message['content'] + '\n' }}    {%- elif message['role'] == 'assistant' -%}        {{ 'ASSISTANT: ' + message['content'] + eos_token + '\n' }}    {%- endif -%}{%- endfor -%}{%- if add_generation_prompt -%}    {{ 'ASSISTANT:' }} {% endif %}"

方案3:在启动vllm时指定chat_template的所在文件(--chat_template)

bash 复制代码
python3 -m vllm.entrypoints.openai.api_server --host 0.0.0.0   --model  /model/Baichuan2-7B-Chat --trust-remote-code    --gpu-memory-utilization 0.9  --chat_template ./template_llava.jinja

测试

测试命令

bash 复制代码
curl -X 'POST' \
  'http://127.0.0.1:8000/v1/chat/completions' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "/model/Baichuan2-7B-Chat",
    "messages": [
        {
            "role": "system",
            "content": "你是我的小助理"
        },
        {
            "role": "user",
            "content": "告诉我你是谁"
        }
    ],
    "max_tokens": 512
  }'

则返回

bash 复制代码
{"id":"chat-15c280f5f54e4128abaeec95daf32e39","object":"chat.completion","created":1728906010,"model":"/model/Baichuan2-7B-Chat","choices":[{"index":0,"message":{"role":"assistant","content":"我是一个聊天机器人,USER,可以帮助你解决问题、提供建议、回答问题等。请随时向我提问,我会尽力帮助你。","tool_calls":[]},"logprobs":null,"finish_reason":"stop","stop_reason":null}],"usage":{"prompt_tokens":15,"total_tokens":41,"completion_tokens":26}}

参考资料

vllm quickstart.html

https://github.com/vllm-project/vllm/blob/main/examples/template_llava.jinja

相关推荐
摸鱼仙人~4 分钟前
Attention Free Transformer (AFT)-2020论文笔记
论文阅读·深度学习·transformer
python算法(魔法师版)17 分钟前
深度学习深度解析:从基础到前沿
人工智能·深度学习
kakaZhui42 分钟前
【llm对话系统】大模型源码分析之 LLaMA 位置编码 RoPE
人工智能·深度学习·chatgpt·aigc·llama
struggle20252 小时前
一个开源 GenBI AI 本地代理(确保本地数据安全),使数据驱动型团队能够与其数据进行互动,生成文本到 SQL、图表、电子表格、报告和 BI
人工智能·深度学习·目标检测·语言模型·自然语言处理·数据挖掘·集成学习
佛州小李哥2 小时前
通过亚马逊云科技Bedrock打造自定义AI智能体Agent(上)
人工智能·科技·ai·语言模型·云计算·aws·亚马逊云科技
云空3 小时前
《DeepSeek 网页/API 性能异常(DeepSeek Web/API Degraded Performance):网络安全日志》
运维·人工智能·web安全·网络安全·开源·网络攻击模型·安全威胁分析
AIGC大时代3 小时前
对比DeepSeek、ChatGPT和Kimi的学术写作关键词提取能力
论文阅读·人工智能·chatgpt·数据分析·prompt
Fhd-学习笔记3 小时前
《大语言模型》综述学习笔记
笔记·学习·语言模型
山晨啊84 小时前
2025年美赛B题-结合Logistic阻滞增长模型和SIR传染病模型研究旅游可持续性-成品论文
人工智能·机器学习
一水鉴天4 小时前
为AI聊天工具添加一个知识系统 之77 详细设计之18 正则表达式 之5
人工智能·正则表达式