trading_agent.backends.ollama_backend
¶
A backend that talks to a model on a local Ollama server.
It uses Ollama's own /api/chat endpoint through the ollama package, because only that endpoint lets each request set the context size (num_ctx) and turn thinking on or off.
Typical usage example:
backend = OllamaBackend('gemma', backend_configuration) reply = await backend.complete(conversation, tool_definitions, on_text)
OllamaBackend
¶
Bases: ModelBackend
One model on one Ollama server.
Creates the backend without contacting the server.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The backend's name in trading_agent.toml. |
required |
backend_configuration
|
BackendConfiguration
|
Its settings, of kind "ollama". |
required |
Source code in src/trading_agent/backends/ollama_backend.py
thinking
¶
Whether the model thinks before answering.
Returns:
| Type | Description |
|---|---|
str | None
|
"on" or "off". |
set_thinking
¶
Turns thinking off for "off", on for any other level, or back to trading_agent.toml for None.
Ollama's thinking is on or off for most models, so the levels between low and max all mean on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str | None
|
One of turn_runner.THINKING_LEVELS, or None for the configured setting. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
Always True. |
Source code in src/trading_agent/backends/ollama_backend.py
complete
async
¶
Streams one reply from Ollama.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conversation
|
Conversation
|
The conversation so far. |
required |
tool_definitions
|
list[dict[str, Any]]
|
The tools the model may call. |
required |
on_text
|
Callable[[str], None]
|
Called with each piece of text as it streams in. |
required |
Returns:
| Type | Description |
|---|---|
ModelReply
|
The complete reply. |
Raises:
| Type | Description |
|---|---|
BackendUnavailableError
|
Ollama cannot be reached, or refused the request. |