How to Customize LLM Models with Ollama's Modelfile

Ollama modelfile is the blueprint to create and share models with Ollama. This guide will show you how to customize your own models, and interact with them via the command line or Web UI.

What is Ollama Modelfile

An Ollama Modelfile is a configuration file that defines and manages models on the Ollama platform. Create new models or modify and adjust existing models through model files to cope with some special application scenarios. Custom prompts are embedded into the model, modify and adjust context length, temperature, random seeds, reduce the degree of nonsense, increase or decrease the diversity of output text, etc. (Note: This is not fine-tuning, just adjusting the original parameters of the model.)

The Ollama Modelfile simplifies the process of managing and running LLMs locally, ensuring optimal performance through effective resource allocation. With Ollama Modelfile, you can easily unlocking the power of Large Language Models.

Prerequisite

Install the Ollama framework in advance;

Download large language models that need to be customized in advance;

Can successfully run the downloaded large language model.

Customize LLM Models with Ollama's Modelfile

Way 1. Create and Use Custom Models with Ollama Command Line

Step 1. Customize the Model

Access the model file to understand its structure and parameters. Use ollama help show to show all the commands.

ollama show llama2:latest --modelfile
# Modelfile generated by "ollama show"
# To build a new Modelfile based on this one, replace the FROM line with:
# FROM llama2:latest

FROM C:\Users\Administrator\.ollama\models\blobs\sha256-8934d96d3f08982e95922b2b7a2c626a1fe873d7c3b06e8e56d7bc0a1fef9246
TEMPLATE """[INST] <<SYS>><</SYS>>

 [/INST]
"""
PARAMETER stop "[INST]"
PARAMETER stop "[/INST]"
PARAMETER stop "<<SYS>>"
PARAMETER stop "<</SYS>>"

Copy the model file to create a customized version.

ollama show llama2:latest --modelfile > myllama2.modelfile

Use any text or code editing tool,open and modify the system prompt and template in the model file to suit your preferences or requirements.

use notepad edit a modelfile

Notes:

The Modelfile is not case sensitive. In the examples, uppercase instructions are used to make it easier to distinguish it from arguments.

Instructions can be in any order. In the examples, the FROM instruction is first to keep it easily readable.

Step 2. Create Your Custom Model

Use the ollama create command to create a new model based on your customized model file.

ollama create myllama2 --file myllama2.modelfile

Verify the creation of your custom model by listing the available models using ollama list.

ollama create your modelfile

Step 3. Test Your Custom Model

Chat with your custom model using the terminal to ensure it behaves as expected. Verify that it responds according to the customized system prompt and template.

ollama run myllama2
>>> Who are you?
Woah! *adjusts hat* It's-a me, Mario! *grinning* I'm the plumber with a passion for rescuing Princess Peach from that no-good Bowser. *winks* And, of course, I'm here to help you with any questions or problems you might have! *hands out a high-five* So, what's on your mind?

>>> Is Peach in trouble again?
Oh boy, is she ever! *excitedly* That no-good Bowser has gotten his hands on the Princess once again, and I need to hop to it to save her! *grabs a fire flower* It's-a gonna be a tough battle, but I ain't afraid of a little danger! *flaps hat* Want to come along and help me rescue the Princess? *offers a hand* We can make short work of that Koopa king and his minions! *puffs out chest* Are you with me?

>>> Send a message (/? for help)

Way 2. Create and Use Custom Models with Open WebUI

Step 1. Create a modelfile

ollama open webui create a modelfile

Step 2. Edit your modelfile

ollama open webui edit your modelfile

Step 3. Test your custom model

ollama open webui test custom model

Conclusion

In summary, an Ollama Modelfile is a vital tool for managing and utilizing large language models on the Ollama platform, offering a user-friendly and streamlined experience for developers and researchers working with these advanced AI models.

By following these steps, you’ll be able to customize your own model, interact with it, and begin exploring the world of large language models with ease.