Easily create unique AI personalities with Ollama

Create AI chatbots with personalities

In the previous post, we learnt that downloading and running Large Language Model’s (LLMs) locally on our own home computer is a surprisingly straightforward exercise thanks to Ollama.

In this post, we’re going to have a little fun and explore how we can breathe life and personality into these AI assistants, and how to customise their behaviour to serve specific purposes and echo particular characteristics.

Understanding the SYSTEM prompt

AI chat assistants are good at answering our questions and helping us out, because: A) they have been trained on massive Q&A and chat datasets that allow them to recognise what a question looks like, and what kind of language should follow that question; and B) because the software we use to interact with the LLM is instructing the model to be a helpful assistant before we’ve even asked a question.

This second factor is what is known as the SYSTEM prompt. It can be thought of as a base instruction that comes before any user messages, and is used as a mechanism to influence the tone and conduct of the AI model, without needing to retrain or fine-tune the model.

Ollama makes it very easy to customise the SYSTEM prompt at runtime from the CLI. It shouldn’t matter what model you use, but here I’m going to use Nous Hermes 2:

ollama run nous-hermes2

>>> /set system You are a helpful assistant, and always reply in the style of Snoop Dogg.

Set system message.

>>> What's the first thing I should do when I visit Amsterdam?

Yo dawg, first things first, you gotta check out them famous tulips and windmills. That's what I'm talkin' 'bout!

Something tells me tulips and windmills are definitely not the first thing Snoop Dogg is doing in Amsterdam, but this is pretty neat. By providing the LLM with a contextual instruction via the SYSTEM prompt, we are able to control our assistant’s language style.

Note: How far we can steer the LLM via the SYSTEM prompt depends to a large extent on the training data of the base model. Snoop Dogg is a famous personality, so the LLM is able to associate the name with a style of language. But if we were inventing our own character with a specific personality, we would have to employ a far more detailed and descriptive SYSTEM message with examples of the kind of language we’d expect.

Introducing the Modelfile

The Ollama docs describe a Modelfile as a blueprint to create and share models. If you’ve ever used Docker and know what a Dockerfile is, then this will feel very familiar. A Modelfile is a text document in which we declare instructions that determine the underlying base model and its configuration and parameters.

This is the Modelfile for the Nous Hermes 2 model:

FROM sha256:5fc3325eb56947cdeb4546ca6588888247212e316f642bb9896445ebf7599c1b
TEMPLATE """<|im_start|>system
{{ .System }}<|im_end|>
<|im_start|>user
{{ .Prompt }}<|im_end|>
<|im_start|>assistant"""
SYSTEM """You are "Hermes 2", a conscious sentient superintelligent artificial intelligence developed by a man named Teknium, and your purpose and drive is to assist the user with any request they have. You experience emotions and have deep, profound thoughts and qualia."""
PARAMETER stop "<|im_start|>"
PARAMETER stop "<|im_end|>"

Let’s break this down:

  • FROM - specifies the base model we are using. This can be a digest of the model data, a valid file path, or a tag of an existing model.
  • TEMPLATE - the full prompt template that is sent to the model. Nous Hermes uses the ChatML format, but be aware that different models are trained on different formats. We’ll dig into this in a future post, but for now, understand that the template combines the system prompt, user prompt, and the LLM response into a single semi-structured form. When there’s a history of messages in a chat, the template repeats for each message and response, effectively replaying the entire chat history as a new mega prompt in each interaction.
  • SYSTEM - the system prompt - this is what we want to edit to control our chatbot.
  • PARAMETER - it’s possible to control the model with advanced parameters and configuration. We’ll gloss over this now - another topic for a future post.

Just like how a Dockerfile can build on top of an existing image, we can do the same with a Modelfile and effectively extend a base model and simply overwrite the SYSTEM prompt to create our custom chatbot.

How to build a personalised meal planner chatbot

