05. Modelfiles: Customizing Models

Chapter 5 of 20 · 20 min

A Modelfile is a text file that defines how Ollama creates a model variant. You can set parameters, add system prompts, configure generation behavior, and create reusable model configurations without modifying the base weights.

Modelfile Syntax

The Modelfile format consists of instructions like FROM, PARAMETER, TEMPLATE, and SYSTEM. The FROM instruction is required-it specifies the base model.

Create a file named Modelfile (no extension):

FROM llama3.2:1b

PARAMETER temperature 0.7
PARAMETER top_p 0.9
PARAMETER num_ctx 4096

SYSTEM """
You are a helpful assistant that responds in short, clear sentences.
Never use jargon. Always provide concrete examples.
"""

TEMPLATE """
{{ if .System }}
<|start_header_id|>system<|end_header_id|>
{{ .System }}<|eot_id|>
{{ end }}
<|start_header_id|>user<|end_header_id|>
{{ .Prompt }}<|eot_id|>
<|start_header_id|>assistant<|end_header_id|>
"""

Creating and Using a Custom Model

ollama create helpful-llama -f ./Modelfile
ollama run helpful-llama

The ollama create command reads the Modelfile, validates the base model exists, and registers the custom variant. The variant stores only the Modelfile configuration-not a copy of the base model. Changing the base model later affects your custom model.

Parameter Instructions

Common parameters you can set in Modelfiles:

Instruction Example Effect
PARAMETER temperature 0.7 Controls randomness. 0 = deterministic, 2 = very random
PARAMETER top_p 0.9 Nucleus sampling threshold
PARAMETER num_ctx 4096 Context window size in tokens
PARAMETER num_gpu 1 Number of GPU layers to load
PARAMETER stop "STOP" Sequence that stops generation

System Prompt via SYSTEM

The SYSTEM instruction sets a persistent system prompt. Unlike passing a system message at runtime, the SYSTEM instruction is baked into the model variant. This means every interaction with your custom model includes the system prompt automatically.

Dynamic Parameters at Runtime

You can override Modelfile parameters at runtime:

ollama run helpful-llama --param temperature 1.2

Or via the API:

curl http://localhost:11434/api/generate -d '{
  "model": "helpful-llama",
  "prompt": "Explain quantum computing",
  "options": {
    "temperature": 1.2
  }
}'

Runtime parameters override the Modelfile defaults but do not change the stored configuration.

EXERCISE

Create a Modelfile for llama3.2:1b that sets temperature to 0.3 and includes a system prompt telling the model to respond like a pirate. Create the model variant and test it with ollama run.