robbiemu/
qwen3-coder:30b-a3b-i-q4_K_XL

101 3 weeks ago

tools
2f27310e9133 · 4.1kB
{{- /* 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 XML-style 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, $val := $param_fields.enum }}{{ if gt $i 0 }}, {{ end }}`{{ $val }}`{{ end }}]</enum>
{{- end }}
{{- if $param_fields.required }}
<required>[{{ range $i, $req := $param_fields.required }}{{ if gt $i 0 }}, {{ end }}`{{ $req }}`{{ end }}]</required>
{{- end }}
</parameter>
{{- end -}}
{{- end -}}
{{- if $tool.Parameters.required }}
<required>[{{ range $i, $req := $tool.Parameters.required }}{{ if gt $i 0 }}, {{ end }}`{{ $req }}`{{ end }}]</required>
{{- 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 with NO suffix:
<tool_call>
<function=example_function_name>
<parameter=example_parameter_1>
value_1
</parameter>
<parameter=example_parameter_2>
This is the value for the second parameter
that can span
multiple lines
</parameter>
</function>
</tool_call>
<IMPORTANT>
Reminder:
- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags
- Required parameters MUST be specified
- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after
- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls
</IMPORTANT>
{{- end -}}
{{- /* Close system message if it was opened */ -}}
{{- if or $system_message $has_tools -}}
<|im_end|>
{{- end -}}
{{- /* Process conversation messages with enhanced tool response handling */ -}}
{{- $in_tool_block := false -}}
{{- range $i, $message := $loop_messages -}}
{{- if eq $message.Role "tool" -}}
{{- if not $in_tool_block -}}
<|im_start|>user
{{- $in_tool_block = true -}}
{{- end }}
<tool_response>
{{ $message.Content }}
</tool_response>
{{- else if eq $message.Role "assistant" -}}
{{- if $in_tool_block -}}
<|im_end|>
{{- $in_tool_block = false -}}
{{- end }}
<|im_start|>assistant
{{- if $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 $args_name, $args_value := $tool_call.Arguments }}
<parameter={{ $args_name }}>
{{ $args_value }}
</parameter>
{{- end -}}
{{- end }}
</function>
</tool_call>
{{- end }}
<|im_end|>
{{- else -}}
{{- if $in_tool_block -}}
<|im_end|>
{{- $in_tool_block = false -}}
{{- end }}
<|im_start|>{{ $message.Role }}
{{ $message.Content }}<|im_end|>
{{- end -}}
{{- end -}}
{{- /* Always add generation prompt */ -}}
<|im_start|>assistant