Introduction
In the previous blog, we learned what Modelfiles are and how they help customize the behavior of AI models. However, understanding Modelfiles is only the first step. The real use of Modelfiles is when we ued them to create our own custom AI models.
By using Modelfiles we can convert a general purpose AI model into a AI assistant to perform any specific task. For example we can create AI Teacher, Coding Assistant, Interview Coach, Customer Support Agent, or a personal AI assistant without retraining the original model.
Why Create Custom AI Models?
Normally when we run a model such as Llama3 it behaves like a general purpose AI assistant.
Workflow:
user → llama3 → general response
The model tries to answer every type of question without following any specific role. However in real world applications we need specialized AI assistants.
Examples:
- AI Teacher
- Coding Assistant
- Interview Coach
- Customer Support Assistant
- Personal Tutor
- Research Assistant
Instead of writing the same instructions repeatedly in every prompt we can create a custom model that permanently follows those instructions. This is where Modelfiles become useful.
Step 1: Create a Modelfile
The first step is creating a Modelfile.
Example:
FROM llama3
SYSTEM You are an AI teacher who explains every concept in short.
Understanding the Modelfile
FROM Instruction
The FROM instruction specifies the base model that will be used.
Example:
FROM llama3
This means our custom model will be built using Llama 3.
SYSTEM Instruction
The SYSTEM instruction defines the personality and behavior of the model.
Example:
SYSTEM You are an AI teacher who explains every concept in short.
Now the model will try to answer like a teacher and provide short explanations.
Step 2: Save the Modelfile
Create a file named:
Modelfile
and save the content inside it.
Example:
FROM llama3
SYSTEM You are an AI teacher who explains every concept in short.
Step 3: Create the Custom Model
After creating the Modelfile use the following command:
ollama create myteacher -f Modelfile
Understanding the Command
ollama create :- Used to create a new custom model.
myteacher:- This is the name of the new model.
-f :- Specifies the Modelfile location.
Workflow:
Modelfile → ollama create → custom model
Ollama reads the instructions from the Modelfile and creates a new customized model.
Step 4: Run Custom Model
Once the model is created then it can be run using:
ollama run myteacher
Now your custom AI model is ready to use.
Testing Model
let any question to ask like:
What is Artificial Intelligence?
Possible response:
Artificial Intelligence (AI) is a field of computer science that enables machines to simulate human intelligence and perform tasks such as learning, reasoning, problem-solving, decision-making, and understanding language.
Notice that the response style follows the teacher behavior defined in the SYSTEM instruction.
read more about ollama:
previous topic: Understanding Modelfiles in Ollama