Support `ollama`

· GitHub ·

6 min read Original article ↗

Description

@barseghyanartur

Activity

  1. samuelcolvin commented on Dec 2, 2024

    @samuelcolvin

  2. abtawfik commented on Dec 2, 2024

    @abtawfik

    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

  3. barseghyanartur commented on Dec 2, 2024

    @barseghyanartur

    Agree with @abtawfik + ollama is 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

  4. gusutabopb commented on Dec 3, 2024

    @gusutabopb

    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

  5. Lambda-Logan commented on Dec 3, 2024

    @Lambda-Logan

    Yes, this is unusable for us without support for totally private & local usage via ollama.

    My company cannot use any network call to ai for analysis of code written in house because of security.

    Reacted by mk17 and Dustin Reagan

  6. curiousily commented on Dec 3, 2024

    @curiousily

    👍
    @samuelcolvin, as others have stated, Ollama helps you run LLMs locally/privately. Please add support for it.

  7. Lambda-Logan commented on Dec 3, 2024

    @Lambda-Logan

    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

  8. arcaputo3 commented on Dec 3, 2024

    @arcaputo3

  9. samuelcolvin commented on Dec 4, 2024

    @samuelcolvin

    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

  10. JojoJr24 commented on Dec 4, 2024

    @JojoJr24

    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

  11. JojoJr24 commented on Dec 4, 2024

    @JojoJr24

    The formated response work well with qwen 2.5 coder models, even with 7b.
    Other models like mistral-nemo fails.

  12. HuronExplodium commented on Dec 5, 2024

    @HuronExplodium

  13. benomahony commented on Dec 5, 2024

    @benomahony

    I'd be happy to give this a crack this weekend unless someone else is already cracking on?

    Reacted by Samuel Colvin, David Montague and Fabian Nonnenmacher

  14. 2 remaining items

  15. pySilver commented on Dec 6, 2024

    @pySilver

  16. samuelcolvin commented on Dec 6, 2024

    @samuelcolvin

    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

  17. cal859 commented on Dec 6, 2024

    @cal859

    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

  18. benomahony commented on Dec 6, 2024

    @benomahony

    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

  19. frodopwns commented on Dec 10, 2024

    @frodopwns

    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

  20. cal859 commented on Dec 10, 2024

    @cal859

    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?

  21. samuelcolvin commented on Dec 10, 2024

    @samuelcolvin

    happy to add another example.

    Reacted by Artur Barseghyan

  22. cal859 commented on Dec 10, 2024

    @cal859

  23. frodopwns commented on Dec 11, 2024

    @frodopwns

    @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!

  24. Badhansen commented on Feb 5, 2025

    @Badhansen

    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)

    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

  25. benomahony commented on Feb 5, 2025

    @benomahony

  26. samuelcolvin commented on Feb 5, 2025

    @samuelcolvin

    @benomahony what are you talking about?

    base_url: str | None = None,
  27. benomahony commented on Feb 5, 2025

    @benomahony

    I'm talking absolute nonsense apparently! I blame my lsp earlier 🤦

  28. dpappas commented on Jul 24, 2025

    @dpappas

    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 AsyncClient

    custom_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