79 4 months ago

tools
7940630548b1 · 2.0kB
{{- /* 1. OPEN SYSTEM BLOCK */ -}}
{{- if or .System .Tools }}<|im_start|>system
{{- /* 2. STATIC SECTION: TOOLS & GUIDELINES
Render this FIRST so it becomes the "Common Prefix" for KV Cache. */ -}}
{{- if .Tools }}
# Tools
You are provided with function signatures within <tools></tools> XML tags:
<tools>
{{- range .Tools }}
{"type": "function", "function": {{ .Function }}}
{{- end }}
</tools>
# Tool Call Guidelines
1. **Format:** Use strictly valid JSON inside <tool_call> tags.
2. **Arguments:** Only use arguments defined in the function signature. Do not invent new arguments.
3. **JSON:** Ensure all strings are double-quoted and special characters are escaped. No trailing commas.
# Example
User: "What is the weather in Seoul?"
<tool_call>
{"name": "get_weather", "arguments": {"location": "Seoul", "unit": "celsius"}}
</tool_call>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
{{ end }}
{{- /* 3. DYNAMIC SECTION: SYSTEM PROMPT
Render this LAST. This breaks the cache, but since it is at the end,
the Tools block above remains cached. */ -}}
{{- if .System }}
# Context & Instructions
{{ .System }}
{{- end }}<|im_end|>
{{ end }}
{{- /* 4. MESSAGE HISTORY */ -}}
{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
{{- if eq .Role "user" }}<|im_start|>user
{{ .Content }}<|im_end|>
{{ else if eq .Role "assistant" }}<|im_start|>assistant
{{ if .Content }}{{ .Content }}
{{- else if .ToolCalls }}<tool_call>
{{ range .ToolCalls }}{"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
{{ end }}</tool_call>
{{- end }}{{ if not $last }}<|im_end|>
{{ end }}
{{- else if eq .Role "tool" }}<|im_start|>user
<tool_response>
{{ .Content }}
</tool_response><|im_end|>
{{ end }}
{{- /* 5. PRE-FILL ASSISTANT TAG */ -}}
{{- if and (ne .Role "assistant") $last }}<|im_start|>assistant
{{ end }}
{{- end }}