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

相关推荐
b***251111 小时前
深圳比斯特|多维度分选:圆柱电池品质管控的自动化解决方案
大数据·人工智能
金井PRATHAMA11 小时前
AI赋能训诂学:解码古籍智能新纪元
人工智能·自然语言处理·知识图谱
练习两年半的工程师11 小时前
AWS TechFest 2025: 智能体企业级开发流程、Strands Agents
人工智能·云计算·aws
Hello123网站11 小时前
Whismer-你的定制化AI问答助手
人工智能·chatgpt·ai工具
yinmaisoft11 小时前
当低代码遇上AI,有趣,实在有趣
android·人工智能·低代码·开发工具·rxjava
正经教主12 小时前
【慢教程】Ollama4:ollama命令汇总
人工智能·ollama
大翻哥哥12 小时前
Python 2025:AI工程化与智能代理开发实战
开发语言·人工智能·python
中杯可乐多加冰12 小时前
深度解析文心大模型X1.1:智能涌现与技术革新
人工智能
用户51914958484512 小时前
揭秘LedgerCTF的AES白盒挑战:逆向工程与密码学分析
人工智能·aigc
用户51914958484512 小时前
SonicWall防火墙安全态势深度分析:固件解密与漏洞洞察
人工智能·aigc