models.ollama

class agentopera.models.ollama.OllamaChatCompletionClient(**kwargs: Unpack)[source]

Bases: BaseOllamaChatCompletionClient

Chat completion client for Ollama hosted models.

Ollama must be installed and the appropriate model pulled.

Parameters:
  • model (str) – Which Ollama model to use.

  • host (optional, str) – Model host url.

  • response_format (optional, pydantic.BaseModel) – The format of the response. If provided, the response will be parsed into this format as json.

  • options (optional, Mapping[str, Any] | Options) – Additional options to pass to the Ollama client.

  • model_info (optional, ModelInfo) – The capabilities of the model. Required if the model is not listed in the ollama model info.

Note

Only models with 200k+ downloads (as of Jan 21, 2025), + phi4, deepseek-r1 have pre-defined model infos. See this file for the full list. An entry for one model encompases all parameter variants of that model.

To use this client, you must install the ollama extension:

pip install "agentopera[ollama]"

The following code snippet shows how to use the client with an Ollama model:

from agentopera.models.ollama import OllamaChatCompletionClient
from agentopera.core.types.models import UserMessage

ollama_client = OllamaChatCompletionClient(
    model="llama3",
)

result = await ollama_client.create([UserMessage(content="What is the capital of France?", source="user")])  # type: ignore
print(result)

To load the client from a configuration, you can use the load_component method:

from agentopera.core.types.models import ChatCompletionClient

config = {
    "provider": "OllamaChatCompletionClient",
    "config": {"model": "llama3"},
}

client = ChatCompletionClient.load_component(config)

To output structured data, you can use the response_format argument:

from agentopera.models.ollama import OllamaChatCompletionClient
from agentopera.core.types.models import UserMessage
from pydantic import BaseModel


class StructuredOutput(BaseModel):
    first_name: str
    last_name: str


ollama_client = OllamaChatCompletionClient(
    model="llama3",
    response_format=StructuredOutput,
)
result = await ollama_client.create([UserMessage(content="Who was the first man on the moon?", source="user")])  # type: ignore
print(result)

Note

Tool usage in ollama is stricter than in its OpenAI counterparts. While OpenAI accepts a map of [str, Any], Ollama requires a map of [str, Property] where Property is a typed object containing type and description fields. Therefore, only the keys type and description will be converted from the properties blob in the tool schema.

To view the full list of available configuration options, see the OllamaClientConfigurationConfigModel class.

component_type = 'model'
component_config_schema

alias of BaseOllamaClientConfigurationConfigModel

component_provider_override = 'agentopera.agents.models.ollama.OllamaChatCompletionClient'
class agentopera.models.ollama.BaseOllamaClientConfigurationConfigModel(*, model: str, host: str | None = None, response_format: Any = None, follow_redirects: bool = True, timeout: Any = None, headers: Mapping[str, str] | None = None, model_capabilities: ModelCapabilities | None = None, model_info: ModelInfo | None = None, options: Mapping[str, Any] | Options | None = None)[source]

Bases: CreateArgumentsConfigModel

follow_redirects: bool
timeout: Any
headers: Mapping[str, str] | None
model_capabilities: ModelCapabilities | None
model_info: ModelInfo | None
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

options: Mapping[str, Any] | Options | None
class agentopera.models.ollama.CreateArgumentsConfigModel(*, model: str, host: str | None = None, response_format: Any = None)[source]

Bases: BaseModel

model: str
host: str | None
response_format: Any
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].