Chat template cleanups + latest fixes (#76)

- Chat template cleanups + latest fixes (22cf5e08d6b96b5266fc75aa4512dcbee33e8ea1)

Co-authored-by: Matthew Carrigan <Rocketknight1@users.noreply.huggingface.co>
This commit is contained in:
ai-modelscope
2025-08-08 07:09:02 +08:00
parent 399e97d9ee
commit 71fc9e89c7

View File

@ -84,8 +84,7 @@
{%- elif param_spec.type == "object" -%} {%- elif param_spec.type == "object" -%}
{%- if param_spec.properties -%} {%- if param_spec.properties -%}
{{- "{ {{- "{\n" }}
" }}
{%- for prop_name, prop_spec in param_spec.properties.items() -%} {%- for prop_name, prop_spec in param_spec.properties.items() -%}
{{- prop_name -}} {{- prop_name -}}
{%- if prop_name not in (param_spec.required or []) -%} {%- if prop_name not in (param_spec.required or []) -%}
@ -107,24 +106,17 @@
{%- endmacro -%} {%- endmacro -%}
{%- macro render_tool_namespace(namespace_name, tools) -%} {%- macro render_tool_namespace(namespace_name, tools) -%}
{{- "## " + namespace_name + " {{- "## " + namespace_name + "\n\n" }}
{{- "namespace " + namespace_name + " {\n\n" }}
" }}
{{- "namespace " + namespace_name + " {
" }}
{%- for tool in tools %} {%- for tool in tools %}
{%- set tool = tool.function %} {%- set tool = tool.function %}
{{- "// " + tool.description + " {{- "// " + tool.description + "\n" }}
" }}
{{- "type "+ tool.name + " = " }} {{- "type "+ tool.name + " = " }}
{%- if tool.parameters and tool.parameters.properties %} {%- if tool.parameters and tool.parameters.properties %}
{{- "(_: { {{- "(_: {\n" }}
" }}
{%- for param_name, param_spec in tool.parameters.properties.items() %} {%- for param_name, param_spec in tool.parameters.properties.items() %}
{%- if param_spec.description %} {%- if param_spec.description %}
{{- "// " + param_spec.description + " {{- "// " + param_spec.description + "\n" }}
" }}
{%- endif %} {%- endif %}
{{- param_name }} {{- param_name }}
{%- if param_name not in (tool.parameters.required or []) -%} {%- if param_name not in (tool.parameters.required or []) -%}
@ -142,20 +134,14 @@
{%- endif -%} {%- endif -%}
{%- endif -%} {%- endif -%}
{%- if not loop.last %} {%- if not loop.last %}
{{- ", {{- ",\n" }}
" }}
{%- else %} {%- else %}
{{- " {{- ",\n" }}
" }}
{%- endif -%} {%- endif -%}
{%- endfor %} {%- endfor %}
{{- "}) => any; {{- "}) => any;\n\n" }}
" }}
{%- else -%} {%- else -%}
{{- "() => any; {{- "() => any;\n\n" }}
" }}
{%- endif -%} {%- endif -%}
{%- endfor %} {%- endfor %}
{{- "} // namespace " + namespace_name }} {{- "} // namespace " + namespace_name }}
@ -163,92 +149,46 @@
{%- macro render_builtin_tools(browser_tool, python_tool) -%} {%- macro render_builtin_tools(browser_tool, python_tool) -%}
{%- if browser_tool %} {%- if browser_tool %}
{{- "## browser {{- "## browser\n\n" }}
{{- "// Tool for browsing.\n" }}
" }} {{- "// The `cursor` appears in brackets before each browsing display: `[{cursor}]`.\n" }}
{{- "// Tool for browsing. {{- "// Cite information from the tool using the following format:\n" }}
" }} {{- "// `【{cursor}†L{line_start}(-L{line_end})?】`, for example: `【6†L9-L11】` or `【8†L3】`.\n" }}
{{- "// The `cursor` appears in brackets before each browsing display: `[{cursor}]`. {{- "// Do not quote more than 10 words directly from the tool output.\n" }}
" }} {{- "// sources=web (default: web)\n" }}
{{- "// Cite information from the tool using the following format: {{- "namespace browser {\n\n" }}
" }} {{- "// Searches for information related to `query` and displays `topn` results.\n" }}
{{- "// `【{cursor}†L{line_start}(-L{line_end})?】`, for example: `【6†L9-L11】` or `【8†L3】`. {{- "type search = (_: {\n" }}
" }} {{- "query: string,\n" }}
{{- "// Do not quote more than 10 words directly from the tool output. {{- "topn?: number, // default: 10\n" }}
" }} {{- "source?: string,\n" }}
{{- "// sources=web (default: web) {{- "}) => any;\n\n" }}
" }} {{- "// Opens the link `id` from the page indicated by `cursor` starting at line number `loc`, showing `num_lines` lines.\n" }}
{{- "namespace browser { {{- "// Valid link ids are displayed with the formatting: `【{id}†.*】`.\n" }}
{{- "// If `cursor` is not provided, the most recent page is implied.\n" }}
" }} {{- "// If `id` is a string, it is treated as a fully qualified URL associated with `source`.\n" }}
{{- "// Searches for information related to `query` and displays `topn` results. {{- "// If `loc` is not provided, the viewport will be positioned at the beginning of the document or centered on the most relevant passage, if available.\n" }}
" }} {{- "// Use this function without `id` to scroll to a new location of an opened page.\n" }}
{{- "type search = (_: { {{- "type open = (_: {\n" }}
" }} {{- "id?: number | string, // default: -1\n" }}
{{- "query: string, {{- "cursor?: number, // default: -1\n" }}
" }} {{- "loc?: number, // default: -1\n" }}
{{- "topn?: number, // default: 10 {{- "num_lines?: number, // default: -1\n" }}
" }} {{- "view_source?: boolean, // default: false\n" }}
{{- "source?: string, {{- "source?: string,\n" }}
" }} {{- "}) => any;\n\n" }}
{{- "}) => any; {{- "// Finds exact matches of `pattern` in the current page, or the page given by `cursor`.\n" }}
{{- "type find = (_: {\n" }}
" }} {{- "pattern: string,\n" }}
{{- "// Opens the link `id` from the page indicated by `cursor` starting at line number `loc`, showing `num_lines` lines. {{- "cursor?: number, // default: -1\n" }}
" }} {{- "}) => any;\n\n" }}
{{- "// Valid link ids are displayed with the formatting: `【{id}†.*】`. {{- "} // namespace browser\n\n" }}
" }}
{{- "// If `cursor` is not provided, the most recent page is implied.
" }}
{{- "// If `id` is a string, it is treated as a fully qualified URL associated with `source`.
" }}
{{- "// If `loc` is not provided, the viewport will be positioned at the beginning of the document or centered on the most relevant passage, if available.
" }}
{{- "// Use this function without `id` to scroll to a new location of an opened page.
" }}
{{- "type open = (_: {
" }}
{{- "id?: number | string, // default: -1
" }}
{{- "cursor?: number, // default: -1
" }}
{{- "loc?: number, // default: -1
" }}
{{- "num_lines?: number, // default: -1
" }}
{{- "view_source?: boolean, // default: false
" }}
{{- "source?: string,
" }}
{{- "}) => any;
" }}
{{- "// Finds exact matches of `pattern` in the current page, or the page given by `cursor`.
" }}
{{- "type find = (_: {
" }}
{{- "pattern: string,
" }}
{{- "cursor?: number, // default: -1
" }}
{{- "}) => any;
" }}
{{- "} // namespace browser
" }}
{%- endif -%} {%- endif -%}
{%- if python_tool %} {%- if python_tool %}
{{- "## python {{- "## python\n\n" }}
{{- "Use this tool to execute Python code in your chain of thought. The code will not be shown to the user. This tool should be used for internal reasoning, but not for code that is intended to be visible to the user (e.g. when creating plots, tables, or files).\n\n" }}
" }} {{- "When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 120.0 seconds. The drive at '/mnt/data' can be used to save and persist user files. Internet access for this session is UNKNOWN. Depends on the cluster.\n\n" }}
{{- "Use this tool to execute Python code in your chain of thought. The code will not be shown to the user. This tool should be used for internal reasoning, but not for code that is intended to be visible to the user (e.g. when creating plots, tables, or files).
" }}
{{- "When you send a message containing Python code to python, it will be executed in a stateful Jupyter notebook environment. python will respond with the output of the execution or time out after 120.0 seconds. The drive at '/mnt/data' can be used to save and persist user files. Internet access for this session is UNKNOWN. Depends on the cluster.
" }}
{%- endif -%} {%- endif -%}
{%- endmacro -%} {%- endmacro -%}
@ -257,23 +197,15 @@
{%- if model_identity is not defined %} {%- if model_identity is not defined %}
{%- set model_identity = "You are ChatGPT, a large language model trained by OpenAI." %} {%- set model_identity = "You are ChatGPT, a large language model trained by OpenAI." %}
{%- endif %} {%- endif %}
{{- model_identity + " {{- model_identity + "\n" }}
" }} {{- "Knowledge cutoff: 2024-06\n" }}
{{- "Knowledge cutoff: 2024-06 {{- "Current date: " + strftime_now("%Y-%m-%d") + "\n\n" }}
" }}
{{- "Current date: " + strftime_now("%Y-%m-%d") + "
" }}
{%- if reasoning_effort is not defined %} {%- if reasoning_effort is not defined %}
{%- set reasoning_effort = "medium" %} {%- set reasoning_effort = "medium" %}
{%- endif %} {%- endif %}
{{- "Reasoning: " + reasoning_effort + " {{- "Reasoning: " + reasoning_effort + "\n\n" }}
" }}
{%- if builtin_tools %} {%- if builtin_tools %}
{{- "# Tools {{- "# Tools\n\n" }}
" }}
{%- set available_builtin_tools = namespace(browser=false, python=false) %} {%- set available_builtin_tools = namespace(browser=false, python=false) %}
{%- for tool in builtin_tools %} {%- for tool in builtin_tools %}
{%- if tool == "browser" %} {%- if tool == "browser" %}
@ -286,8 +218,7 @@
{%- endif -%} {%- endif -%}
{{- "# Valid channels: analysis, commentary, final. Channel must be included for every message." }} {{- "# Valid channels: analysis, commentary, final. Channel must be included for every message." }}
{%- if tools -%} {%- if tools -%}
{{- " {{- "\nCalls to these tools must go to the commentary channel: 'functions'." }}
Calls to these tools must go to the commentary channel: 'functions'." }}
{%- endif -%} {%- endif -%}
{%- endmacro -%} {%- endmacro -%}
@ -312,18 +243,12 @@ Calls to these tools must go to the commentary channel: 'functions'." }}
{%- if developer_message or tools %} {%- if developer_message or tools %}
{{- "<|start|>developer<|message|>" }} {{- "<|start|>developer<|message|>" }}
{%- if developer_message %} {%- if developer_message %}
{{- "# Instructions {{- "# Instructions\n\n" }}
" }}
{{- developer_message }} {{- developer_message }}
{%- endif %} {%- endif %}
{%- if tools -%} {%- if tools -%}
{{- " {{- "\n\n" }}
{{- "# Tools\n\n" }}
" }}
{{- "# Tools
" }}
{{- render_tool_namespace("functions", tools) }} {{- render_tool_namespace("functions", tools) }}
{%- endif -%} {%- endif -%}
{{- "<|end|>" }} {{- "<|end|>" }}
@ -346,6 +271,15 @@ Calls to these tools must go to the commentary channel: 'functions'." }}
{%- endif %} {%- endif %}
{%- endif %} {%- endif %}
{%- if "tool_calls" in message %} {%- if "tool_calls" in message %}
{#- We need very careful handling here - we want to drop the tool call analysis message if the model #}
{#- has output a later <|final|> message, but otherwise we want to retain it. This is the only case #}
{#- when we render CoT/analysis messages in inference. #}
{%- set future_final_message = namespace(found=false) %}
{%- for future_message in loop_messages[loop.index:] %}
{%- if future_message.role == 'assistant' and "tool_calls" not in future_message %}
{%- set future_final_message.found = true %}
{%- endif %}
{%- endfor %}
{#- We assume max 1 tool call per message, and so we infer the tool call name #} {#- We assume max 1 tool call per message, and so we infer the tool call name #}
{#- in "tool" messages from the most recent assistant tool call name #} {#- in "tool" messages from the most recent assistant tool call name #}
{%- set tool_call = message.tool_calls[0] %} {%- set tool_call = message.tool_calls[0] %}
@ -354,9 +288,9 @@ Calls to these tools must go to the commentary channel: 'functions'." }}
{%- endif %} {%- endif %}
{%- if message.content and message.thinking %} {%- if message.content and message.thinking %}
{{- raise_exception("Cannot pass both content and thinking in an assistant message with tool calls! Put the analysis message in one or the other, but not both.") }} {{- raise_exception("Cannot pass both content and thinking in an assistant message with tool calls! Put the analysis message in one or the other, but not both.") }}
{%- elif message.content %} {%- elif message.content and not future_final_message.found %}
{{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }} {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }}
{%- elif message.thinking %} {%- elif message.thinking and not future_final_message.found %}
{{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }} {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
{%- endif %} {%- endif %}
{{- "<|start|>assistant to=" }} {{- "<|start|>assistant to=" }}