Module 5 Lesson 5: Model Inheritance
·AI & LLMs

Module 5 Lesson 5: Model Inheritance

Standing on the shoulders of giants. How to create layers of custom models using the FROM command.

Model Inheritance: Layering Instructions

One of the most powerful features of Modelfiles is Inheritance. You don't always have to start from a "Base" model like llama3. You can start from a model you already customized.

This allows you to create a "hierarchy" of AI behavior.

1. The Concept of "Base" vs "Derived"

  1. Level 1 (The Base): llama3 (The smart generalist).
  2. Level 2 (The Expert): cloud-expert (Inherits from Llama 3 but adds 50 pages of cloud knowledge).
  3. Level 3 (The Persona): polite-cloud-expert (Inherits from cloud-expert but adds a polite tone).

2. How to implement Inheritance

Simply use the name of your previously created model in the FROM line.

File 1: BaseBot

FROM llama3
SYSTEM "You are a professional software engineer."

ollama create engine-bot -f BaseBot

File 2: PythonBot

FROM engine-bot
SYSTEM "You specialize ONLY in Python. Refuse to talk about Javascript."

ollama create python-bot -f PythonBot


3. Why is this useful?

DRY (Don't Repeat Yourself)

If you have a core "System Prompt" that includes your company's safety rules, you can put it in a base-company-model. Every other bot you build for HR, Sales, or Engineering can inherit from that base, ensuring the safety rules are always present without you having to copy-paste them into every file.

Versioning

You can create my-bot:v1 and then build my-bot:v2 by inheriting from v1 and just changing one parameter or adding a new stop sequence.


4. What is "Copied" and what is "Overwritten"?

When you inherit:

  • SYSTEM: The new SYSTEM prompt overwrites the old one. If you want to keep the old instructions, you must include them in the new file.
  • PARAMETERS: The new parameters replace the old ones.
  • ADAPTERS: (Advanced) LoRA adapters are kept and stacked (which we will learn in Module 11).

Key Takeaways

  • Inheritance allows you to create layers of specialized models.
  • Use your own model names in the FROM line to inherit.
  • Inheritance is the best way to maintain consistency across multiple "Personas."
  • A "Derived" model is smaller because it doesn't download the weights again; it just points to the "Base" model on your disk.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn