{{- /* If there's a System or Tools, create a system message with multi-turn instructions and "respond concisely." */ -}}
{{- if or .System .Tools }}
{{- if .System }}
{{ .System }}
{{- end }}
{{- if .Tools }}
You are a helpful assistant with tool calling capabilities. You can call functions multiple times if needed. Always respond concisely and use any tool outputs to finalize your response.
{{- end }}
{{- end }}
{{- /* Iterate through the messages. */ -}}
{{- range $i, $msg := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 }}
{{- if eq $msg.Role "user" }}
User:
{{- /* If it's the final user message and Tools exist, show instructions for JSON. */ -}}
{{- if and $.Tools $last }}
Please respond with a JSON call to the most appropriate function.
Format (no variables):
{
"name": "myFunction",
"parameters": {
"argName": "argValue",
...
}
}
{{ $.Tools }}
{{- end }}
{{ $msg.Content }}
{{- /* When the user message ends and it's the last user message, start the assistant. */ -}}
{{- if $last }}
Assistant:
{{- end }}
{{- else if eq $msg.Role "assistant" }}
Assistant:
{{- /* If the assistant has performed tool calls, output them with improved formatting. */ -}}
{{- if .ToolCalls }}
{{- range .ToolCalls }}
{
"name": "{{ .Function.Name }}",
"parameters": {{ .Function.Arguments }}
}
{{- end }}
{{- else }}
{{ .Content }}
{{- end }}
{{- else if eq $msg.Role "tool" }}
{{ $msg.Content }}
{{- /* If the conversation ends after a tool response, we prompt for final assistant output. */ -}}
{{- if $last }}Assistant:{{ end }}
{{- end }}
{{- end }}
{{- else }}
{{- /* If no .Messages, fall back on .System or .Prompt. */ -}}