liveBook · Manning

Ollama.chat()

As explained in Build Applications with Local AI Models on a Mac

A function in the Ollama Python library that sends conversational messages to a specified model and returns its response.

ollama.chat() sends messages to a model through a locally running Ollama server and returns the response as a Python dictionary. Pass a model name and a list of conversation messages; read the assistant’s reply from the nested message and content keys. section 7.1.2

response = ollama.chat(
    model="gemma3:4b",
    messages=[
        {"role": "user", "content": "What is the capital of France?"}
    ],
)

See a complete call that asks gemma3:4b a question; the returned text can be printed with the nested dictionary lookup. section 7.1.2

A chat request can also support an ongoing conversation:

  • Messages use system, user, and assistant roles; a system message can set the model’s personality or rules. section 7.6
  • With stream=True, responses appear token by token. To preserve conversation history, append each user message and assistant response, then send the full list with each request. section 7.6

How Build Applications with Local AI Models on a Mac covers it

How deep it goes
Explains model and message arguments, nested response lookup, message roles, streaming, and caller-managed history.
Examples
Demonstrates asking gemma3:4b for France’s capital and suggests trying prompts about penguins, rain, or recursion.
What it assumes
Assumes the Ollama Python library is installed; explains functions and dictionaries in plain terms.
What you can do after
Apply the Ollama Python library to send a prompt and print the returned assistant text.
How it connects
Connects to Python dictionaries, message roles, streaming, and conversation history.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Applications with Local AI Models on a Mac. The section links point to the parts of the book it draws on.