32 1 week ago

A Quantized, Fine-Tuned Model for Enhanced Tool Calling, Code Generation, and Reasoning Edit

vision tools thinking
640c2b784403 ยท 8.7kB
{{- define "upper_type" -}}
{{- if eq . "string" -}}STRING
{{- else if eq . "object" -}}OBJECT
{{- else if eq . "array" -}}ARRAY
{{- else if eq . "number" -}}NUMBER
{{- else if eq . "integer" -}}INTEGER
{{- else if eq . "boolean" -}}BOOLEAN
{{- else if eq . "null" -}}NULL
{{- else -}}{{ . }}{{- end -}}
{{- end -}}
{{- define "type" -}}
{{- if eq (printf "%T" .) "string" -}}
<|"|>{{ template "upper_type" . }}<|"|>
{{- else if eq (len .) 1 -}}
<|"|>{{ template "upper_type" (index . 0) }}<|"|>
{{- else -}}
[{{ range $i, $v := . }}{{ if $i }},{{ end }}<|"|>{{ template "upper_type" $v }}<|"|>{{ end }}]
{{- end -}}
{{- end -}}
{{- define "value" -}}
{{- $kind := printf "%T" . -}}
{{- if eq $kind "string" -}}
<|"|>{{ . }}<|"|>
{{- else if eq $kind "<nil>" -}}
null
{{- else if eq (printf "%.4s" $kind) "map[" -}}
{ {{- $first := true -}}{{- range $key, $value := . -}}{{ if not $first }},{{ end }}{{ $first = false }}{{ $key }}:{{ template "value" $value }}{{- end -}}}
{{- else if eq (printf "%.2s" $kind) "[]" -}}
[{{ range $i, $value := . }}{{ if $i }},{{ end }}{{ template "value" $value }}{{ end }}]
{{- else -}}
{{ . }}
{{- end -}}
{{- end -}}
{{- define "schema_value" -}}
{{- $kind := printf "%T" . -}}
{{- if eq (printf "%.4s" $kind) "map[" -}}
{ {{- $first := true -}}{{- range $key, $value := . -}}{{ if not $first }},{{ end }}{{ $first = false }}<|"|>{{ $key }}<|"|>:{{ template "schema_value" $value }}{{- end -}}}
{{- else if eq (printf "%.2s" $kind) "[]" -}}
[{{ range $i, $value := . }}{{ if $i }},{{ end }}{{ template "schema_value" $value }}{{ end }}]
{{- else -}}
{{ template "value" . }}
{{- end -}}
{{- end -}}
{{- define "properties" -}}
{{- $first := true -}}
{{- range $name, $property := . -}}
{{- if and (ne $name "description") (ne $name "type") (ne $name "properties") (ne $name "required") (ne $name "nullable") -}}
{{- if not $first }},{{ end }}{{ $first = false -}}
{{ $name }}:{ {{- template "property" $property -}}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- define "items" -}}
{{- $first := true -}}
{{- range $key, $value := . -}}
{{- if ne (printf "%T" $value) "<nil>" -}}
{{- if not $first }},{{ end }}{{ $first = false -}}
{{ $key }}:
{{- if eq $key "properties" -}}
{ {{- template "properties" $value -}}}
{{- else if eq $key "type" -}}
{{ template "type" $value }}
{{- else -}}
{{ template "schema_value" $value }}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- define "property" -}}
{{- $description := "" -}}{{- $type := "" -}}{{- $enum := false -}}{{- $items := false -}}
{{- $properties := false -}}{{- $required := false -}}{{- $nullable := false -}}
{{- if eq (printf "%T" .) "api.ToolProperty" -}}
{{- $description = .Description -}}{{- $type = .Type -}}{{- $enum = .Enum -}}{{- $items = .Items -}}
{{- $properties = .Properties.ToMap -}}{{- $required = .Required -}}{{- $nullable = .Extra.nullable -}}
{{- else -}}
{{- $description = .description -}}{{- $type = .type -}}{{- $enum = .enum -}}{{- $items = .items -}}
{{- $properties = .properties -}}{{- $required = .required -}}{{- $nullable = .nullable -}}
{{- end -}}
{{- $name := "" -}}
{{- if eq (printf "%T" $type) "string" -}}{{- $name = $type -}}
{{- else if $type -}}{{- $name = index $type 0 -}}{{- end -}}
{{- $comma := false -}}
{{- if $description -}}description:<|"|>{{ $description }}<|"|>{{ $comma = true }}{{- end -}}
{{- if and (or (eq $name "string") (eq $name "STRING")) $enum -}}
{{ if $comma }},{{ end }}enum:{{ template "schema_value" $enum }}{{ $comma = true }}
{{- else if and (or (eq $name "array") (eq $name "ARRAY")) $items -}}
{{ if $comma }},{{ end }}items:{ {{- template "items" $items -}}}{{ $comma = true }}
{{- end -}}
{{- if $nullable -}}{{ if $comma }},{{ end }}nullable:true{{ $comma = true }}{{- end -}}
{{- if or (eq $name "object") (eq $name "OBJECT") -}}
{{ if $comma }},{{ end }}properties:{ {{- if $properties }}{{ template "properties" $properties }}{{ end -}}}{{ $comma = true }}
{{- if $required -}},required:{{ template "value" $required }}{{- end -}}
{{- end -}}
{{- if $type -}}{{ if $comma }},{{ end }}type:{{ template "type" $type }}{{- end -}}
{{- end -}}
{{- define "trim" -}}
{{- $text := . -}}
{{- range len $text -}}
{{- if not $text }}{{ break }}{{ end -}}
{{- $c := slice $text 0 1 -}}
{{- if or (eq $c " ") (eq $c "\n") (eq $c "\r") (eq $c "\t") -}}
{{- $text = slice $text 1 -}}
{{- else -}}{{ break }}{{- end -}}
{{- end -}}
{{- $last := 0 -}}
{{- range $i := len $text -}}
{{- $c := printf "%.1s" (slice $text $i) -}}
{{- if and (ne $c " ") (ne $c "\n") (ne $c "\r") (ne $c "\t") -}}{{- $last = $i -}}{{- end -}}
{{- end -}}
{{- range $i := len $text -}}{{- if le $i $last -}}{{ slice (slice $text $i) 0 1 }}{{- end -}}{{- end -}}
{{- end -}}
{{- define "content" -}}
{{- $text := . -}}{{- $clean := "" -}}{{- $thinking := false -}}
{{- range len $text -}}
{{- if not $text }}{{ break }}{{ end -}}
{{- if eq (printf "%.10s" $text) "<|channel>" -}}
{{- $thinking = true -}}{{- $text = slice $text 10 -}}
{{- else if eq (printf "%.10s" $text) "<channel|>" -}}
{{- $thinking = false -}}{{- $text = slice $text 10 -}}
{{- else -}}
{{- if not $thinking -}}{{- $clean = printf "%s%s" $clean (slice $text 0 1) -}}{{- end -}}
{{- $text = slice $text 1 -}}
{{- end -}}
{{- end -}}
{{- template "trim" $clean -}}
{{- end -}}
{{- $lastUser := -1 -}}
{{- range $i, $message := .Messages -}}{{- if eq $message.Role "user" -}}{{- $lastUser = $i -}}{{- end -}}{{- end -}}
{{- $hasSystem := false -}}{{- $system := .System -}}
{{- range $i, $message := .Messages -}}
{{- if eq $message.Role "system" -}}{{- $hasSystem = true -}}
{{- else if and (eq $i 0) (eq $message.Role "developer") -}}{{- $hasSystem = true -}}{{- $system = $message.Content -}}
{{- end -}}{{- end -}}
<bos>
{{- if or $hasSystem .Tools .Think -}}
<|turn>system
{{ if .Think }}<|think|>
{{ end -}}
{{- if $system }}{{ template "trim" $system }}{{ end -}}
{{- range .Tools -}}
<|tool>declaration:{{ .Function.Name }}{description:<|"|>{{ .Function.Description }}<|"|>
{{- $params := .Function.Parameters -}}
{{- if or $params.Properties $params.Type $params.Required -}}
,parameters:{
{{- if $params.Properties -}}properties:{ {{- template "properties" $params.Properties -}}},{{- end -}}
{{- if $params.Required -}}required:{{ template "value" $params.Required }},{{- end -}}
type:{{ template "type" $params.Type }}}
{{- end -}}
}<tool|>
{{- end -}}
<turn|>
{{ end -}}
{{- $previousRole := "" -}}{{- $previousType := "" -}}
{{- range $i, $message := .Messages -}}
{{- if and (ne $message.Role "tool") (ne $message.Role "system") (not (and (eq $i 0) (eq $message.Role "developer"))) -}}
{{- $previousType = "" -}}
{{- if not (and (eq $message.Role "assistant") (eq $previousRole "assistant")) -}}
<|turn>{{ if eq $message.Role "assistant" }}model{{ else if eq $message.Role "developer" }}system{{ else }}{{ $message.Role }}{{ end }}
{{ end -}}
{{- if and $message.Thinking (gt $i $lastUser) -}}
<|channel>thought
{{ $message.Thinking }}
<channel|>
{{- end -}}
{{- range $message.ToolCalls -}}
<|tool_call>call:{{ .Function.Name }}{ {{- $first := true -}}
{{- range $key, $value := .Function.Arguments -}}{{ if not $first }},{{ end }}{{ $first = false }}{{ $key }}:{{ template "value" $value }}{{- end -}}
}<tool_call|>
{{- $previousType = "tool_call" -}}
{{- end -}}
{{- $responses := false -}}
{{- $nextRole := "" -}}{{- $scanning := true -}}
{{- range $j, $follow := $.Messages -}}
{{- if and (gt $j $i) $scanning -}}
{{- if ne $follow.Role "tool" -}}
{{- $nextRole = $follow.Role -}}{{- $scanning = false -}}
{{- else if $message.ToolCalls -}}
{{- $name := or $follow.ToolName "unknown" -}}
{{- range $message.ToolCalls -}}{{- if and $follow.ToolCallID (eq .ID $follow.ToolCallID) -}}{{- $name = .Function.Name -}}{{- end -}}{{- end -}}
<|tool_response>response:{{ $name }}{value:{{ template "value" $follow.Content }}}<tool_response|>
{{- $responses = true -}}{{- $previousType = "tool_response" -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if eq $message.Role "assistant" -}}
{{ template "content" $message.Content }}
{{- else -}}
{{ template "trim" $message.Content }}
{{- end -}}
{{- $continues := and (eq $message.Role "assistant") (eq $nextRole "assistant") (or (not $message.ToolCalls) $responses) -}}
{{- if and (eq $previousType "tool_call") (not $responses) -}}
<|tool_response>
{{- else if and (not $continues) (not (and $responses (not $message.Content) (not $nextRole))) -}}
<turn|>
{{ end -}}
{{- $previousRole = $message.Role -}}
{{- end -}}
{{- end -}}
{{- if and (ne $previousType "tool_response") (ne $previousType "tool_call") -}}
<|turn>model
{{ if not .Think }}<|channel>thought
<channel|>{{ end -}}
{{- else if and (eq $previousType "tool_response") .Think -}}
<|channel>thought
{{ end -}}