trader.llm.ollama module

Ollama over its HTTP API.

Verified against Ollama 0.32.5 with qwen2.5:32b on 2026-07-31: passing a JSON Schema as format to POST /api/chat returns exactly the requested keys with enums respected — no prose wrapper, no code fence. That is a decode-time constraint rather than a polite request, and it is why this design does not need a “strip the markdown fence” step.

Stdlib `urllib` on purpose. Adding httpx for one POST would be a new runtime dependency, and leaning on requests — present only transitively via yfinance — would be worse.

class trader.llm.ollama.OllamaProvider(*, model, base_url='http://localhost:11434', timeout_seconds=120, temperature=0.0, seed=7, num_ctx=4096)[source]

Bases: object

Talks to a local Ollama server.

Parameters:
  • model (str)

  • base_url (str)

  • timeout_seconds (int)

  • temperature (float)

  • seed (int)

  • num_ctx (int)

generate_json(system, user, schema)[source]

Ask the model and return parsed JSON matching schema.

Parameters:
  • system (str)

  • user (str)

  • schema (dict[str, object])

Return type:

dict[str, object]

health()[source]

Round-trip a trivial prompt. Returns the model name.

Return type:

str