M13 solution
The expected, fully-commented artifact for M13's lab. Peek only after you've tried.
| File | What it is |
|---|---|
chat_local.py |
Chats with an open model running locally via Ollama's local API (POST localhost:11434/api/chat), same shape as M4's hosted call, but no API key, offline, and free. Handles "Ollama not running" gracefully. |
There's no .env here on purpose, local models need no key.
Run it
Install Ollama and pull a small model (see the Ollama guide), then with your venv active:
pip install requests
python chat_local.py
How this was verified
Verified on Python 3 with the HTTP call mocked (no running Ollama needed):
- compiles;
- the client posts to http://localhost:11434/api/chat with the right body (model, messages,
stream: false) and correctly parses the reply from message.content, confirmed by inspecting
what the mocked requests.post received and returning Ollama's documented response shape;
- the "Ollama not running" path (ConnectionError) is handled with a clear message, no crash.
Ollama itself wasn't run here (the build sandbox has no Ollama daemon or downloaded model), > the install +
ollama run+ live local generation must be piloted on a real machine. The code follows Ollama's documented local API. No API key or billed call was used (local models are free).