Module 5 Lesson 2: Modelfile Syntax
·AI & LLMs

Module 5 Lesson 2: Modelfile Syntax

Mastering the commands. A deep dive into FROM, SYSTEM, PARAMETER, and ADAPTER.

Modelfile Syntax: The Language of Creation

To create a custom model, you need to understand the four primary "verbs" of a Modelfile. Each one controls a different part of the AI's behavior.

1. FROM (The Foundation)

Every Modelfile must start with a FROM instruction. This tells Ollama which base model to "inherit" from.

Examples:

  • FROM llama3: Uses the standard 8B Llama 3 model.
  • FROM ./my-model.gguf: Uses a raw file you downloaded from Hugging Face.

2. SYSTEM (The Persona)

The SYSTEM instruction sets the persistent instructions that the model will follow. This is where you define the model's role, tone, and knowledge boundaries.

Example:

SYSTEM """
You are a Cloud Architect. 
Always provide answers using AWS-specific services. 
Be concise and avoid conversational filler like 'Sure!' or 'Of course!'
"""

Note: Use triple quotes (""") for multi-line instructions.


3. PARAMETER (The Engine Settings)

Parameters allow you to fine-tune the technical performance of the model.

Common Parameters:

  • PARAMETER temperature 0.5: Controls creativity (0 is factual, 1 is creative).
  • PARAMETER num_ctx 8192: Sets the context window (memory size).
  • PARAMETER stop "[DONE]": Tells the model to stop generating text when it sees this specific string.
  • PARAMETER repeat_penalty 1.1: Prevents the model from repeating the same sentence over and over.

4. TEMPLATE (The Advanced Formatting)

Every model (Llama, Mistral, Google) expects prompts to be formatted in a specific way. For example:

  • Llama 3: <|begin_of_text|><|start_header_id|>user<|end_header_id|>
  • Mistral: [INST] ... [/INST]

Unless you are an expert, you usually don't need to change this. Ollama automatically applies the correct template based on the FROM line.


5. MESSAGE (The Pre-fill)

You can include "Few-shot" examples directly in the Modelfile so the model always sees them before your first question.

Example:

MESSAGE user "What is the capital of France?"
MESSAGE assistant "The capital of France is Paris. [Butler Mode Active]"

The Complete Picture

Here is a professional-grade Modelfile for a "Code Reviewer" bot:

FROM llama3
PARAMETER temperature 0.1
PARAMETER num_ctx 16384
SYSTEM "You are an expert Senior Security Engineer. Review the user's code for potential SQL injection and XSS vulnerabilities."

How to Apply It

Save that text as a file named SecurityBot. Then run: ollama create sec-bot -f SecurityBot


Key Takeaways

  • FROM is the only mandatory instruction.
  • SYSTEM defines the "Soul" of the model.
  • PARAMETER defines the "Spec" of the engine.
  • Use Triple Quotes for long instructions.
  • The ollama create command brings the script to life.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn