ollama
ollama
¶
Ollama backend for local model execution with MCP tool support.
Enables Marianne to use local Ollama models with translated MCP tool schemas. Implements the Backend protocol with an agentic loop for multi-turn tool calling.
Architecture Decision: ADR-001 specifies MCPProxyService as in-process subprocess manager, not a separate proxy process. This backend integrates with it for tool execution during the agentic loop.
Classes¶
OllamaFunctionDef
¶
Bases: TypedDict
Function definition within an Ollama tool.
OllamaToolDef
¶
Bases: TypedDict
Ollama tool definition (OpenAI-style function calling format).
OllamaRawFunction
¶
Bases: TypedDict
Raw function payload inside a tool call from Ollama response.
OllamaRawToolCall
¶
Bases: TypedDict
Raw tool call object from Ollama chat response.
OllamaChatResponse
¶
Bases: TypedDict
Top-level Ollama /api/chat response shape.
OllamaChatOptions
¶
Bases: TypedDict
Options sub-object for Ollama chat requests.
OllamaChatRequest
¶
Bases: TypedDict
Request payload for Ollama /api/chat endpoint.
OllamaMessage
dataclass
¶
OllamaBackend
¶
OllamaBackend(base_url='http://localhost:11434', model='llama3.1:8b', timeout=300.0, num_ctx=32768, keep_alive='5m', max_tool_iterations=10, mcp_proxy=None)
Bases: HttpxClientMixin, Backend
Backend for Ollama model execution with tool translation.
Implements the Backend protocol for local Ollama models. Supports: - MCP tool schema translation to Ollama function format - Multi-turn agentic loop for tool calling - Health checks via /api/tags endpoint
Example usage
backend = OllamaBackend( base_url="http://localhost:11434", model="llama3.1:8b", ) result = await backend.execute("Write a hello world function")
Initialize Ollama backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_url
|
str
|
Ollama server URL (default: http://localhost:11434) |
'http://localhost:11434'
|
model
|
str
|
Model to use (must support tool calling) |
'llama3.1:8b'
|
timeout
|
float
|
Request timeout in seconds |
300.0
|
num_ctx
|
int
|
Context window size (recommend >= 32768 for Claude Code tools) |
32768
|
keep_alive
|
str
|
Keep model loaded duration (e.g., "5m", "1h") |
'5m'
|
max_tool_iterations
|
int
|
Maximum tool call iterations per execution |
10
|
mcp_proxy
|
MCPProxyService | None
|
Optional MCPProxyService for tool execution |
None
|
Source code in src/marianne/backends/ollama.py
Attributes¶
Functions¶
from_config
classmethod
¶
Create backend from configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
BackendConfig
|
Backend configuration with ollama settings |
required |
Returns:
| Type | Description |
|---|---|
OllamaBackend
|
Configured OllamaBackend instance |
Source code in src/marianne/backends/ollama.py
set_preamble
¶
set_prompt_extensions
¶
execute
async
¶
Execute a prompt and return the result.
Runs the agentic loop if tools are available via MCPProxyService, otherwise performs a simple completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The prompt to send to Ollama |
required |
timeout_seconds
|
float | None
|
Per-call timeout override. Ollama uses the httpx
client-level timeout from |
None
|
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
ExecutionResult with output and metadata |
Source code in src/marianne/backends/ollama.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | |
health_check
async
¶
Check if Ollama is available and model is loaded.
Uses /api/tags to verify Ollama is running and configured model exists.
Returns:
| Type | Description |
|---|---|
bool
|
True if healthy, False otherwise |