Developer Blog

Tipps und Tricks für Entwickler und IT-Interessierte

Ollama Cookbook

Setting up VS Code Environment

Debugging

Create a file launch.json and add this to configuration

"configurations": [
        {
            "name": "Streamlit",
            "type": "debugpy",
            "request": "launch",
            "module": "streamlit",
            "args": [
                "run",
                "${file}",
                "--server.port",
                "2000"
            ]
        }
    ]

Ollama und Python

Beispiele

Getting Started

Python

pip install ollama
import ollama

response = ollama.chat(model='llama2', messages=[
  {
    'role': 'user',
    'content': 'Why is the sky blue?',
  },
])

print(response['message']['content'])

JavaScript

npm install ollama
import ollama from 'ollama'

const response = await ollama.chat({
  model: 'llama2',
  messages: [{ role: 'user', content: 'Why is the sky blue?' }],
})
console.log(response.message.content)

Use cases

Both libraries support Ollama’s full set of features. Here are some examples in Python:

Streaming

for chunk in chat('mistral', messages=messages, stream=True):
    print(chunk['message']['content'], end='', flush=True)

Multi-modal

with open('image.png', 'rb') as file:
  response = ollama.chat(
    model='llava',
    messages=[
      {
        'role': 'user',
        'content': 'What is strange about this image?',
        'images': [file.read()],
      },
    ],
  )

print(response['message']['content'])

Text Completion

result = ollama.generate(
  model='stable-code',
  prompt='// A c function to reverse a string\n',
)

print(result['response'])

Creating custom models

modelfile='''
FROM llama2
SYSTEM You are mario from super mario bros.
'''

ollama.create(model='example', modelfile=modelfile)

Custom client

ollama = Client(host='my.ollama.host')

More examples are available in the GitHub repositories for the Python and JavaScript libraries.

Tipps und Tricks

ollama serve

OLLAMA_ORIGINS=https://webml-demo.vercel.app
OLLAMA_HOST=127.0.0.1:11435 ollama serve

Docker

Run Ollama in Docker container

CPU only

docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama

Nvidia GPU

Install the Nvidia container toolkit.
Run Ollama inside a Docker container

docker run -d --gpus=all -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama

Run a model

Now you can run a model like Llama 2 inside the container.

docker exec -it ollama ollama run llama2

OpenAI

OpenAI Compatibility

from openai import OpenAI

client = OpenAI(
    base_url = 'http://localhost:11434/v1',
    api_key='ollama', # required, but unused
)

response = client.chat.completions.create(
  model="llama2",
  messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Who won the world series in 2020?"},
    {"role": "assistant", "content": "The LA Dodgers won in 2020."},
    {"role": "user", "content": "Where was it played?"}
  ]
)

print(response.choices[0].message.content)

Using Streamlit

LangChain

from langchain_community.llms import Ollama
llm = Ollama(model="gemma2")
llm.invoke("Why is the sky blue?")

LlamaIndex

from llama_index.llms.ollama import Ollama

llm = Ollama(model="gemma2")
llm.complete("Why is the sky blue?")

LLMs and Models by Example

wizard-math

ollama run wizard-math 'Expand the following expression: $7(3y+2)$'

Ollama | Getting Started

Installation

Read here for details.

Linuxcurl -fsSL https://ollama.com/install.sh | sh
Mac OSDownload
WindowsDownload

Start Ollama Service

ollama start

This starts the Ollama Service and binds it to the default ip address 127.0.0.1:11434

If you want to access the service from another host/client, you have to use the ip address 0.0.0.0

systemctl stop ollama.service
export OLLAMA_HOST=0.0.0.0:11434
ollama start

To change the IP address of the service, edit the file /etc/systemd/system/ollama.service and add

[Unit]
Description=Ollama Service
After=network-online.target

[Service]
ExecStart=/usr/local/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="OLLAMA_HOST=0.0.0.0:11434"

[Install]
WantedBy=default.target

Access the Ollama Service from your Browser using <ip-adress of host>:11434

Sample Python Chat

Install Python

Install Ollama Library

pip install ollama

Create the chat programm

#!/usr/bin/env python

from ollama import Client

ollama = Client(host='127.0.0.1')

response = ollama.chat(model='llama3', messages=[
  {
    'role': 'user',
    'content': 'Why is the sky blue?',
  },
])
print(response['message']['content'])

Run the programm

❯ ./chat.py

Hint: To see, whats happen with the service, monitor the logfile

❯ sudo journalctl -u ollama.service -f

Query the API

Read the API definition here

curl http://localhost:11434/api/generate -d '{
  "model": "llama2",
  "prompt": "Why is the sky blue?",
  "stream": false
}'

Create a simple ChatGPT Clone

Create the Gradio App