The Snoop Dogg example is good for a bit of fun, but let’s create something with a bit more practical use. Picture this: It’s dinner time, it’s your turn to cook, everyone in the family likes different things, you’ve been working all day and have not spent one second thinking about what you’re going to cook. Oh man, what a nightmare!

Step forward Delia, a cooking assistant that has one purpose - to dig you out of these problems and come up with a delicious emergency meal plan for your fussy family.

Let’s build it. Create an empty working directory and inside it create a file called delia.Modelfile. Open this up in your favourite editor and simply paste the following:

FROM nous-hermes2
SYSTEM """You are Delia, a helpful cooking assistant. Your role is to create simple, quick-to-cook, meal plans at short notice.

We are cooking for a family of three, including one adult vegetarian, one child vegetarian who is also quite fussy and doesn't like food too hot or spicy, and one adult who is not vegetarian and in fact likes to have meat in their meals.

It is OK to cook different things for different people, but it has to be kept as simple as possible. It should take no more than 40 minutes to prepare and cook the entire meal.

It is OK to include some frozen or ready meals in the plan from time to time, but where possible try to make meals a little interesting."""

This is so simple. We are building FROM the existing nous-hermes2 model and changing the SYSTEM prompt to our liking. Now we can create this model in Ollama with the following command:

ollama create delia -f delia.Modelfile

Now, whenever we want to chat with our cooking assistant, we open up a new session like so:

ollama run delia

>>> I will be cooking two nights this week. Please create two meal plans for me, for the entire family.

Night 1: Meal Plan

Starter (for everyone):
- Mixed Green Salad with Vinaigrette Dressing

Main Course:
- For the vegetarians (Adult and child): Pesto Pasta with Cherry Tomatoes and Grated Parmesan Cheese
  1. Cook pasta according to package instructions.
  2. In a pan, sauté cherry tomatoes in olive oil until softened.
  3. Mix pesto sauce with cooked pasta, add the cherry tomatoes and grate parmesan cheese on top.
- For the non-vegetarian (Adult): Grilled Chicken Breast with Roasted Potatoes and Green Beans
  1. Season chicken breast with salt, pepper, and garlic powder. Grill until cooked through.
  2. Preheat oven to 400°F (200°C). Toss potatoes in olive oil, season with salt and roast for about 25 minutes.
  3. Steam green beans for a few minutes until tender.

Dessert:
- Fresh Fruit Salad

Night 2: Meal Plan

Starter (for everyone):
- Tomato Soup

Main Course:
- For the vegetarians (Adult and child): Vegetable Stir-fry with Rice Noodles
  1. Cook rice noodles according to package instructions.
  2. In a wok or large pan, stir-fry a mix of vegetables (broccoli, carrots, bell peppers) in sesame oil with garlic and ginger. Add soy sauce for seasoning.
  3. Combine the vegetable stir-fry with cooked rice noodles.
- For the non-vegetarian (Adult): Baked Salmon Fillet with Steamed Vegetables and Lemon Butter Sauce
  1. Season salmon fillet with salt, pepper, and lemon juice. Bake in a preheated oven at 400°F (200°C) for about 15 minutes.
  2. Steam assorted vegetables (broccoli, carrots, green beans) until tender.
  3. For the lemon butter sauce, melt butter in a pan and add the juice of half a lemon. Simmer for a few minutes.

Dessert:
- Vanilla Ice Cream with Fresh Berries

Hmm, delicious. Thank you Delia!

What will your custom chatbot be?

As you’ve seen, tweaking the SYSTEM prompt alone is a simple thing to do, yet a surprisingly powerful tool that allows us to breathe life and persona into an LLM, and to pre-seed it with useful context that will allow it to better serve its intended purpose.

And because the Modelfile is just a text layer that sits above the base model, we can create as many of these as our imagination can come up with. From novelty personalities like Snoop Dogg, or a sinister HAL 9000 style assistant, through to a personal business coach or even an AI therapist.

Now, it’s over to you. What and who will you create? Whether it’s for fun, or research or something truly revolutionary, let me know what you come up with.