Most developers use Ollama by executing basic commands like ollama run llama3.1. However, standard models come with general-purpose system instructions, conservative context window limits (often defaulting to 2,048 or 4,096 tokens), and moderate temperature settings that can cause subtle hallucinations during strict coding tasks. The Ollama Modelfile is the configuration blueprint that gives you granular control over model parameters, custom personas, and external GGUF weights.
Key Modelfile Directives
| Directive | Syntax Example | Purpose & Best Practice |
|---|---|---|
| FROM | FROM llama3.1:8b or ./model.gguf |
Specifies the base model layer or raw quantized GGUF weights. |
| SYSTEM | SYSTEM """You are a senior Rust engineer...""" |
Sets immutable base instructions and behavioral constraints. |
| num_ctx | PARAMETER num_ctx 16384 |
Expands context window memory. Critical for multi-file code reviews. |
| temperature | PARAMETER temperature 0.1 |
Controls randomness. Lower values (0.0 - 0.2) ensure deterministic code output. |
| stop | PARAMETER stop "<|eot_id|>" |
Defines custom stop tokens to prevent runaway token generation. |
Complete Modelfile Example: Strict Code Reviewer
FROM qwen2.5-coder:14b
# Set low temperature for strict deterministic code analysis
PARAMETER temperature 0.1
PARAMETER top_p 0.95
PARAMETER num_ctx 16384
# Define system persona
SYSTEM """
You are a Staff Security and Performance Code Reviewer.
When analyzing pull requests:
1. Identify memory safety hazards and concurrency races.
2. Flag missing error handling and resource leaks.
3. Suggest concrete code fixes formatted as unified diffs.
Never output conversational filler. Provide only actionable technical critique.
"""
- VRAM KV-Cache Scaling: Increasing
num_ctxfrom 2,048 to 16,384 tokens increases the GPU VRAM needed for the Key-Value (KV) cache by approximately 1.5 GB to 3 GB depending on model layer count. Ensure your GPU has sufficient VRAM before expanding to 32k+ contexts.
Conclusion
The Modelfile turns local LLMs from generic conversational bots into specialized, domain-tailored developer tools that run entirely offline on your workstation.
- Ollama Official Modelfile Documentation — Complete parameter specifications and syntax.
Discussion & Issues
Comments
Post a Comment