liveBook · Manning

Chapter 7: Controlling LLMs with the Ollama Python library

What you will learn in chapter 7 of Build Applications with Local AI Models on a Mac

The chapter introduces Python control of a local LLM and gives the checks needed before running its scripts.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces Python control of a local LLM and gives the checks needed before running its scripts.

Key terms: Python scripts, LLM, programmatic control, locally running AI model, virtual environment, (.venv), Ollama service, gemma3:4b, ollama pull gemma3:4b, port 11434

7.1 Your first Ollama Python script

Introduces an upcoming Python script that will ask Gemma 3 a question and print its answer, with the code explained afterward.

7.1.1 The simplest AI program

This subsection has the reader run a small Python script that sends a message to a locally running Gemma 3 model and prints its reply.

Key terms: chat_basic.py, Gemma 3 model, local

7.1.2 Breaking down the code

This section explains how a short Python script imports Ollama, sends messages to a local model, and prints the model’s reply.

Key terms: Ollama Python library, function, import, ollama.chat(), arguments, model, messages, Ollama server, Python dictionary, nested dictionary

7.2 Understanding the message format

This introduction notes that the previous script used a user message and previews three message roles that affect AI behavior.

7.2.1 The three LLM message roles

This subsection explains the three message roles sent to an LLM and what each contributes to a conversation.

Key terms: system, user, assistant, role

7.2.2 Adding a system message

This subsection shows that changing a system message can steer the same model’s response style and role.

Key terms: system message

7.2.3 Experimenting with different system messages

Changing the system message can change an LLM’s answer to the same question, though models vary in how closely they follow it.

Key terms: system message, system role, system instructions, larger models, smaller ones

7.3 Streaming responses for real-time output

Introduces streaming as a change in how a model response reaches a program, not a change to the message roles.

Key terms: streaming, system, user, assistant

7.3.1 The problem with waiting for the model’s response

This subsection contrasts waiting for a complete response with streaming text as it appears, explaining why streaming feels more responsive.

Key terms: streaming

7.3.2 Implementing streaming

The section has the reader run a streaming chat script and observe its response appear progressively rather than all at once.

Key terms: streaming, chat_stream.py

7.3.3 Understanding the code

Explains how the streaming Ollama script receives a response piece by piece and prints it as it arrives.

Key terms: stream=True, ollama.chat(), iterator, chunk, for chunk in stream, print(), end="", flush=True, newline, buffering

7.3.4 The streaming communication flow

This section shows how a Python script, the Ollama server, and an LLM communicate during streaming, and why streaming feels faster without necessarily speeding up generation.

Key terms: Python script, Ollama server, LLM model, streaming, token, responsive

7.4 Maintaining conversation history

This introduction contrasts a one-message script with a chatbot that responds to follow-up questions in context.

Key terms: single message, single response, follow-up questions, in context

7.4.1 The LLM’s memory problem

Explains why an Ollama chat appears to remember earlier messages and why a Python script must manage conversation history itself.

Key terms: LLMs, ollama run, conversation history

7.4.2 Building a conversation loop

This section explains how a Python script repeats a chat exchange while retaining conversation history until the user quits.

Key terms: while loop, while True, break, quit, system message, conversation history, streamed response, history list

7.4.3 Running the conversation

This subsection shows how to test a multiturn chat script, why it appears to remember a name, and how to end the chat.

Key terms: multiturn conversation, full history, infinite loop, quit, exit, bye

7.4.4 How the history list grows

This section traces how a conversation loop adds messages to history and sends that history to Ollama for each reply.

Key terms: history list, role: "user", role: "assistant", ollama.chat(), response_text

7.4.5 The complete conversation flow

This section summarizes the repeating conversation-history loop behind a chatbot and explains the limit imposed by a model’s context window.

Key terms: conversation history, conversation loop, context window

7.5 Exercises

7.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 6: Creating and managing Python virtual environments Chapter 8: Building a web UI with Streamlit →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.