pydantic_ai.models.ollama
Setup
For details on how to set up authentication with this model, see model configuration for Ollama.
Example local usage
With ollama
installed, you can run the server with the model you want to use:
ollama run llama3.2
llama3.2
model if you don't already have it downloaded)
Then run your code, here's a minimal example:
from pydantic import BaseModel
from pydantic_ai import Agent
class CityLocation(BaseModel):
city: str
country: str
agent = Agent('ollama:llama3.2', result_type=CityLocation)
result = agent.run_sync('Where were the olympics held in 2012?')
print(result.data)
#> city='London' country='United Kingdom'
print(result.cost())
#> Cost(request_tokens=57, response_tokens=8, total_tokens=65, details=None)
Example using a remote server
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.ollama import OllamaModel
ollama_model = OllamaModel(
model_name='qwen2.5-coder:7b', # (1)!
base_url='http://192.168.1.74:11434/v1', # (2)!
)
class CityLocation(BaseModel):
city: str
country: str
agent = Agent(model=ollama_model, result_type=CityLocation)
result = agent.run_sync('Where were the olympics held in 2012?')
print(result.data)
#> city='London' country='United Kingdom'
print(result.cost())
#> Cost(request_tokens=57, response_tokens=8, total_tokens=65, details=None)
- The name of the model running on the remote server
- The url of the remote server
See OllamaModel
for more information
CommonOllamaModelNames
module-attribute
CommonOllamaModelNames = Literal[
"codellama",
"gemma",
"gemma2",
"llama3",
"llama3.1",
"llama3.2",
"llama3.2-vision",
"llama3.3",
"mistral",
"mistral-nemo",
"mixtral",
"phi3",
"qwq",
"qwen",
"qwen2",
"qwen2.5",
"starcoder2",
]
This contains just the most common ollama models.
For a full list see ollama.com/library.
OllamaModelName
module-attribute
OllamaModelName = Union[CommonOllamaModelNames, str]
Possible ollama models.
Since Ollama supports hundreds of models, we explicitly list the most models but allow any name in the type hints.
OllamaModel
dataclass
Bases: Model
A model that implements Ollama using the OpenAI API.
Internally, this uses the OpenAI Python client to interact with the Ollama server.
Apart from __init__
, all methods are private or match those of the base class.
Source code in pydantic_ai_slim/pydantic_ai/models/ollama.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
__init__
__init__(
model_name: OllamaModelName,
*,
base_url: str | None = "http://localhost:11434/v1/",
openai_client: AsyncOpenAI | None = None,
http_client: AsyncClient | None = None
)
Initialize an Ollama model.
Ollama has built-in compatability for the OpenAI chat completions API (source), so we reuse the
OpenAIModel
here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_name
|
OllamaModelName
|
The name of the Ollama model to use. List of models available here
You must first download the model ( |
required |
base_url
|
str | None
|
The base url for the ollama requests. The default value is the ollama default |
'http://localhost:11434/v1/'
|
openai_client
|
AsyncOpenAI | None
|
An existing
|
None
|
http_client
|
AsyncClient | None
|
An existing |
None
|
Source code in pydantic_ai_slim/pydantic_ai/models/ollama.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
|