RUNLOCALAIv38
->Will it run?Best GPUCompareTroubleshootStartLearnPulseModelsHardwareToolsBench
Run check
RUNLOCALAI

Independently operated catalog for local-AI hardware and software. Hand-written verdicts. Source-cited claims. Reproducible commands when we have them.

OP·Eruo Fredoline
DIR
  • Models
  • Hardware
  • Tools
  • Benchmarks
TOOLS
  • Will it run?
  • Compare hardware
  • Cost vs cloud
  • Choose my GPU
  • Prompting kits
  • Quick answers
REF
  • All buyer guides
  • Learn local AI
  • Methodology
  • Glossary
  • Errors KB
  • Trust
  • Suggest a feature
EDITOR
  • About
  • Author
  • How we make money
  • Editorial policy
  • Contact
LEGAL
  • Privacy
  • Terms
  • Sitemap
MAIL · MONTHLY DIGEST
Get monthly local AI changes
Monthly recap. No spam.
DISCLOSURE

Some links on this site are affiliate links (Amazon Associates and other first-class retailers). When you buy through them, we earn a small commission at no extra cost to you. Affiliate links do not influence our verdicts — there are cards we rate highly that we don't have affiliate relationships with, and cards that sell well that we refuse to recommend. Read more →

© 2026 runlocalai.coIndependently operated
RUNLOCALAI · v38
  1. >
  2. Home
  3. /Learn
  4. /Courses
  5. /First Local Chatbot
  6. /Ch. 3
First Local Chatbot

03. Ollama Integration

Chapter 3 of 15 · 15 min
KEY INSIGHT

Ollama's API is just HTTP JSON over SSE. You do not need an SDK; `httpx` is sufficient for all operations.

Add a client module for Ollama. Create app/ollama_client.py:

import httpx

OLLAMA_BASE = "http://localhost:11434"

def list_models() -> list[dict]:
    response = httpx.get(f"{OLLAMA_BASE}/api/tags", timeout=5.0)
    response.raise_for_status()
    data = response.json()
    return [m["name"] for m in data.get("models", [])]

def stream_chat(model: str, messages: list[dict]):
    """Yield raw SSE lines from Ollama."""
    payload = {
        "model": model,
        "messages": messages,
        "stream": True,
    }
    with httpx.stream("POST", f"{OLLAMA_BASE}/api/chat", json=payload, timeout=60.0) as resp:
        resp.raise_for_status()
        for line in resp.iter_lines():
            if line:
                yield line + "\n"

Add the route to app/main.py:

from app.ollama_client import list_models

@app.get("/models")
def get_models():
    return {"models": list_models()}

Test it. If list_models() raises httpx.ConnectError, Ollama is not running or the URL is wrong. Verify with curl http://localhost:11434/api/tags. If the error says model not found, that means Ollama is running but the requested model is not pulled—run ollama pull llama3 first.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

Local verification checkpoint

Run the smallest example from this chapter in a local workspace and record the package version, runtime, data path, and observed output. If the result depends on model size, vector count, CPU/GPU backend, or available memory, note that constraint beside the exercise so the lesson remains reproducible.

EXERCISE

Write a test script test_ollama.py that calls list_models() and prints the model names. Run it with python test_ollama.py.

← Chapter 2
FastAPI Backend Setup
Chapter 4 →
Streaming Responses