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

Kubernetes–Limit your environmental impact

Reducing the carbon footprint and CO2 emission of our (cloud) workloads, is a responsibility of all of us. If you are running a Kubernetes cluster, have a look at Kube-Green . kube-green is a simple Kubernetes operator that automatically shuts down (some of) your pods when you don't need them. A single pod produces about 11 Kg CO2eq per year( here the calculation). Reason enough to give it a try! Installing kube-green in your cluster The easiest way to install the operator in your cluster is through kubectl. We first need to install a cert-manager: kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.14.5/cert-manager.yaml Remark: Wait a minute before you continue as it can take some time before the cert-manager is up & running inside your cluster. Now we can install the kube-green operator: kubectl apply -f https://github.com/kube-green/kube-green/releases/latest/download/kube-green.yaml Now in the namespace where we want t...

Azure DevOps/ GitHub emoji

I’m really bad at remembering emoji’s. So here is cheat sheet with all emoji’s that can be used in tools that support the github emoji markdown markup: All credits go to rcaviers who created this list.

DevToys–A swiss army knife for developers

As a developer there are a lot of small tasks you need to do as part of your coding, debugging and testing activities.  DevToys is an offline windows app that tries to help you with these tasks. Instead of using different websites you get a fully offline experience offering help for a large list of tasks. Many tools are available. Here is the current list: Converters JSON <> YAML Timestamp Number Base Cron Parser Encoders / Decoders HTML URL Base64 Text & Image GZip JWT Decoder Formatters JSON SQL XML Generators Hash (MD5, SHA1, SHA256, SHA512) UUID 1 and 4 Lorem Ipsum Checksum Text Escape / Unescape Inspector & Case Converter Regex Tester Text Comparer XML Validator Markdown Preview Graphic Col...