tool-call(dsv3): Improve deepseek-v3 chat template and tool_choice = required (#9525)

This commit is contained in:
Chang Su
2025-08-23 01:46:56 -07:00
committed by GitHub
parent 6b2b8bf0e1
commit c9dd70fbde
2 changed files with 17 additions and 17 deletions
@@ -12,7 +12,7 @@
{% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %} {% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
{%- endif %} {%- endif %}
{%- endif %} {%- endif %}
{%- endfor %} {%- endfor -%}
{# --- Append tool descriptions if tools are defined --- #} {# --- Append tool descriptions if tools are defined --- #}
{% if tools is defined and tools is not none %} {% if tools is defined and tools is not none %}
@@ -23,13 +23,13 @@
'Make sure the JSON is valid.' 'Make sure the JSON is valid.'
'## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %} '## Tools\n\n### Function\n\nYou have the following functions available:\n\n') %}
{% for tool in tools %} {% for tool in tools %}
{% set tool_ns.text = tool_ns.text + '- `' + tool['name'] + '`:\n```json\n' + (tool | tojson) + '\n```\n' %} {% set tool_ns.text = tool_ns.text + '\n```json\n' + (tool | tojson) + '\n```\n' %}
{% endfor %} {% endfor %}
{% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %} {% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
{% endif %} {% endif %}
{{ bos_token }} {{- bos_token }}
{{ ns.system_prompt }} {{- ns.system_prompt }}
{%- for message in messages %} {%- for message in messages %}
{%- if message['role'] == 'user' %} {%- if message['role'] == 'user' %}
@@ -41,7 +41,7 @@
{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %} {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
{%- set ns.is_last_user = false -%} {%- set ns.is_last_user = false -%}
{%- if ns.is_tool %} {%- if ns.is_tool %}
{{'<|tool▁outputs▁end|>'}} {{- '<|tool▁outputs▁end|>'}}
{%- endif %} {%- endif %}
{%- set ns.is_first = false %} {%- set ns.is_first = false %}
{%- set ns.is_tool = false -%} {%- set ns.is_tool = false -%}
@@ -49,43 +49,43 @@
{%- for tool in message['tool_calls'] %} {%- for tool in message['tool_calls'] %}
{%- if not ns.is_first %} {%- if not ns.is_first %}
{%- if message['content'] is none %} {%- if message['content'] is none %}
{{'<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}} {{- '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
{%- else %} {%- else %}
{{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}} {{- message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments']|tojson + '\n' + '```' + '<|tool▁call▁end|>'}}
{%- endif %} {%- endif %}
{%- set ns.is_first = true -%} {%- set ns.is_first = true -%}
{%- else %} {%- else %}
{{'\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}} {{- '\n' + '<|tool▁call▁begin|>' + tool['type'] + '<|tool▁sep|>' + tool['function']['name'] + '\n' + '```json' + '\n' + tool['function']['arguments'] + '\n' + '```' + '<|tool▁call▁end|>'}}
{%- endif %} {%- endif %}
{%- endfor %} {%- endfor %}
{{'<|tool▁calls▁end|><|end▁of▁sentence|>'}} {{- '<|tool▁calls▁end|><|end▁of▁sentence|>'}}
{%- endif %} {%- endif %}
{%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%} {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none)%}
{%- set ns.is_last_user = false -%} {%- set ns.is_last_user = false -%}
{%- if ns.is_tool %} {%- if ns.is_tool %}
{{'<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}} {{- '<|tool▁outputs▁end|>' + message['content'] + '<|end▁of▁sentence|>'}}
{%- set ns.is_tool = false -%} {%- set ns.is_tool = false -%}
{%- else %} {%- else %}
{% set content = message['content'] %} {% set content = message['content'] %}
{{content + '<|end▁of▁sentence|>'}} {{- content + '<|end▁of▁sentence|>'}}
{%- endif %} {%- endif %}
{%- endif %} {%- endif %}
{%- if message['role'] == 'tool' %} {%- if message['role'] == 'tool' %}
{%- set ns.is_last_user = false -%} {%- set ns.is_last_user = false -%}
{%- set ns.is_tool = true -%} {%- set ns.is_tool = true -%}
{%- if ns.is_output_first %} {%- if ns.is_output_first %}
{{ 'Use the results below to formulate an answer to the user question unless additional information is needed.' }} {{- 'Use the results below to formulate an answer to the user question unless additional information is needed.' }}
{{'<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}} {{- '<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
{%- set ns.is_output_first = false %} {%- set ns.is_output_first = false %}
{%- else %} {%- else %}
{{'\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}} {{- '\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
{%- endif %} {%- endif %}
{%- endif %} {%- endif %}
{%- endfor -%} {%- endfor -%}
{% if ns.is_tool %} {% if ns.is_tool %}
{{"<|tool▁outputs▁end|>"}} {{- '<|tool▁outputs▁end|>'}}
{% endif %} {% endif %}
{% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %} {% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}
{{'<|Assistant|>'}} {{- '<|Assistant|>'}}
{% endif %} {% endif %}