The Art of AI: Create / Recreating Images

Today, i want to find out, how to create / recreating images with ChatGPT.

Challenge: I found an image and want to recreate the image:

Write a Prompt that creates this Image
Create an image of a woman with long, sleek black hair and striking features. She should have a strong, confident posture with her arms crossed. She's wearing a simple, elegant black dress with a round neckline. The background is a modern, minimalist office with a hint of sophistication. The color palette should be neutral, with emphasis on shades of grey and black to match her attire. The lighting is soft and diffused, highlighting her from the front and casting a gentle shadow behind her. The overall aesthetic is chic and polished with a touch of corporate professionalism.
Write a Prompt that creates this Image
Create an image of a poised and professional woman with long, sleek black hair and prominent, striking features. She stands with a sense of authority, her arms confidently crossed in front of her. She is dressed in a simple yet sophisticated black dress with a modest round neckline. The setting is a contemporary, minimalist office space that exudes a refined elegance. The color scheme is a harmonious blend of neutral tones, dominated by greys and blacks that complement her attire. Soft, ambient lighting bathes the scene, casting a gentle illumination that accentuates her silhouette against the muted sophistication of the office backdrop. The composition should convey an atmosphere of chic professionalism and understated luxury.
Write a Prompt that creates this Image
Create an image of an elegant and professional woman with long, black hair and defined, striking features. She exhibits a posture of confidence and composure with her arms crossed. Her attire is a chic, form-fitting black dress with a modest neckline. The environment is a stylish, minimalist office setting with sophisticated furnishings and a neutral color scheme emphasizing grays and blacks. The scene is illuminated with soft, ambient lighting that casts a gentle glow on the woman, highlighting her assertive stance against the contemporary backdrop, evoking a sense of refined grace and corporate elegance.

Notes:

Mostly, after running the same prompt the second time, ChatGPT responded with something like that:

So, i ask ChatGPT nicely to do it again:

Create the image again, because each time the image looks different

Next Step: find the precise prompt

As we see, using the same prompt results in different images. So i asked ChatGPT how to create a prompt that produces the same image each time:

MeUsing one prompt multiple times, each image is different from another. How can a state the prompt more clearly so that the prompt creates each time exactly the same imageChatGPT
ChatGPTCreating exactly the same image multiple times using a text-to-image AI model like DALL-E is challenging due to the inherent variability in how these models interpret and render prompts. Each time a prompt is processed, the model generates a unique interpretation based on its training and algorithms, which can lead to variations in the resulting images. However, there are some strategies you can use to minimize these differences:
Be Highly Specific: Provide as many details as possible in your prompt, including specifics about posture, facial expression, clothing, and background elements. The more detailed your description, the less room there is for the AI to interpret differently.
Use Precise Descriptors: Instead of using general terms, use precise descriptors. For example, instead of saying “long, black hair,” specify the style, such as “long, straight, black hair that falls to mid-back.”
Define the Scene Layout: Be explicit about the composition of the image, like where the subject is positioned in the frame, how much of the background is visible, and the perspective or angle of the view.
Describe Colors and Lighting: Clearly state the colors you want to be used, including shades and tones, and describe the lighting (e.g., “soft, diffused lighting from the front left”).
Include Comparable Examples: If possible, reference a similar existing image or style to guide the AI’s interpretation.
Consistency in Format: If you’re looking for a portrait, landscape, or specific aspect ratio, mention this explicitly.
Even with these strategies, it’s important to remember that AI models like DALL-E are probabilistic and may still produce variations with each rendering. The nature of these models means they interpret prompts creatively, so exact replication of images is not always possible.

Ok, lets try:

Me
Using your suggestions, create a precise prompt that create this image. Use all 6 strategies in your prompt
ChatGPTCreate a digital portrait of a woman who embodies elegance and professionalism. She should have long, straight, jet-black hair with a glossy sheen, cascading down to mid-back. Her facial features are sharply defined, with high cheekbones, almond-shaped hazel eyes, meticulously groomed eyebrows, and full, neutral-colored lips. Her skin has a matte finish with a porcelain-like quality.
Her stance is assertive; she stands straight with her shoulders back and arms crossed at the waist, showcasing a silver bracelet on her right wrist and a matching ring on her left middle finger. She wears a tailored, form-fitting black sheath dress with three-quarter sleeves and a boat neckline, ending just above the knees.
The setting is an upscale, minimalist office: to her right is a steel-gray filing cabinet with a potted green succulent on top; to her left, a sleek, black office chair behind a dark walnut desk, on which rests a closed silver laptop and a white ceramic mug. The background comprises floor-to-ceiling windows with thin, vertical blinds casting soft shadows, and the walls are a light gray.
The lighting is soft and directional, coming from the top left, creating a subtle highlight on her left cheek and a soft shadow to her right, enhancing her features without harshness. The overall color palette is monochrome, with a focus on blacks, whites, and grays, providing contrast against her black dress and the lighter office environment.
The composition is centered and balanced, with the woman placed in the middle of the frame, allowing for an equal amount of office space on either side of her. The camera angle is at eye level, ensuring a direct and engaging connection with the viewer. The image should convey a narrative of confidence, sophistication, and modern professional elegance.

