27 4 days ago

Ternary Bonsai: 1.58-bit Language Models at 8B, 4B, and 1.7B Scale.

tools thinking
68201b185f80 · 2.9kB
{{- $lastUserIdx := -1 -}}
{{- range $idx, $msg := .Messages -}}
{{- 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>" -}}
{{- range .Tools -}}
{{- printf "\n{\"type\": \"function\", \"function\": {\"name\": %q" .Function.Name -}}
{{- if .Function.Description -}}
{{- printf ", \"description\": %q" .Function.Description -}}
{{- end -}}
{{- printf ", \"parameters\": {\"type\": %q" .Function.Parameters.Type -}}
{{- if .Function.Parameters.Required -}}
{{- printf ", \"required\": [" -}}
{{- range $ri, $required := .Function.Parameters.Required -}}
{{- if $ri }}{{- printf ", " -}}{{- end -}}
{{- printf "%q" $required -}}
{{- end -}}
{{- printf "]" -}}
{{- end -}}
{{- printf ", \"properties\": %s}}}" .Function.Parameters.Properties -}}
{{- printf "}" -}}
{{- end -}}
{{- 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" -}}
{{- else -}}
{{- if .Messages -}}
{{- $first := index .Messages 0 -}}
{{- if eq $first.Role "system" -}}
{{- printf "<|im_start|>system\n%s<|im_end|>\n" $first.Content -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- range $i, $msg := .Messages -}}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
{{- if and (eq $msg.Role "system") (eq $i 0) -}}
{{- else if or (eq $msg.Role "user") (eq $msg.Role "system") -}}
{{- printf "<|im_start|>%s\n%s<|im_end|>\n" $msg.Role $msg.Content -}}
{{- else if eq $msg.Role "assistant" -}}
{{- printf "<|im_start|>assistant\n" -}}
{{- if gt $i $lastUserIdx -}}
{{- if or $last $msg.Thinking -}}
{{- printf "<think>\n%s\n</think>\n\n%s" $msg.Thinking $msg.Content -}}
{{- else -}}
{{- printf "%s" $msg.Content -}}
{{- end -}}
{{- else -}}
{{- printf "%s" $msg.Content -}}
{{- end -}}
{{- if $msg.ToolCalls -}}
{{- range $j, $call := $msg.ToolCalls -}}
{{- if or (and (eq $j 0) $msg.Content) (gt $j 0) -}}
{{- printf "\n" -}}
{{- end -}}
{{- printf "<tool_call>\n{\"name\": %q, \"arguments\": %s}\n</tool_call>" $call.Function.Name $call.Function.Arguments -}}
{{- end -}}
{{- end -}}
{{- printf "<|im_end|>\n" -}}
{{- else if eq $msg.Role "tool" -}}
{{- printf "<|im_start|>user\n<tool_response>\n%s\n</tool_response><|im_end|>\n" $msg.Content -}}
{{- end -}}
{{- if and (ne $msg.Role "assistant") $last -}}
{{- printf "<|im_start|>assistant\n<think>\n\n</think>\n\n" -}}
{{- end -}}
{{- end -}}