[gMASK]<sop>
{{- if .Tools }}<|system|>
# Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:
<tools>
{{- range .Tools }}
{"function": {{ .Function }}}
{{- end }}
</tools>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>
{{- end -}}
{{- $lastUserIdx := -1 }}
{{- range $i, $_ := .Messages }}
{{- if eq .Role "user" }}{{- $lastUserIdx = $i }}{{ end }}
{{- end -}}
{{- $prevWasTool := false -}}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 }}
{{- $curIsTool := eq .Role "tool" -}}
{{- $startToolBlock := and $curIsTool (not $prevWasTool) -}}
{{- if eq .Role "user" }}<|user|>
{{ .Content }}
{{- if and $.IsThinkSet (not $.Think) -}}
/nothink
{{- end -}}
{{- else if eq .Role "assistant" }}<|assistant|>
{{- if (and $.IsThinkSet (and .Thinking (or $last (gt $i $lastUserIdx)))) }}
<think>{{ .Thinking }}</think>
{{- else if $.IsThinkSet }}
<think></think>
{{- end }}
{{- if .Content }}
{{ .Content }}
{{- end -}}
{{ if .ToolCalls }}
<tool_call>
{{- range .ToolCalls }}
{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
{{- end }}
</tool_call>
{{- end }}
{{- else if $curIsTool -}}
{{ if not $prevWasTool }}<|observation|>
{{- end }}
<tool_response>
{{ .Content }}
</tool_response>
{{- $prevWasTool = true -}}
{{- else if eq .Role "system" -}}<|system|>
{{ .Content }}
{{- end }}
{{- if and (ne .Role "assistant") $last }}<|assistant|>
{{- if and $.IsThinkSet (not $.Think) }}
<think></think>
{{- end -}}
{{- end }}
{{- $prevWasTool = $curIsTool -}}
{{- end }}