Wow, impresiv. Ok, here are the image created by this prompt:

The results looks more the same, but still lacking these “unique” factor.

So i asked ChatGPT again:

MeThe images still looks different. Is there a deterministic way to uniquely generate an imageChatGPT
ChatGPTAI image generation models like DALL-E are designed to introduce variation and creativity into their outputs. Even with highly detailed prompts, they are not deterministic in the traditional sense — you won’t get the exact same image with each generation because the model incorporates an element of randomness or “seed” into each creation process. This randomness is a core feature, as it enables the generation of unique images from the same prompt.
However, in some advanced implementations of these models, you can specify a “seed” value, which can help in producing the same image across multiple runs if the seed is kept constant. The “seed” is essentially a starting point for the random number generator used in the generation process. By using the same seed, you tell the AI to start from the same random point, leading to the same sequence of “random” numbers, which can result in the same output.
At the moment, the system you are interacting with does not provide an option to control the seed value manually for each request. Each new image generation is treated as a fresh instance, without a way to replicate the exact randomness of a previous run.
Therefore, while you can guide the AI with very specific and detailed prompts to achieve close approximations, there is currently no way to ensure that a publicly available AI image generation model like DALL-E will produce the exact same image twice.

Ok, so we wont get 100% equal images. But being more precise, hopefully we will reach someting around 90 – 95%.

Me

Could you suggest the general structure of an image creating prompt, so that this prompt will create nearly the same images, each time i run the pr

To achieve the most consistent results with an AI image generation model like DALL-E, you should craft a prompt that is detailed and specific, reducing the room for interpretation as much as possible. Here’s a general structure you can follow:

  1. Subject Description: Start with a detailed description of the main subject, including physical characteristics, expressions, and attire.
    • Example: “Create an image of a woman in her 30s with shoulder-length curly brown hair, green eyes, wearing a navy blue blazer and white shirt.”
  2. Posture and Expression: Clearly describe the subject’s posture, facial expression, and any gestures.
    • Example: “She has a friendly smile, direct eye contact with the viewer, and her hands are folded in her lap.”
  3. Setting and Background: Detail the setting, including specific objects, colors, and the overall atmosphere.
    • Example: “She is seated on a red armchair in a well-lit room with bookshelves filled with colorful books in the background.”
  4. Lighting and Shadows: Describe the lighting direction and intensity, as well as the resulting shadows.
    • Example: “There is soft lighting coming from the left, creating a gentle shadow on the right side of her face.”
  5. Color Palette: Specify the colors you want to be dominant in the image.
    • Example: “The color palette is warm, with rich browns and creams, accentuating the wooden elements of the furniture and books.”
  6. Camera Angle and Composition: Mention the perspective of the image, such as the angle and framing.
    • Example: “The viewpoint is at eye level, capturing the upper body of the woman, centered in the frame with equal space on both sides.”
  7. Additional Details: Add any other specific details that you want to include.
    • Example: “She wears a silver pendant necklace and has a small mole on her right cheek.”

Here’s how it would look put together:

“Create an image of a woman in her 30s with shoulder-length curly brown hair and green eyes, smiling friendly at the viewer, seated on a red armchair with hands folded in her lap. She is wearing a navy blue blazer and white shirt. The room is well-lit, with soft lighting coming from the left creating a gentle shadow on the right side of her face. The background is filled with bookshelves with colorful books. The color palette is warm, with rich browns and creams. The viewpoint is eye level, framing the woman in the center with equal space on both sides. She wears a silver pendant necklace and has a small mole on her right cheek.”

Even with this structure, slight variations can occur due to the stochastic nature of AI image generation. However, providing a highly structured and detailed prompt like the one above can significantly increase the consistency of the images produced.

Ok, let’s try i out

Me

Create an image of a woman in her 30s with shoulder-length curly brown hair and green eyes, smiling friendly at the viewer, seated on a red armchair with hands folded in her lap. She is wearing a navy blue blazer and white shirt. The room is well-lit, with soft lighting coming from the left creating a gentle shadow on the right side of her face. The background is filled with bookshelves with colorful books. The color palette is warm, with rich browns and creams. The viewpoint is eye level, framing the woman in the center with equal space on both sides. She wears a silver pendant necklace and has a small mole on her right cheek.