{{- $lastUserIdx := -1 -}} {{- range $idx, $msg := .Messages -}} {{- if eq $msg.Role "user" }}{{ $lastUserIdx = $idx }}{{ end -}} {{- end }} {{- $effort := "" }} {{- if $.ThinkLevel }} {{- if or (eq $.ThinkLevel "high") (eq $.ThinkLevel "max") }}{{ $effort = "Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer." }} {{- else if eq $.ThinkLevel "low" }}{{ $effort = "Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration." }} {{- end }} {{- end }} {{- if or .System .Tools $effort }}<|im_start|>system {{ if $effort }}{{ $effort }}{{ if or .System .Tools }} {{ end }}{{ end }}{{ if .System }}{{ .System }}{{ if .Tools }} {{ end }}{{ end }} {{- if .Tools }}# Tools You may call one or more functions to assist with the user query. You are provided with function signatures within XML tags: {{- range .Tools }} {"type": "function", "function": {{ json .Function }}} {{- end }} For each function call, return a json object with function name and arguments within XML tags: {"name": , "arguments": } {{- end -}}<|im_end|> {{ end }} {{- $prevIsTool := false }} {{- range $i, $_ := .Messages }} {{- $rest := slice $.Messages $i }} {{- $last := eq (len $rest) 1 -}} {{- $nextIsTool := and (gt (len $rest) 1) (eq (index $rest 1).Role "tool") -}} {{- if eq .Role "user" }}<|im_start|>user {{ .Content }}<|im_end|> {{ else if eq .Role "assistant" }}{{ $calls := .ToolCalls }}{{ $blank := or .Content (not $calls) }}<|im_start|>assistant {{ if $.IsThinkSet -}} {{ .Thinking }} {{ end -}} {{ if and $.IsThinkSet $blank }} {{ end -}} {{ if .Content }}{{ .Content }}{{ if $calls }} {{ end }}{{ end }} {{- if .ToolCalls }} {{- range .ToolCalls }} {"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}} {{- end }} {{- end }}{{ if not $last }}<|im_end|> {{ end }} {{- else if eq .Role "tool" }} {{- if $prevIsTool }} {{ else }}<|im_start|>user {{ end }} {{ .Content }} {{- if not $nextIsTool }}<|im_end|> {{ end }} {{- end }} {{- $prevIsTool = eq .Role "tool" }} {{- if and (ne .Role "assistant") $last }}<|im_start|>assistant {{ if and $.IsThinkSet (not $.Think) -}} {{ else -}} {{ end -}} {{ end }} {{- end }}