Skip to main content

Tweak your LLM models with Ollama

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:

 

More information

ollama/docs/modelfile.md at main · ollama/ollama

Popular posts from this blog

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Cache stampede: when our cache turned against us

While investigating some performance issues, we ran into an ASP.NET Core API that cached a fairly expensive aggregation query for 60 seconds. Under normal load, that was fine: one request rebuilds the cache, everyone else reads from it. Under peak load, dozens of requests would arrive in that same expiry window, all see a cache miss, and all fire the same expensive query in parallel. The database didn't like that. That was the moment when our caching layer stopped helping and started hurting. A burst of requests comes in at the same time, all miss the cache, and all go hammer the database or the downstream API at once. That's a cache stampede . The cache was supposed to protect our backend, and for a few hundred milliseconds it did the opposite. Why this happens IMemoryCache.GetOrCreate (and its async sibling) looks like it protects you, but it doesn't add any locking on its own. Look at the naive version: public async Task<Report> GetReportAsync(string key) ...

A complex system designed from scratch never works

A few years ago, I worked as an architect on a big mainframe rewrite. I still count it as one of my failures. Not because the technology was wrong, but because I couldn't convince the management team to simplify the approach. Years later, the organization is still struggling to get the new system up and running. I left the project at the time, because I couldn't put my name behind an approach that would take very long and cost a lot of money without a working system to show for it along the way. Gall’s Law That memory keeps coming back to me, because it's a textbook case of Gall's Law playing out in real life. Gall's Law , from John Gall's Systemantics , states it plainly: A complex system that works is invariably found to have evolved from a simple system that worked. A complex system designed from scratch never works, and it cannot be patched to make it work. You have to start over with a simple system that works. What does that mean in practice,...