← all conversations

Using Ollama with Agents

2025-03-133 turns7,445 charsgpt-4o, o3-mini1 fork(s)
ollamaopenai-agents-sdkllm-integration

Summary

User wants to modify an OpenAI Agents SDK Python script to use non-OpenAI models via Ollama.

Messages

⑂ thread forked here
answer the final question: I have a noob question on the newly released OpenAI Agents SDK. In the Python script below (obtained from https://openai.com/index/new-tools-for-building-agents/) how do modify the script below to use non-OpenAI models? Would greatly appreciate any help on this! from agents import Agent, Runner, WebSearchTool, function_tool, guardrail @function_tool def submit_refund_request(item_id: str, reason: str): # Your refund logic goes here return "success" support_agent = Agent( name="Support & Returns", instructions="You are a support agent who can submit refunds [...]", tools=[submit_refund_request], ) shopping_agent = Agent( name="Shopping Assistant", instructions="You are a shopping assistant who can search the web [...]", tools=[WebSearchTool()], ) triage_agent = Agent( name="Triage Agent", instructions="Route the user to the correct agent.", handoffs=[shopping_agent, support_agent], ) output = Runner.run_sync( starting_agent=triage_agent, input="What shoes might work best with my outfit so far?", ) Upvote 4 Downvote 8 Go to comments Share Share Comments Section Single comment thread See full discussion u/KonradFreeman avatar KonradFreeman • 10h ago The adapter processes responses from the Ollama model, looking for tool usage instructions and formatting them in a way the Agents SDK expects. Without this adapter, the Ollama client wouldn't be able to properly support tool calling within the Agents SDK framework. The adapter is implicitly used when the `OllamaClient` is passed to the `Agent` constructor: agent = Agent( ollama_client, tools=[add_numbers], instructions=INSTRUCTIONS ) When you create an `Agent` with the `ollama_client`, the SDK internally uses the adapter that was registered with that client. The adapter is registered through this line: agent_adapter = OllamaAgentAdapter() agent_adapter.register(ollama_client) The registration process associates the adapter with the client, so when the client is used in the Agent, the adapter is implicitly employed to process responses. This design pattern follows dependency injection principles, where the adapter's functionality is added to the client without needing to reference it directly in subsequent code. Comment Image Upvote 2 Downvote Reply reply redd-dev OP • 9h ago Great thanks! So say if I wanted to explicitly specify the use of the adaptor when the “OllamaClient” is passed to the “Agent” constructor, will it look something like the below: agent = Agent( ollama_client, tools=[add_numbers], instructions=INSTRUCTIONS, agent_adapter = OllamaAgentAdapter(), agent_adapter.register(ollama_client) ) Upvote 2 Downvote Reply reply u/KonradFreeman avatar KonradFreeman • 9h ago No, that would not work. This is the correct way: # First register the adapter with the client agent_adapter = OllamaAgentAdapter() agent_adapter.register(ollama_client) # Then create the agent with the client agent = Agent( ollama_client, tools=[add_numbers], instructions=INSTRUCTIONS ) Registration of the adapter needs to be done separately before creating the Agent. Comment Image Upvote 2 Downvote Reply reply redd-dev OP • 6h ago Ok thanks. Do you happen to know where the OpenAI documentation is which describes this “agent_adapter” parameter?
To modify the script to use non-OpenAI models (like **Ollama**), you need to replace the OpenAI agent instantiation with an **OllamaClient** and register an adapter for it. Here's how you can do it: ### **Modified Script Using Ollama** ```python from agents import Agent, Runner, WebSearchTool, function_tool, guardrail from ollama import OllamaClient from ollama_adapter import OllamaAgentAdapter # Assuming an adapter exists @function_tool def submit_refund_request(item_id: str, reason: str): return "success" # Initialize Ollama client ollama_client = OllamaClient(model="your-local-model-name") # Specify your model # Register the adapter with the client agent_adapter = OllamaAgentAdapter() agent_adapter.register(ollama_client) # Define the agents with the Ollama client support_agent = Agent( ollama_client, tools=[submit_refund_request], instructions="You are a support agent who can submit refunds [...]", ) shopping_agent = Agent( ollama_client, tools=[WebSearchTool()], instructions="You are a shopping assistant who can search the web [...]", ) triage_agent = Agent( ollama_client, handoffs=[shopping_agent, support_agent], instructions="Route the user to the correct agent.", ) # Run the agent pipeline output = Runner.run_sync( starting_agent=triage_agent, input="What shoes might work best with my outfit so far?", ) print(output) ``` ### **Key Modifications** 1. **Added `OllamaClient`** – Replaces OpenAI's API. 2. **Registered `OllamaAgentAdapter`** – Ensures compatibility with OpenAI’s Agents SDK. 3. **Passed `ollama_client` to each `Agent`** – Instead of OpenAI's built-in models. If you need help setting up **OllamaAgentAdapter**, let me know! 🚀
gpt-4o
There isn’t a separate, dedicated “agent_adapter” section in the official documentation. Instead, the adapter’s functionality is explained implicitly through the examples and the SDK’s design. To use non-OpenAI models (like those provided by Ollama), you need to do two things: 1. **Instantiate your non-OpenAI client:** For example, create an instance of an `OllamaClient` (or another client for the model you wish to use). 2. **Register the appropriate adapter with your client:** You must register the adapter (e.g., `OllamaAgentAdapter`) with your client *before* you create your Agent. Once registered, the SDK automatically uses the adapter’s functionality when processing responses. You don’t pass an adapter as a parameter to the `Agent` constructor. Here’s a brief example outlining the proper sequence: ```python from agents import Agent, Runner, WebSearchTool, function_tool from agents.clients import OllamaClient # Hypothetical import for the client from agents.adapters import OllamaAgentAdapter # Hypothetical import for the adapter # Create your non-OpenAI client ollama_client = OllamaClient(model="llama2") # Register the adapter with the client agent_adapter = OllamaAgentAdapter() agent_adapter.register(ollama_client) # Now create the Agent with the client agent = Agent( ollama_client, tools=[submit_refund_request], # example tool instructions="You are a support agent who can submit refunds [...]" ) ``` This pattern follows a dependency injection approach: the adapter’s functionality is added to the client during registration, so when you pass the client to the Agent, it automatically handles tool calls and response formatting. Regarding the documentation for the “agent_adapter” parameter: OpenAI hasn’t provided a dedicated documentation page solely for that parameter. Instead, its use is demonstrated in examples (like the one above) and within the SDK’s source code. For further details, you might check the SDK’s GitHub repository or related blog posts—these are currently the best resources for understanding how the adapter mechanism works. In short, the adapter is not a parameter you pass into the Agent constructor; it’s something you register with your client beforehand, and its behavior is covered by the examples and the SDK’s implementation rather than through separate official docs.
o3-mini