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 toreq.Files. Ifpathdoesn't exist on disk, the value is treated as another model name (req.From).ADAPTER <path>is hashed and added toreq.Adapters.TEMPLATE,SYSTEM,LICENSE,MESSAGEpopulate 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:
- Hash
gemma3.ggufand the (inline) template/system/license values. - Upload missing blobs through
POST /api/blobs/:digest. - POST a
CreateRequestto/api/create. - The daemon assembles the manifest and stores the model under
~/.ollama/models/manifests/.
Integration points
- The CLI handler is
cmd.CreateHandler(cmd/cmd.go). - The server handler is
Server.CreateHandler(server/create.go,server/routes.go). - The experimental safetensors flow uses
x/create/client/but reuses the Modelfile parser to read directives.
Entry points for modification
- New directive → extend the lexer and
Modelfile.CreateRequestinparser/parser.go, updatedocs/modelfile.mdx. - Deprecate a parameter → add it to
deprecatedParametersand 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.