Skip to content

Commit d1fe943

Browse files
committed
1 parent 1ba0322 commit d1fe943

File tree

1 file changed

+35
-48
lines changed

1 file changed

+35
-48
lines changed

models/templates/Qwen3-Coder.jinja

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
1-
{% macro render_item_list(item_list, tag_name='required') %}
2-
{%- if item_list is defined and item_list is iterable and item_list | length > 0 %}
3-
{%- if tag_name %}{{- '\n<' ~ tag_name ~ '>' -}}{% endif %}
4-
{{- '[' }}
5-
{%- for item in item_list -%}
6-
{%- if loop.index > 1 %}{{- ", "}}{% endif -%}
7-
{%- if item is string -%}
8-
{{ "`" ~ item ~ "`" }}
9-
{%- else -%}
10-
{{ item }}
11-
{%- endif -%}
12-
{%- endfor -%}
13-
{{- ']' }}
14-
{%- if tag_name %}{{- '</' ~ tag_name ~ '>' -}}{% endif %}
1+
{% macro render_extra_keys(json_dict, handled_keys) %}
2+
{%- if json_dict is mapping %}
3+
{%- for json_key in json_dict if json_key not in handled_keys %}
4+
{%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) %}
5+
{{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '</' ~ json_key ~ '>' }}
6+
{%- else %}
7+
{{-'\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '</' ~ json_key ~ '>' }}
8+
{%- endif %}
9+
{%- endfor %}
1510
{%- endif %}
1611
{% endmacro %}
12+
1713
{%- if messages[0]["role"] == "system" %}
1814
{%- set system_message = messages[0]["content"] %}
1915
{%- set loop_messages = messages[1:] %}
2016
{%- else %}
2117
{%- set loop_messages = messages %}
2218
{%- endif %}
19+
2320
{%- if not tools is defined %}
2421
{%- set tools = [] %}
2522
{%- endif %}
23+
2624
{%- if system_message is defined %}
2725
{{- "<|im_start|>system\n" + system_message }}
2826
{%- else %}
@@ -31,48 +29,37 @@
3129
{%- endif %}
3230
{%- endif %}
3331
{%- if tools is iterable and tools | length > 0 %}
34-
{{- "\n\nYou have access to the following functions:\n\n" }}
32+
{{- "\n\n# Tools\n\nYou have access to the following functions:\n\n" }}
3533
{{- "<tools>" }}
3634
{%- for tool in tools %}
3735
{%- if tool.function is defined %}
3836
{%- set tool = tool.function %}
3937
{%- endif %}
4038
{{- "\n<function>\n<name>" ~ tool.name ~ "</name>" }}
41-
{{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
39+
{%- if tool.description is defined %}
40+
{{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
41+
{%- endif %}
4242
{{- '\n<parameters>' }}
43-
{%- for param_name, param_fields in tool.parameters.properties|items %}
44-
{{- '\n<parameter>' }}
45-
{{- '\n<name>' ~ param_name ~ '</name>' }}
46-
{%- if param_fields.type is defined %}
47-
{{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
48-
{%- endif %}
49-
{%- if param_fields.description is defined %}
50-
{{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
51-
{%- endif %}
52-
{{- render_item_list(param_fields.enum, 'enum') }}
53-
{%- set handled_keys = ['type', 'description', 'enum', 'required'] %}
54-
{%- for json_key, json_value in param_fields|items %}
55-
{%- if json_key not in handled_keys %}
56-
{%- set normed_json_key = json_key|string %}
57-
{%- if json_value is mapping %}
58-
{{- '\n<' ~ normed_json_key ~ '>' ~ (json_value | tojson | safe) ~ '</' ~ normed_json_key ~ '>' }}
59-
{%- else %}
60-
{{- '\n<' ~ normed_json_key ~ '>' ~ (json_value | string) ~ '</' ~ normed_json_key ~ '>' }}
61-
{%- endif %}
43+
{%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}
44+
{%- for param_name, param_fields in tool.parameters.properties|items %}
45+
{{- '\n<parameter>' }}
46+
{{- '\n<name>' ~ param_name ~ '</name>' }}
47+
{%- if param_fields.type is defined %}
48+
{{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
49+
{%- endif %}
50+
{%- if param_fields.description is defined %}
51+
{{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
6252
{%- endif %}
53+
{%- set handled_keys = ['name', 'type', 'description'] %}
54+
{{- render_extra_keys(param_fields, handled_keys) }}
55+
{{- '\n</parameter>' }}
6356
{%- endfor %}
64-
{{- render_item_list(param_fields.required, 'required') }}
65-
{{- '\n</parameter>' }}
66-
{%- endfor %}
67-
{{- render_item_list(tool.parameters.required, 'required') }}
68-
{{- '\n</parameters>' }}
69-
{%- if tool.return is defined %}
70-
{%- if tool.return is mapping %}
71-
{{- '\n<return>' ~ (tool.return | tojson | safe) ~ '</return>' }}
72-
{%- else %}
73-
{{- '\n<return>' ~ (tool.return | string) ~ '</return>' }}
74-
{%- endif %}
7557
{%- endif %}
58+
{% set handled_keys = ['type', 'properties'] %}
59+
{{- render_extra_keys(tool.parameters, handled_keys) }}
60+
{{- '\n</parameters>' }}
61+
{%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}
62+
{{- render_extra_keys(tool, handled_keys) }}
7663
{{- '\n</function>' }}
7764
{%- endfor %}
7865
{{- "\n</tools>" }}
@@ -99,7 +86,7 @@
9986
{%- if tool_call.arguments is defined %}
10087
{%- for args_name, args_value in tool_call.arguments|items %}
10188
{{- '<parameter=' + args_name + '>\n' }}
102-
{%- set args_value = args_value if args_value is string else args_value | string %}
89+
{%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
10390
{{- args_value }}
10491
{{- '\n</parameter>\n' }}
10592
{%- endfor %}
@@ -127,4 +114,4 @@
127114
{%- endfor %}
128115
{%- if add_generation_prompt %}
129116
{{- '<|im_start|>assistant\n' }}
130-
{%- endif %}
117+
{%- endif %}

0 commit comments

Comments
 (0)