[INST]{{- if .System }}
{{ .System }}
{{- end }}
{{- if .Tools }}
<tools>
{{- range .Tools }}
{"type": "function", "function": {{ .Function }}}
{{- end }}
</tools>
For each function call, return only a JSON object wrapped in <tool_call></tool_call>.
No backticks, no extra text. Example:
<tool_call>
{"name": "<function-name>", "arguments": { ... }}
</tool_call>
{{- end }}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1}}
{{- if or (eq .Role "user") (eq .Role "system") }}
<start_of_turn>user
{{ .Content }}<end_of_turn>
{{- if $last }}
<start_of_turn>model
{{- end }}
{{- else if eq .Role "assistant" }}
<start_of_turn>model
{{- if and $.IsThinkSet (and $last .Thinking) }}
<think>
{{ .Thinking }}
</think>
{{- end }}
{{- if .Content }}{{ .Content }}{{ end }}
{{- if .ToolCalls }}<tool_call>
{{- range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
{{- end }}</tool_call>{{- end }}
{{- if not $last }}<end_of_turn>{{- end }}
{{- else if eq .Role "tool" }}
<|User|>
<tool_response>
{{ .Content }}
</tool_response>
{{- end }}
{{- if and $last (ne .Role "assistant") }}
{{- if and $.IsThinkSet (not $.Think) }}
<think>
</think>
{{- end }}
{{- end -}}
{{- end }}[/INST]