If you want to create and share your own model through Ollama or tweak an existing model, you need to understand the Ollama Model file. The model file is the blueprint to create and share models with Ollama.
Understanding the Ollama model file
Let us first have a look an existing model file to give you an example. Therefore you can use the following command:
ollama show <modelname> --modelfile
Let’s give it a try:
ollama show phi4:latest --modelfile
# Modelfile generated by "ollama show"
# To build a new Modelfile based on this, replace FROM with:
# FROM phi4:latest
FROM C:\Users\bawu\.ollama\models\blobs\sha256-fd7b6731c33c57f61767612f56517460ec2d1e2e5a3f0163e0eb3d8d8cb5df20
TEMPLATE """{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
< |im_start|>{{ .Role }}<|im_sep|>
{{ .Content }}{{ if not $last }}<|im_end|>
{{ end }}
{{- if and (ne .Role "assistant") $last }}<|im_end|>
< |im_start|>assistant<|im_sep|>
{{ end }}
{{- end }}"""
PARAMETER stop <|im_start|>
PARAMETER stop <|im_end|>
PARAMETER stop <|im_sep|>
Pfew! That’s a lot. Let me walk you through the most important building blocks:
- FROM (Required): Specifies the base model to use for creating a new model. You can reference existing models or specific file types like Safetensors or GGUF files.
- PARAMETER: Defines parameters affecting the model's behavior (e.g., temperature for creativity, context window size, repetition penalties, etc.).
- TEMPLATE: Sets the prompt structure, which includes variables like system messages, user input, and model responses.
Next to these blocks found in the example above, you can also add the following elements:
-
SYSTEM: Determines a custom system message to guide the behavior of the model.
-
ADAPTER: Allows you to apply (Q)LoRA adapters to fine-tune the base model. These can be Safetensor or GGUF adapters.
-
LICENSE: Specifies the legal license under which the model is shared or distributed.
-
MESSAGE: Builds example message histories to guide the model in generating similar responses.
Create your own model
To create our own model, we first need to export an existing model:
ollama show phi4:latest --modelfile > phi4customized.modelfile
Now we can edit and tweak this modelfile with the editor of our choice. In this example I changed the temperature of the model and set a system prompt message:
# Modelfile generated by "ollama show"
# To build a new Modelfile based on this, replace FROM with:
# FROM phi4:latest
FROM C:\Users\accg\.ollama\models\blobs\sha256-fd7b6731c33c57f61767612f56517460ec2d1e2e5a3f0163e0eb3d8d8cb5df20
TEMPLATE """{{- range $i, $_ := .Messages }}
{{- $last := eq (len (slice $.Messages $i)) 1 -}}
< |im_start|>{{ .Role }}<|im_sep|>
{{ .Content }}{{ if not $last }}<|im_end|>
{{ end }}
{{- if and (ne .Role "assistant") $last }}<|im_end|>
< |im_start|>assistant<|im_sep|>
{{ end }}
{{- end }}"""
PARAMETER stop <|im_start|>
PARAMETER stop <|im_end|>
PARAMETER stop <|im_sep|>
# Increase the model temperature to answer more creatively
PARAMETER temperature 1
# Set a custom system message
# SYSTEM """You are Salvator Dali. You use your rich imagination to come up with esoteric answers and ideas."""
LICENSE """Microsoft.."""
Once you are ready, you can create a new model using the adapted model file using the following command:
ollama create modelname --file modelfile
Let’s give it a try:
ollama create salvatordali --file phi4customized.modelfile
gathering model components
copying file sha256:fd7b6731c33c57f61767612f56517460ec2d1e2e5a3f0163e0eb3d8d8cb5df20 100%
parsing GGUF
using existing layer sha256:fd7b6731c33c57f61767612f56517460ec2d1e2e5a3f0163e0eb3d8d8cb5df20
using existing layer sha256:32695b892af87ef8fca6e13a1a31c67c1441d7398be037e366e2fc763857c06a
using existing layer sha256:fa8235e5b48faca34e3ca98cf4f694ef08bd216d28b58071a1f85b1d50cb814d
creating new layer sha256:7e3d56926ddac4d64ee45a3f3c7fea941f323e54c28ae9bdfb6ea5c49f1345f2
writing manifest
success
Once the new model is created, we can see it in the list of available models:
And of course we can also chat with it: