miss chat template file?

#4
by reking - opened

no chat template in tokenizer_config.json file, could we use the file provided in V3.2-exp

but tool call changed a lot

I tried the v3.2-exp template, and it looks normal in scenarios not involving tool calls.

Is there anyone have a solution?

Quoting from the README:

import transformers
# encoding/encoding_dsv32.py
from encoding_dsv32 import encode_messages, parse_message_from_completion_text

tokenizer = transformers.AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-V3.2")

messages = [
    {"role": "user", "content": "hello"},
    {"role": "assistant", "content": "Hello! I am DeepSeek.", "reasoning_content": "thinking..."},
    {"role": "user", "content": "1+1=?"}
]
encode_config = dict(thinking_mode="thinking", drop_thinking=True, add_default_bos_token=True)

# messages -> string
prompt = encode_messages(messages, **encode_config)
# Output: "<|begin▁of▁sentence|><|User|>hello<|Assistant|></think>Hello! I am DeepSeek.<|end▁of▁sentence|><|User|>1+1=?<|Assistant|><think>"

# string -> tokens
tokens = tokenizer.encode(prompt)
# Output: [0, 128803, 33310, 128804, 128799, 19923, 3, 342, 1030, 22651, 4374, 1465, 16, 1, 128803, 19, 13, 19, 127252, 128804, 128798]

This release does not include a Jinja-format chat template. Please refer to the Python code mentioned above.


The encode_messagesfunction acts roughly the same way as a typical Jinja2 chat template, where given messages and other args, in return, you'll get the "rendered" prompt string that you can send for Completions. A slight thing to look out for are for tool definitions, response format, etc, where this new approach defines them as messages attributes.

I encourage you to read through the render_messages() function. It is very intuitive and easy to read. For me personally, this is a better approach and is better than reading through Jinja2 template. For the most part, the encoding_dsv32.py file still contains quite a lot of template strings. But the logic and branching is easier to read through and debug, because all is done in standard Python.

Hope this helps.

Sign up or log in to comment