6OllamaProvider — :class:`ProviderPlugin` implementation for Ollama.
10Connects OC Chat to a local (or LAN) Ollama daemon via the native
11``/api/chat`` endpoint. No API key required. Base URL defaults to
12``http://localhost:11434`` and is overridable via ``OLLAMA_BASE_URL``.
16- :class:`OllamaProvider` — implement every abstract method on
17 :class:`ProviderPlugin` for the Ollama provider.
18- :meth:`build_client` — return an :class:`OllamaClient`.
19- :meth:`models` / :meth:`tier` — surface the static registries.
20- :meth:`build_extra_body` — always ``None`` (no thinking payload).
21- :meth:`transform_outgoing_messages` — pass-through (no sentinel).
22- :meth:`parse_usage` — read ``prompt_eval_count`` / ``eval_count``
23 from the Ollama-shaped usage dict yielded by :class:`OllamaClient`.
24- :meth:`fetch_live_models` — delegate to ``client.list_models()``.
25- :meth:`default_caps` — permissive local defaults for any live model
26 not in the curated registry.
38- ``api_key_env_var = ""`` — Ollama does not use API keys.
39- ``transform_outgoing_messages`` is a true pass-through (no sentinel
40 embedding — Ollama has no disk-based prefix cache that benefits from
42- ``parse_usage`` handles both the Ollama-native field names
43 (``prompt_eval_count`` / ``eval_count``) and the normalised names
44 (``prompt_tokens`` / ``completion_tokens``) so it works regardless of
45 which shape the caller passes.
49- oc_diagnostics (_dbg)
50- providers.ollama.client (OllamaClient)
51- providers.ollama.models (MODELS, TIER_MAP)
52- option_c_agent_contracts (ProviderPlugin base class and the ModelCaps, ModelInfo, PerformanceStats, UsageStats and BaseClient types)
54from __future__
import annotations
56from oc_diagnostics
import _dbg
58from option_c_agent_contracts
import (
67from .client
import OllamaClient
68from .models
import MODELS, TIER_MAP
72 """Ollama LLM provider — native /api/chat, no API key."""
77 base_url_env_var =
"OLLAMA_BASE_URL"
78 default_base_url =
"http://localhost:11434"
79 default_model =
"llama3.2"
85 _dbg(
"OC_CHAT.CLIENT", func, f
"OllamaProvider base_url={base_url!r}", 3)
90 def models(self) -> dict[str, ModelCaps]:
94 return client.list_models()
98 supports_thinking=
False,
99 supports_reasoning=
False,
101 max_context_tokens=8_192,
106 def tier(self, name: str) -> str |
None:
107 return TIER_MAP.get(name)
115 enable_thinking: bool,
122 messages: list[dict],
123 tool_schemas: list[dict] |
None,
130 from option_c_agent_contracts
import GenerationOptions
132 if opts.temperature
is not None: d[
"temperature"] = opts.temperature
133 if opts.top_p
is not None: d[
"top_p"] = opts.top_p
134 if opts.top_k
is not None: d[
"top_k"] = opts.top_k
135 if opts.num_predict
is not None: d[
"num_predict"] = opts.num_predict
136 if opts.repeat_penalty
is not None: d[
"repeat_penalty"] = opts.repeat_penalty
137 if opts.seed
is not None: d[
"seed"] = opts.seed
138 return {
"options": d}
if d
else None
141 return PerformanceStats(
142 prompt_eval_count=chunk.get(
"prompt_eval_count", 0),
143 prompt_eval_duration_ns=chunk.get(
"prompt_eval_duration", 0),
144 eval_count=chunk.get(
"eval_count", 0),
145 eval_duration_ns=chunk.get(
"eval_duration", 0),
146 total_duration_ns=chunk.get(
"total_duration", 0),
149 def model_info(self, model: str, client: BaseClient) -> ModelInfo |
None:
152 data = client.show_model(model)
153 except Exception
as exc:
154 _dbg(
"OC_CHAT.CLIENT", func, f
"show_model failed: {exc}", 2)
156 details = data.get(
"details")
or {}
159 family=details.get(
"family",
""),
160 parameter_count=details.get(
"parameter_size",
""),
161 quantization=details.get(
"quantization_level",
""),
162 context_window=details.get(
"context_length", 0),
169 if not isinstance(usage, dict):
173 usage.get(
"prompt_eval_count")
174 or usage.get(
"prompt_tokens")
178 usage.get(
"eval_count")
179 or usage.get(
"completion_tokens")
182 total = int(usage.get(
"total_tokens")
or 0)
or (prompt + completion)
184 prompt_tokens=prompt,
185 completion_tokens=completion,
list[str] fetch_live_models(self, BaseClient client)
str|None tier(self, str name)
list[dict] transform_outgoing_messages(self, list[dict] messages, list[dict]|None tool_schemas)
PerformanceStats parse_performance(self, dict chunk)
dict|None translate_generation_options(self, "GenerationOptions" opts)
UsageStats parse_usage(self, dict usage)
BaseClient build_client(self, str api_key, str base_url)
ModelCaps default_caps(self)
ModelInfo|None model_info(self, str model, BaseClient client)
dict|None build_extra_body(self, str model, *, bool enable_thinking, str|None effort)