Sub topic 13
Writing a Modelfile
This chapter assumes you can use a terminal, have Ollama installed, and have pulled a model such as llama3.2. It also assumes you know what a context window and sampling temperature are, from the earlier chapter on parameters.
What a Modelfile is
A Modelfile is a plain text recipe that tells Ollama how to build a local model. It does not contain weights. It points to a base model and adds settings on top. Ollama stores the result as a new named model, and the base weights are reused rather than copied.
Use a Modelfile when you keep retyping the same system prompt or parameters. Writing them once makes the behavior repeatable.
A minimal example
FROM llama3.2
PARAMETER temperature 0.3
PARAMETER num_ctx 4096
SYSTEM You are a concise assistant that answers in at most three sentences.Each line starts with an instruction in capital letters. The file is usually named Modelfile, with no extension.
The core instructions
| Instruction | Purpose | Required |
|---|---|---|
| `FROM` | Base model name with optional tag, or a path to GGUF weights | Yes |
| `PARAMETER` | Sets a generation or runtime option | No |
| `SYSTEM` | Sets the system prompt | No |
| `TEMPLATE` | Overrides the prompt template | No |
| `MESSAGE` | Adds example conversation turns | No |
FROM
FROM must come first. It names the starting point. Use a tag such as llama3.2:3b to pin an exact variant, because a bare name resolves to the default tag, which can change over time.
PARAMETER
Each PARAMETER line sets one option, with the name and value separated by a space. Common ones are:
temperature: lower values give more predictable output.num_ctx: the context window size in tokens. A larger value uses more memory.top_pandtop_k: limit which tokens the model samples from.stop: a string that ends generation when the model produces it.
You can repeat stop to define several stop strings.
SYSTEM
SYSTEM sets the instruction the model sees before every conversation. For text that spans several lines, wrap it in triple quotes:
SYSTEM """
You are a code reviewer.
Point out bugs first, then style issues.
"""TEMPLATE
Most library models already carry the right template. Leave TEMPLATE alone unless you import raw weights or know the base model's chat format. A wrong template is a common cause of strange or rambling output.
Building and running it
ollama create concise-assistant -f Modelfile
ollama run concise-assistantcreate registers the model under the name you give it. After that, ollama list shows it, and ollama show concise-assistant --modelfile prints the Modelfile Ollama stored. To change anything, edit the file and run create again with the same name.
How the pieces fit together
Common errors
- Misspelled base name:
createfails if theFROMmodel is not local and cannot be pulled. Runollama listto check the name. - Quotes around single-line values:
PARAMETER temperature "0.3"is unnecessary. Write the value bare. - Raising
num_ctxtoo far: a very large window can exhaust memory and slow responses.
Exercise
You want a model based on llama3.2 that always replies in formal English and uses a temperature of 0.2. Which two Modelfile instructions do you need besides FROM, and which command builds it as formal-bot?
Show answer
Use PARAMETER temperature 0.2 and a SYSTEM line describing the formal style. Build it with ollama create formal-bot -f Modelfile.