{{- if (or .Tools .System) }}System {{ if .System }}{{ .System }} {{ end }} {{- if .Tools }} {{- range .Tools }} {{ . }} {{ end }} {{ end }} {{- end }} {{- range $i, $m := .Messages }} {{- $last := eq (len (slice $.Messages $i)) 1 -}} {{- if eq .Role "user" }}User {{ .Content }} {{- if $last }} Assistant {{- end }} {{ else if eq .Role "tool" }}Tool {{ .Content }} {{- if $last }} Assistant {{- end }} {{ else if eq .Role "assistant" }}Assistant {{- if .ToolCalls }} {{ range .ToolCalls }} {"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}} {{ end }} {{ else }} {{ .Content }} {{- if not $last }} {{ end }} {{- end }} {{- end }} {{- end }}