Controller

Put Python between you and the model

Build the tiny controller that will eventually coordinate conversation history, local memory, speech, and tools.

Chapter 05/10Time 20 minCost $0 software

Why add a controller?

Talking directly to Ollama proves the model works. A controller gives us a place to manage history, memory, voice, permissions, and tools. Python is convenient here because its standard library can call the local Ollama API without adding another dependency.

Create jarvis.py

import json
import urllib.request

URL = "http://localhost:11434/api/chat"
MODEL = "lakehouse-assistant"

messages = []

while True:
    text = input("You> ").strip()
    if text.lower() in {"quit", "exit"}:
        break

    messages.append({"role": "user", "content": text})
    data = json.dumps({
        "model": MODEL,
        "messages": messages,
        "stream": False
    }).encode("utf-8")

    request = urllib.request.Request(
        URL,
        data=data,
        headers={"Content-Type": "application/json"},
        method="POST"
    )

    with urllib.request.urlopen(request) as response:
        result = json.loads(response.read().decode("utf-8"))

    answer = result["message"]["content"]
    messages.append({"role": "assistant", "content": answer})
    print(f"\nAssistant> {answer}")

Run it

python jarvis.py

If your custom model is named differently, change MODEL. If you stayed with the base model, use qwen3.5:2b.

You> Give me a two-line explanation of DNS.
Assistant> DNS translates human-readable domain names into IP addresses.
It lets applications find the servers that host a requested service.

What we gained

  • The model is now behind code that we control.
  • Conversation history is explicit.
  • We have a safe place to add memory and tools.
  • We can log real tool results instead of trusting the model to claim actions happened.
Checkpoint

python jarvis.py opens a conversation and receives responses through localhost:11434.

Finished this chapter?
Your progress is stored only in this browser.