Go template for Ollama

#3
by aoleb - opened

{{- /* ---------------------------------------------------------------------- / -}}
{{- /
Default setup and flags / -}}
{{- /
---------------------------------------------------------------------- */ -}}
{{- $messages := .Messages -}}
{{- $tools := .Tools -}}
{{- $add_generation_prompt := true -}}
{{- $add_thoughts := false -}}
{{- $bos_token := "" -}}
{{- if .BosToken }}{{ $bos_token = .BosToken }}{{ end -}}

{{- $reasoning_prompt := "You are a thoughtful, systematic AI assistant from ServiceNow Language Models (SLAM) lab. Analyze each question carefully, present your reasoning step-by-step, then provide the final response after the marker [BEGIN FINAL RESPONSE]." -}}
{{- $reasoning_asst_turn_start := "Here are my reasoning steps:\n" -}}

{{- /* Tool list setup */ -}}
{{- $available_tool_string := "" -}}
{{- if $tools -}}
{{- $available_tool_string = "\nYou are provided with function signatures within XML tags. You may call one or more functions to assist with the user query. Here are the available tools:\n" -}}
{{- range $tools -}}
{{- $available_tool_string = printf "%s%v" $available_tool_string . -}}
{{- end -}}
{{- $available_tool_string = printf "%s\n.\n\nReturn all function calls as a list of JSON objects within XML tags." $available_tool_string -}}
{{- end -}}

{{- /* Start system block if first message is not system */ -}}
{{- if $messages -}}
{{- $firstMsg := index $messages 0 -}}
{{- if ne $firstMsg.Role "system" -}}
{{- $bos_token }}<|begin_system|>
{{ $reasoning_prompt }}
{{- if $available_tool_string }}{{ $available_tool_string }}{{ end }}
{{- end -}}
{{- end -}}

{{- /* Iterate through messages */ -}}
{{- range $index, $message := .Messages -}}

{{- if eq .Role "user" -}}
    <|begin_user|>

{{ .Content }}

{{- else if eq .Role "system" -}}
    {{- $bos_token }}<|begin_system|>

{{ $reasoning_prompt }}
{{ .Content }}
{{- if $available_tool_string }}{{ $available_tool_string }}{{ end }}

{{- else if eq .Role "assistant" -}}
    <|begin_assistant|>
    {{- if .Thought }}<thinking>{{ .Thought }}</thinking>{{ end -}}
    {{- .Content -}}

    {{- if .ToolCalls -}}
        <tool_calls>[
        {{- range .ToolCalls -}}
            {"name": "{{ .Function.Name }}", "arguments": {{ .Function.Arguments }}}
        {{- end -}}
        ]</tool_calls>
    {{- end -}}
    <|end|>

{{- else if eq .Role "tool" -}}
    <|begin_tool_result|>

{{ .Content }}

{{- end -}}

{{- end -}}

{{- /* Handle Generation Prompt at the very end */ -}}
{{- if $add_generation_prompt -}}
<|begin_assistant|>
{{ $reasoning_asst_turn_start -}}
{{- end -}}

Sign up or log in to comment