{{- if eq $msg.Role "user" }}{{- $lastUserIdx = $idx -}}{{- end -}}
{{- end -}}
{{- if .Tools -}}
{{- printf "<|im_start|>system\n" -}}
{{- if .Messages -}}
{{- $first := index .Messages 0 -}}
{{- if eq $first.Role "system" -}}
{{- printf "%s\n\n" $first.Content -}}
{{- end -}}
{{- end -}}
{{- printf "# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" -}}
{{- printf "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" -}}