Description
Reacted by Venelin Valkov, Lucas V. F. Ventura, Luigi, kelvin, HuronExplodium, runhar, fLLMen, Georvic Tur, Leonid Shamis and swayson
Reacted by batrlatom, Martin Pierangeli, Paul Repin, Serge Sotnyk, Köksal KOTAN, Gustavo Bezerra, Ruben Sprengel (ITSC), Jannik Castenow (ITSC), Moritz Scherrmann, Hirotaka Kawashima and 19 more
Activity
Ollama is so you don't have to rely on vendors and can keep things local. So Ollama supports use-cases that require more security and locality.
Reacted by Paul Repin, Théo Foucher, Serge Sotnyk, Köksal KOTAN, Artur Barseghyan, Nicolas Gras, Gustavo Bezerra, Ruben Sprengel (ITSC), Hans Nielsen, Luc Stepniewski and 26 more
Agree with @abtawfik +
ollamais ideal for development. Keeping the costs low, less mocking, cheap testing.Reacted by Nicolas Gras, Luc Stepniewski, mk17, Scot, Zsolt Revay, Dang Manh Truong and hadjipantelis
I think this needed due to privacy/data security purposes, not model availability.
Lots of corporate environments completely block access to external AI/LLM services due to infosec concerns.Reacted by Nicolas Gras, Luc Stepniewski, James DiGioia, fLLMen, mk17 and Leonid Shamis
👍
@samuelcolvin, as others have stated, Ollama helps you run LLMs locally/privately. Please add support for it.Reacted by batrlatom, Artur Barseghyan, fLLMen, mk17, silverkiwi, Scot, Max Tesla, Leonid Shamis, Krzysztof Sibiński, erictbenson10 and 6 more
Reacted by Kenji Hikmatullah, Juan Pablo Tamayo, Leonid Shamis, Krzysztof Sibiński, Dustin Reagan, Tim and hadjipantelis
Okay, happy to support it, especially if we can reuse some of the openai integration.
PR welcome
Reacted by batrlatom, siddhant-middleware, Richie Caputo, Théo Foucher, Lorenzo Castellino, Daniel, kelvin, Artur Barseghyan, xns140, Ben O'Mahony and 5 more
Reacted by Sami Hamine, Zsolt Revay and Tanim Jalal
It is posible to use ollama right now.
Run ollama and set the AsyncOpenAI client with the ollama url "http://localhost:11434/v1"(if local), set the model name and that's all folks
from pydantic_ai import Agent from pydantic import BaseModel from openai import AsyncOpenAI from pydantic_ai.models.openai import OpenAIModel class CityLocation(BaseModel): city: str country: str client = AsyncOpenAI( base_url='http://localhost:11434/v1', api_key='your-api-key', ) model = OpenAIModel('qwen2.5-coder:latest', openai_client=client) agent = Agent(model,result_type=CityLocation) result = agent.run_sync('Where the olympics held in 2012?') print(result.data) #> city='London' country='United Kingdom' print(result.cost()) #> Cost(request_tokens=56, response_tokens=8, total_tokens=64, details=None)
Reacted by Richie Caputo, Samuel Colvin, Yannick Stephan, xns140, HuronExplodium, fLLMen, Ilkka-LBL, mk17, Brandon, pySilver and 9 more
Reacted by Evandro Neto, Théo Foucher, Nahum Rosa Cruz Sa, HuronExplodium, fLLMen, Hubix9, Ilkka-LBL, mk17, Brandon, Lucas Luz and 10 more
2 remaining items
I've tried running PydanticAI with Ollama, and it's awesome. Thanks @JojoJr24, I took your code and tweaked it slightly.
Really looking forward to a nice wrapper for running this without a custom
AsyncOpenAI.pydantic-ai-with-ollama-smaller.mov
Code
from pydantic_ai import Agent from pydantic import BaseModel from openai import AsyncOpenAI from pydantic_ai.models.openai import OpenAIModel from devtools import debug class CityLocation(BaseModel): city: str country: str client = AsyncOpenAI(base_url='http://localhost:11434/v1', api_key='-') model = OpenAIModel('llama3.2:latest', openai_client=client) agent = Agent(model, result_type=CityLocation) result = agent.run_sync('Where the olympics held in 2012?') debug(result.data) debug(result.cost())
Reacted by mk17
Reacted by Ben O'Mahony, Sujith S and FabEng
Reacted by Sujith S, Juan Pablo Tamayo and FabEng
Hey @samuelcolvin @benomahony ,
I actually just raised a PR for this here (sorry, I didn't see the message saying you were going to try it!). There are a few questions I have put in the PR description, but otherwise I think it works OK
No drama @cal859! Will take a peek tomorrow but if this is solved then I'll see if I can pick up something else! 🤣
Reacted by mk17, cal859 and Scot
For future folks who land here. An example using the new OllamaModel wrapper with a remote ollama service.
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', base_url='http://192.168.1.74:11434/v1' ) class CityLocation(BaseModel): city: str country: str agent = Agent(model=ollama_model, result_type=CityLocation) result = agent.run_sync('In what city and country were the olympics held in 2012?') print(result.data) #> city='London' country='United Kingdom' print(result.cost()) #> Cost(request_tokens=56, response_tokens=8, total_tokens=64, details=None)
Reacted by Artur Barseghyan, Juan Manuel Cristóbal Moreno, Isaac Salvador and rajanski
Thanks @frodopwns. Was this not obvious from the docs / examples? I can see it is maybe not that obvious from the example I added here.
@samuelcolvin Should I create a small PR adding the example @frodopwns has shared to the docs and/or updating the existing docs to make the example more clear for how to work with remote servers?
@cal859 Thanks adding the example! I think it was a little confusing for a newcomer to the project to figure out just how to set the
base_url. ChatGPT knew how to do it though so it must be made clear somewhere I didn't find. Lazy Googling on my part most likely!For future folks who land here. An example using the new OllamaModel wrapper with a remote ollama service.
from pydantic import BaseModel
from pydantic_ai import Agent
from pydantic_ai.models.ollama import OllamaModelollama_model = OllamaModel(
model_name='qwen2.5-coder:7b',
base_url='http://192.168.1.74:11434/v1'
)class CityLocation(BaseModel):
city: str
country: stragent = Agent(model=ollama_model, result_type=CityLocation)
result = agent.run_sync('In what city and country were the olympics held in 2012?')
print(result.data)
#> city='London' country='United Kingdom'
print(result.cost())
#> Cost(request_tokens=56, response_tokens=8, total_tokens=64, details=None)There is no module name 'pydantic_ai.models.ollama'
from pydantic_ai.models.ollama import OllamaModel ModuleNotFoundError: No module named 'pydantic_ai.models.ollama'Reacted by Dang Manh Truong
@benomahony what are you talking about?
base_url: str | None = None, For me, the following worked
`
from pydantic_ai.models.openai import OpenAIModel
from pydantic_ai.providers.openai import OpenAIProvider
from pydantic_ai import Agent, RunContext
from httpx import AsyncClientcustom_http_client = AsyncClient(timeout=30)
the_model = OpenAIModel('qwen3:latest', provider=OpenAIProvider(base_url='http://localhost:11434/v1/', http_client=custom_http_client))
agent = Agent(model=the_model, system_prompt=['Reply in one sentence'])
response = agent.run_sync('What is the capital of Japan?')
print(response.data)`
I hope that helps