baichuan-inc / Baichuan2

A series of large language models developed by Baichuan Intelligent Technology
https://huggingface.co/baichuan-inc
Apache License 2.0
4.03k stars 286 forks source link

Baichuan2 Chat Template #392

Closed YanshekWoo closed 3 months ago

YanshekWoo commented 3 months ago

似乎目前版本的baichuan2还未提供huggingface支持的chat template 我自己写了一份chat template jinja如下:

{% for message in messages %}

{% if message['role'] == 'user' %}
{{ '<reserved_106>' + message['content'] }}
{% elif message['role'] == 'assistant' %}
{{ '<reserved_107>' + message['content'] + '</s>'}}
{% endif %}

{% endfor %}

{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}
{{ '<reserved_107>' }}
{% endif %}

即在tokenizer_config.json添加如下字段

"chat_template": "{% for message in messages %}{% if message['role'] == 'user' %}{{ '<reserved_106>' + message['content'] }}{% elif message['role'] == 'assistant' %}{{ '<reserved_107>' + message['content'] + '</s>'}}{% endif %}{% endfor %}{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}{{ '<reserved_107>' }}{% endif %}"

添加后可以正常使用huggingface的tokenizer.apply_chat_template和vllm调用

okwinds commented 3 months ago

reserved_107是什么? 请教一下,这类chat template一般应该怎么写? 有的模型是支持工具调用以及代码解释器的,这类又应该怎么写?也可以写入chat template吗? 多谢!

YanshekWoo commented 3 months ago

reserved_107是什么? 请教一下,这类chat template一般应该怎么写? 有的模型是支持工具调用以及代码解释器的,这类又应该怎么写?也可以写入chat template吗? 多谢!

<reserved_107>是预定义的special token。 chat template是参考huggingface官方文档写的。该template是为了多轮对话的prompt设计的。 我理解的工具调用之类的template应该由你自己设计定义好后输入到user轮的对话内容中

supdizh commented 3 months ago

给LZ点赞,vllm 文档支持bc2但其实用的hf chat template, 还在想咋改 发现LZ这,好用,chat template jinja是什么魔法写法

okwinds commented 3 months ago

谢谢回复

reserved_107是什么? 请教一下,这类chat template一般应该怎么写? 有的模型是支持工具调用以及代码解释器的,这类又应该怎么写?也可以写入chat template吗? 多谢!

<reserved_107>是预定义的special token。

chat template是参考huggingface官方文档写的。该template是为了多轮对话的prompt设计的。

我理解的工具调用之类的template应该由你自己设计定义好后输入到user轮的对话内容中

QwertyJack commented 1 month ago

是不是没有考虑 system ? 另外,请问 </s> 是为什么?

YanshekWoo commented 1 month ago

是不是没有考虑 system ? 另外,请问 </s> 是为什么?

Baichuan原生就没有system message,这段代码是可以兼容处理有system message的messages列表的。 </s>之类special token都是baichuan chat模型微调用的,参照他代码里给的prompt