Skip to content

Instantly share code, notes, and snippets.

@robbiemu
Created August 21, 2025 18:03
Show Gist options
  • Select an option

  • Save robbiemu/02baf6cf8db43f50ee8beaff629f1ff1 to your computer and use it in GitHub Desktop.

Select an option

Save robbiemu/02baf6cf8db43f50ee8beaff629f1ff1 to your computer and use it in GitHub Desktop.
qwen3 coder template
{{- /* Extract system message and other messages */ -}}
{{- $system_message := "" -}}
{{- $loop_messages := .Messages -}}
{{- if and .Messages (gt (len .Messages) 0) (eq (index .Messages 0).Role "system") -}}
{{- $system_message = (index .Messages 0).Content -}}
{{- $loop_messages = slice .Messages 1 -}}
{{- end -}}
{{- /* Handle tools if they exist */ -}}
{{- $has_tools := and .Tools (gt (len .Tools) 0) -}}
{{- /* System message */ -}}
{{- if $system_message -}}
<|im_start|>system
{{ $system_message }}
{{- else if $has_tools -}}
<|im_start|>system
You are Qwen, a helpful AI assistant that can interact with a computer to solve tasks.
{{- end -}}
{{- /* Add tools section if tools exist */ -}}
{{- if $has_tools -}}
You have access to the following functions:
<tools>
{{- range .Tools -}}
{{- $tool := . -}}
{{- if .Function -}}
{{- $tool = .Function -}}
{{- end }}
<function>
<name>{{ $tool.Name }}</name>
<description>{{ $tool.Description }}</description>
<parameters>
{{- if $tool.Parameters -}}
{{- if $tool.Parameters.Properties -}}
{{- range $param_name, $param_fields := $tool.Parameters.Properties }}
<parameter>
<name>{{ $param_name }}</name>
{{- if $param_fields.Type }}
<type>{{ $param_fields.Type }}</type>
{{- end }}
{{- if $param_fields.Description }}
<description>{{ $param_fields.Description }}</description>
{{- end }}
{{- if $param_fields.Enum }}
<enum>[{{ range $i, $v := $param_fields.Enum }}{{ if gt $i 0 }}, {{ end }}`{{ $v }}`{{ end }}]</enum>
{{- end }}
</parameter>
{{- end }}
{{- if $tool.Parameters.Required }}
<required>[{{ range $i, $v := $tool.Parameters.Required }}{{ if gt $i 0 }}, {{ end }}`{{ $v }}`{{ end }}]</required>
{{- end }}
{{- end }}
{{- end }}
</parameters>
{{- if $tool.Return }}
<return>{{ $tool.Return }}</return>
{{- end }}
</function>
{{- end }}
</tools>
If you choose to call a function ONLY reply in the following format:
<tool_call>
<function=example_function_name>
<parameter=example_parameter_1>
value_1
</parameter>
<parameter=example_parameter_2>
value_2
</parameter>
</function>
</tool_call>
<IMPORTANT>
Reminder:
- Use only the above XML format with correct tag nesting.
- `function=` tag must appear exactly like shown.
- Each parameter must have opening and closing tags.
- Multiline values are supported inside <parameter>...</parameter>.
- No trailing output or suffix allowed after </tool_call>.
</IMPORTANT>
{{- end -}}
{{- /* Close system message if it was opened */ -}}
{{- if or $system_message $has_tools -}}
<|im_end|>
{{- end -}}
{{- /* Process conversation messages */ -}}
{{- range $i, $message := $loop_messages -}}
{{- if and (eq $message.Role "assistant") $message.ToolCalls (gt (len $message.ToolCalls) 0) -}}
<|im_start|>{{ $message.Role }}
{{- if and $message.Content (ne $message.Content "") }}
{{ $message.Content }}
{{- end -}}
{{- range $message.ToolCalls -}}
{{- $tool_call := . -}}
{{- if .Function -}}
{{- $tool_call = .Function -}}
{{- end }}
<tool_call>
<function={{ $tool_call.Name }}>
{{- if $tool_call.Arguments -}}
{{- range $arg_name, $arg_value := $tool_call.Arguments }}
<parameter={{ $arg_name }}>
{{ $arg_value }}
</parameter>
{{- end -}}
{{- end }}
</function>
</tool_call>
{{- end }}
<|im_end|>
{{- else if or (eq $message.Role "user") (eq $message.Role "system") (eq $message.Role "assistant") -}}
<|im_start|>{{ $message.Role }}
{{ $message.Content }}<|im_end|>
{{- else if eq $message.Role "tool" -}}
{{- /* Check if previous message was not a tool */ -}}
{{- $prev_index := sub $i 1 -}}
{{- $prev_role := "" -}}
{{- if ge $prev_index 0 -}}
{{- $prev_role = (index $loop_messages $prev_index).Role -}}
{{- end -}}
{{- if ne $prev_role "tool" }}
<|im_start|>user
{{- end }}
<tool_response>
{{ $message.Content }}
</tool_response>
{{- /* Check if next message is not a tool or if this is the last message */ -}}
{{- $next_index := add $i 1 -}}
{{- $next_role := "" -}}
{{- $is_last := eq $next_index (len $loop_messages) -}}
{{- if not $is_last -}}
{{- $next_role = (index $loop_messages $next_index).Role -}}
{{- end -}}
{{- if or $is_last (ne $next_role "tool") }}
<|im_end|>
{{- end -}}
{{- else -}}
<|im_start|>{{ $message.Role }}
{{ $message.Content }}<|im_end|>
{{- end -}}
{{- end -}}
{{- /* Add generation prompt if needed */ -}}
{{- if .AddGenerationPrompt -}}
<|im_start|>assistant
{{- end -}}
{{- /* Extract system message and other messages */ -}}
{{- $system_message := "" -}}
{{- $loop_messages := .Messages -}}
{{- if and .Messages (gt (len .Messages) 0) (eq (index .Messages 0).Role "system") -}}
{{- $system_message = (index .Messages 0).Content -}}
{{- $loop_messages = slice .Messages 1 -}}
{{- end -}}
{{- /* Handle tools if they exist */ -}}
{{- $has_tools := and .Tools (gt (len .Tools) 0) -}}
{{- /* System message */ -}}
{{- if $system_message -}}
<|im_start|>system
{{ $system_message }}
{{- else if $has_tools -}}
<|im_start|>system
You are Qwen, a helpful AI assistant that can interact with a computer to solve tasks.
{{- end -}}
{{- /* Add tools section if tools exist */ -}}
{{- if $has_tools -}}
You have access to the following functions:
{{ range .Tools -}}
{{- $tool := . -}}
{{- if .Function -}}
{{- $tool = .Function -}}
{{- end }}
- {{ $tool.Name }}: {{ $tool.Description }}
Parameters: {{ if $tool.Parameters }}{{ $tool.Parameters | toJson }}{{ else }}{}{{ end }}
{{ end }}
To call a function, respond with JSON in this format:
<tool_call>
{"name": "function_name", "arguments": {"param1": "value1", "param2": "value2"}}
</tool_call>
IMPORTANT: Only use the exact JSON format shown above. Do not use any other XML tags or formats.
{{- end -}}
{{- /* Close system message if it was opened */ -}}
{{- if or $system_message $has_tools -}}
<|im_end|>
{{- end -}}
{{- /* Process conversation messages */ -}}
{{- range $i, $message := $loop_messages -}}
{{- if and (eq $message.Role "assistant") $message.ToolCalls (gt (len $message.ToolCalls) 0) -}}
<|im_start|>{{ $message.Role }}
{{- if and $message.Content (ne $message.Content "") }}
{{ $message.Content }}
{{- end -}}
{{- range $message.ToolCalls -}}
{{- $tool_call := . -}}
{{- if .Function -}}
{{- $tool_call = .Function -}}
{{- end }}
<tool_call>
{"name": "{{ $tool_call.Name }}", "arguments": {{ $tool_call.Arguments | toJson }}}
</tool_call>
{{- end }}
<|im_end|>
{{- else if or (eq $message.Role "user") (eq $message.Role "system") (eq $message.Role "assistant") -}}
<|im_start|>{{ $message.Role }}
{{ $message.Content }}<|im_end|>
{{- else if eq $message.Role "tool" -}}
{{- /* Check if previous message was not a tool */ -}}
{{- $prev_index := sub $i 1 -}}
{{- $prev_role := "" -}}
{{- if ge $prev_index 0 -}}
{{- $prev_role = (index $loop_messages $prev_index).Role -}}
{{- end -}}
{{- if ne $prev_role "tool" }}
<|im_start|>user
{{- end }}
<tool_response>
{{ $message.Content }}
</tool_response>
{{- /* Check if next message is not a tool or if this is the last message */ -}}
{{- $next_index := add $i 1 -}}
{{- $next_role := "" -}}
{{- $is_last := eq $next_index (len $loop_messages) -}}
{{- if not $is_last -}}
{{- $next_role = (index $loop_messages $next_index).Role -}}
{{- end -}}
{{- if or $is_last (ne $next_role "tool") }}
<|im_end|>
{{- end -}}
{{- else -}}
<|im_start|>{{ $message.Role }}
{{ $message.Content }}<|im_end|>
{{- end -}}
{{- end -}}
{{- /* Add generation prompt if needed */ -}}
{{- if .AddGenerationPrompt -}}
<|im_start|>assistant
{{- end -}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment