Open-Source Wikis

/

Ollama

/

Primitives

/

Modelfile

ollama/ollama

Modelfile

The Modelfile is a small DSL describing a model: which base weights to use, which template, which parameters, license text, and optional adapter or projector files. The parser is in parser/parser.go and the user-facing reference is docs/modelfile.mdx.

Purpose

Make it possible to assemble a usable Ollama model without writing OCI manifests by hand. The DSL has stayed source-compatible since 2023, with deprecated parameters silently dropped rather than errored on.

Directives

FROM <model|path>          # required; base weights or another Ollama model
ADAPTER <path>             # LoRA / adapter weights
TEMPLATE """<go template>"""
SYSTEM """<system prompt>"""
PARAMETER <key> <value>    # runtime parameters
MESSAGE <role> <content>   # seed conversation
LICENSE """<text>"""

Multi-line values use Go-style raw string """...""" blocks.

Parser

parser.ParseFile(io.Reader) (*Modelfile, error) returns a Modelfile{Commands []Command}. Modelfile.CreateRequest(relativeDir) walks the commands and produces an *api.CreateRequest:

  • FROM <path> is hashed and added to req.Files. If path doesn't exist on disk, the value is treated as another model name (req.From).
  • ADAPTER <path> is hashed and added to req.Adapters.
  • TEMPLATE, SYSTEM, LICENSE, MESSAGE populate the obvious fields.
  • PARAMETER <key> <value> is collected into a map, with deprecated keys filtered out.

The deprecated list is a literal in parser/parser.go:

var deprecatedParameters = []string{
    "penalize_newline", "low_vram", "f16_kv", "logits_all",
    "vocab_only", "use_mlock", "mirostat", "mirostat_tau", "mirostat_eta",
}

File hashing

fileDigestMap(path) walks files and produces {relpath: sha256-...}. The CLI uses this map to upload only blobs the daemon doesn't already have, via POST /api/blobs/:digest and HEAD /api/blobs/:digest (server/routes.go).

Path expansion

expandPath (parser/parser.go) handles ~, environment variables, and relative paths against the Modelfile's own directory. This is what makes FROM . work when run from a model checkpoint directory.

Example

FROM ./gemma3.gguf
TEMPLATE """{{ if .System }}{{ .System }}

{{ end }}{{ range .Messages }}<|user|>{{ .Content }}{{ end }}"""
SYSTEM "You are a helpful assistant."
PARAMETER num_ctx 8192
PARAMETER temperature 0.7
LICENSE "Gemma Terms of Use"

ollama create my-model -f Modelfile will:

  1. Hash gemma3.gguf and the (inline) template/system/license values.
  2. Upload missing blobs through POST /api/blobs/:digest.
  3. POST a CreateRequest to /api/create.
  4. The daemon assembles the manifest and stores the model under ~/.ollama/models/manifests/.

Integration points

Entry points for modification

  • New directive → extend the lexer and Modelfile.CreateRequest in parser/parser.go, update docs/modelfile.mdx.
  • Deprecate a parameter → add it to deprecatedParameters and document it in the docs.

Key source files

File Purpose
parser/parser.go Modelfile parser and CreateRequest builder.
parser/parser_test.go Behavior tests covering each directive.
docs/modelfile.mdx User-facing reference.

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

Modelfile – Ollama wiki | Factory