Open-Source Wikis

/

Ollama

/

Systems

/

Template

ollama/ollama

Template

The template/ package wraps Go's text/template with chat-specific helpers. Every model carries a template that turns a list of messages into the prompt string the runner consumes.

Purpose

Render a chat conversation into a model-specific prompt without each handler having to reimplement Llama-style or ChatML-style formatting. Templates are stored as model blobs and fetched as part of the manifest, so they ship with the model.

Key abstractions

Symbol Location Purpose
Template template/template.go Wraps a parsed *text/template.Template plus the source string.
Parse template/template.go Parse a template source string with the chat-specific function map.
Helper functions template/template.go eq, ne, slice, gt, lt, len, JSON helpers, conditional helpers used by stock templates.

Stock templates

Common templates ship with model definitions in the registry: ChatML, Llama 2, Llama 3, Gemma, Mistral instruct, Phi, Qwen, etc. They live as blobs and are visible via ollama show <model> --template.

How it works

graph LR
    Messages[api.Messages]
    System[system prompt]
    Template[template.Template]
    Render[Render]
    Prompt[final prompt string]
    Messages --> Render
    System --> Render
    Template --> Render
    Render --> Prompt

Render takes the messages plus options (system prompt, response prefix, available tools) and returns the prompt string. Tool definitions are JSON-encoded and embedded by templates that support tool calls.

Integration points

  • The daemon calls Render from the prompt-assembly path in server/prompt.go.
  • Models that need richer formatting (e.g., harmony, GPT-OSS) bypass template.Render and use a renderer in model/renderers/ — see model engine.
  • The Template.Contains helper is used in server/routes.go to detect harmony-format models (shouldUseHarmony).

Entry points for modification

  • New helper function → add it to the function map in template.go.
  • Model-specific prompt formats that the template engine cannot express → use a renderer instead.

Key source files

File Purpose
template/template.go Template wrapper.
template/template_test.go Golden tests for stock templates.
server/prompt.go Caller for /api/generate.
model/renderers/ Per-model renderers used when templates aren't enough.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

Template – Ollama wiki | Factory