Open-Source Wikis

/

Ollama

/

Features

/

Model creation

ollama/ollama

Model creation

Building a new model from a Modelfile or a folder of safetensors. Spans parser/parser.go, server/create.go, server/routes.go (CreateHandler, CreateBlobHandler, HeadBlobHandler), and x/create/ for the experimental safetensors flow.

Purpose

Let users define a model by pointing at base weights and adding a template, system prompt, license, and parameters — without writing OCI manifests. The flow uploads required blobs, persists a manifest, and stores the model under ~/.ollama/models/.

Modelfile flow

Modelfile  -->  parser.ParseFile  -->  CLI: upload blobs (POST /api/blobs/:digest)
                                  -->  CLI: send CreateRequest (POST /api/create)
                                  -->  Server: build manifest under manifests/

Modelfile directives:

  • FROM <name|path> — base weights or another Ollama model.
  • ADAPTER <path> — LoRA / adapter weights.
  • MESSAGE <role> <content> — seed conversation messages.
  • TEMPLATE """...""" — chat template.
  • SYSTEM """...""" — default system prompt.
  • PARAMETER <key> <value> — runtime parameters; deprecated ones (e.g., mirostat, low_vram, vocab_only) are silently dropped (see deprecatedParameters in parser/parser.go).
  • LICENSE """...""" — license text.

Modelfile.CreateRequest (parser/parser.go) walks each command and either inlines the value, computes a sha256 of the referenced file (fileDigestMap), or merges parameters into a map.

Server side

Server.CreateHandler (server/create.go) accepts the request, validates the model name (model.ParseName), confirms every referenced blob is present (the CLI uploaded them via POST /api/blobs/:digest first), assembles the manifest, optionally re-quantizes (server/quantization.go), and writes it to ~/.ollama/models/manifests/.

Server.CreateBlobHandler and HeadBlobHandler are the local equivalents of OCI HEAD/PUT. The CLI HEADs a blob digest before uploading; the daemon returns 200 if the blob is already on disk so the upload is skipped.

Experimental safetensors path

ollama create --experimental (in cmd/cmd.go) routes through x/create/client/ instead of the legacy create handler. Steps:

  1. CLI parses the Modelfile (or defaults to FROM .).
  2. xcreateclient.ConfigFromModelfile constructs an xcreateclient.Config and identifies a model directory containing safetensors.
  3. The CLI ships the safetensors files to the daemon and the daemon builds GGUF on the fly via convert/.
  4. Resulting GGUF blob is stored as usual.

This path only runs when the daemon is local (isLocalhost() in cmd/cmd.go) — sending raw safetensors over the network was deemed out of scope.

Quantization

server/quantization.go knows how to re-quantize a model on creation. The CLI passes --quantize (e.g., q4_K_M, q8_0) and the server applies it during the GGUF write. Tests in server/quantization_test.go and server/laguna_quantization_test.go cover the supported types.

Integration points

Entry points for modification

  • New Modelfile directive → extend Modelfile.CreateRequest and the parser in parser/parser.go.
  • New quantization type → add it under server/quantization.go and ensure the runner backends (llamarunner, ollamarunner) support reading it.
  • Different non-Modelfile creation source → extend x/create/.

Key source files

File Purpose
parser/parser.go Modelfile parser, Modelfile.CreateRequest.
server/create.go Server-side creation pipeline.
server/routes.go CreateHandler, CreateBlobHandler, HeadBlobHandler.
server/quantization.go On-create quantization.
x/create/ Experimental safetensors creation flow.
docs/modelfile.mdx User-facing Modelfile reference.

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

Model creation – Ollama wiki | Factory