Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions examples/chat_template/tool_chat_template_deepseekv31.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
{%- for tool in message['tool_calls'] %}
{%- if not ns.is_first %}
{%- if message['content'] is none %}
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] + '<|tool▁call▁end|>'}}
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
{%- else %}
{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] + '<|tool▁call▁end|>'}}
{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
{%- endif %}
{%- set ns.is_first = true -%}
{%- else %}
{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments'] + '<|tool▁call▁end|>'}}
{{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
{%- endif %}
{%- endfor %}
Comment on lines 43 to 54
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

感谢您修复此问题!添加 |tojson 过滤器是正确的,可以确保工具调用的参数被正确序列化为 JSON 字符串。

另外,为了提高代码的可读性和可维护性,我建议重构这部分逻辑以消除重复。当前的实现中,构建工具调用字符串的逻辑在多个地方重复出现。通过使用 Jinja 的 loop.first 变量和定义一个局部变量,可以使模板更简洁、更易于理解。

您可以考虑应用以下建议的重构:

    {%- for tool in message['tool_calls'] %}
      {%- set tool_call_str = '<|tool call begin|>' + tool['function']['name'] + '<|tool sep|>' + (tool['function']['arguments']|tojson) + '<|tool call end|>' -%}
      {%- if loop.first %}
        {%- if message['content'] is not none -%}
          {{- message['content'] -}}
        {%- endif -%}
        {{- '<|tool calls begin|>' -}}
      {%- endif -%}
      {{- tool_call_str -}}
    {%- endfor %}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, looks reasonable

{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
Expand Down
Loading