Ollama Fundamentals
Model Customization
3 min read
Ollama's Modelfile lets you create custom model configurations with specific system prompts, parameters, and behaviors. Think of it as a Dockerfile for LLMs.
What is a Modelfile?
┌─────────────────────────────────────────────────────────────────┐
│ Modelfile │
├─────────────────────────────────────────────────────────────────┤
│ │
│ A text file that defines: │
│ │
│ • Base model to start from │
│ • System prompt (personality/instructions) │
│ • Runtime parameters (temperature, context, etc.) │
│ • Custom template format │
│ │
│ Result: A new model variant tailored to your use case │
│ │
└─────────────────────────────────────────────────────────────────┘
Creating Your First Custom Model
Step 1: Create a Modelfile
# Create a file named "Modelfile" (no extension)
cat > Modelfile << 'EOF'
FROM llama3.2
SYSTEM """
You are a helpful coding assistant specialized in Python.
You always:
- Write clean, well-documented code
- Include type hints
- Add docstrings to functions
- Suggest improvements when reviewing code
Keep responses concise and focused on code.
"""
PARAMETER temperature 0.3
PARAMETER top_p 0.9
EOF
Step 2: Build the Model
# Create the custom model
ollama create python-coder -f Modelfile
# Output:
# transferring model data
# using existing layer sha256:...
# creating new layer sha256:...
# writing manifest
# success
# Verify it was created
ollama list | grep python-coder
Step 3: Use Your Custom Model
ollama run python-coder "Write a function to validate email addresses"
Modelfile Syntax Reference
FROM (Required)
# Use a base model from Ollama library
FROM llama3.2
# Use a specific version/size
FROM llama3.2:70b
# Use another custom model as base
FROM my-other-model
SYSTEM (System Prompt)
# Single line
SYSTEM "You are a helpful assistant."
# Multi-line with triple quotes
SYSTEM """
You are a senior software engineer.
You write production-quality code.
You explain your reasoning step by step.
"""
PARAMETER (Runtime Settings)
# Generation parameters
PARAMETER temperature 0.7
PARAMETER top_p 0.9
PARAMETER top_k 40
PARAMETER repeat_penalty 1.1
# Context and memory
PARAMETER num_ctx 4096
PARAMETER num_keep 24
# Stop tokens
PARAMETER stop "<|endoftext|>"
PARAMETER stop "User:"
TEMPLATE (Prompt Format)
# Custom chat template
TEMPLATE """
{{ if .System }}<|system|>
{{ .System }}<|end|>
{{ end }}{{ if .Prompt }}<|user|>
{{ .Prompt }}<|end|>
{{ end }}<|assistant|>
{{ .Response }}<|end|>
"""
Practical Examples
Example 1: SQL Query Generator
FROM llama3.2
SYSTEM """
You are a SQL expert. When given a description of data needs:
1. Write the SQL query
2. Explain what it does
3. Suggest indexes if relevant
Database type: PostgreSQL
Output format: SQL code block followed by explanation.
"""
PARAMETER temperature 0.2
PARAMETER num_ctx 4096
ollama create sql-expert -f Modelfile
ollama run sql-expert "Get all users who signed up last month and made at least 3 purchases"
Example 2: Code Reviewer
FROM llama3.2:70b
SYSTEM """
You are a code review assistant. For each code snippet:
1. Security: Check for vulnerabilities (injection, XSS, etc.)
2. Performance: Identify bottlenecks and inefficiencies
3. Maintainability: Suggest cleaner patterns
4. Bugs: Find potential runtime errors
Format: Use markdown with sections for each category.
Be specific and actionable in your feedback.
"""
PARAMETER temperature 0.3
PARAMETER num_ctx 8192
Example 3: Technical Writer
FROM llama3.2
SYSTEM """
You are a technical documentation writer.
Write clear, concise documentation following these rules:
- Use active voice
- Include code examples for all concepts
- Structure with clear headings
- Add notes for gotchas and edge cases
Target audience: Intermediate developers
"""
PARAMETER temperature 0.5
Advanced Customization
Multiple Stop Tokens
FROM llama3.2
SYSTEM "You are a chatbot."
# Multiple stop sequences
PARAMETER stop "Human:"
PARAMETER stop "User:"
PARAMETER stop "###"
Combining with Adapters
FROM llama3.2
# Apply a LoRA adapter (if you've trained one)
ADAPTER /path/to/adapter.gguf
SYSTEM "You are specialized in medical terminology."
Managing Custom Models
# List all models (including custom)
ollama list
# Show details of custom model
ollama show python-coder --modelfile
# Copy custom model
ollama cp python-coder python-coder-v2
# Delete custom model
ollama rm python-coder
# Export (not directly supported, but you can save the Modelfile)
ollama show python-coder --modelfile > python-coder.Modelfile
Best Practices
1. Start Simple
# Good: Focused system prompt
SYSTEM "You are a Python expert. Write clean, typed code."
# Bad: Overly complex prompt
SYSTEM "You are an AI that does everything perfectly..."
2. Version Your Modelfiles
models/
├── sql-expert/
│ ├── Modelfile
│ └── README.md
├── code-reviewer/
│ ├── Modelfile
│ └── README.md
└── python-coder/
├── Modelfile
└── README.md
3. Test Iteratively
# Create and test
ollama create test-model -f Modelfile
ollama run test-model "test prompt"
# Adjust Modelfile, recreate
ollama rm test-model
ollama create test-model -f Modelfile
Custom models let you create specialized AI assistants without any training. In the next module, we'll build applications using Ollama's API. :::