Skip to content

OpenAI Agents Third-Party model providers #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: jssmith/openai-samples-reorg
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions openai_agents/model_providers/README.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left two comments at #226 that apply here as well

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Model Providers Examples

Custom LLM provider integration examples for OpenAI Agents SDK with Temporal workflows.

*Adapted from [OpenAI Agents SDK model providers examples](https://github.com/openai/openai-agents-python/tree/main/examples/model_providers)*

Before running these examples, be sure to review the [prerequisites and background on the integration](../README.md).

## Running the Examples

### Currently Implemented

#### LiteLLM Auto
Uses built-in LiteLLM support to connect to various model providers.

Start the LiteLLM provider worker:
```bash
# Set the required environment variable for your chosen provider
export ANTHROPIC_API_KEY="your_anthropic_api_key" # For Anthropic

uv run openai_agents/model_providers/run_worker_litellm_provider.py
```

Then run the example in a separate terminal:
```bash
uv run openai_agents/model_providers/run_litellm_auto_workflow.py
```

The example uses Anthropic Claude by default but can be modified to use other LiteLLM-supported providers.

Find more LiteLLM providers at: https://docs.litellm.ai/docs/providers

## Not Yet Implemented

- **Custom Example Agent** - Custom OpenAI client integration
- **Custom Example Global** - Global default client configuration
- **Custom Example Provider** - Custom ModelProvider pattern
- **LiteLLM Provider** - Interactive model/API key input
27 changes: 27 additions & 0 deletions openai_agents/model_providers/run_litellm_auto_workflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import asyncio

from temporalio.client import Client
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin

from openai_agents.model_providers.workflows.litellm_auto_workflow import LitellmAutoWorkflow


async def main():
client = await Client.connect(
"localhost:7233",
plugins=[
OpenAIAgentsPlugin(),
],
)

result = await client.execute_workflow(
LitellmAutoWorkflow.run,
"What's the weather in Tokyo?",
id="litellm-auto-workflow-id",
task_queue="openai-agents-model-providers-task-queue",
)
print(f"Result: {result}")


if __name__ == "__main__":
asyncio.run(main())
37 changes: 37 additions & 0 deletions openai_agents/model_providers/run_worker_litellm_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import asyncio
from datetime import timedelta

from agents.extensions.models.litellm_provider import LitellmProvider
from temporalio.client import Client
from temporalio.contrib.openai_agents import ModelActivityParameters, OpenAIAgentsPlugin
from temporalio.worker import Worker

from openai_agents.model_providers.workflows.litellm_auto_workflow import LitellmAutoWorkflow


async def main():
# Create client connected to server at the given address
client = await Client.connect(
"localhost:7233",
plugins=[
OpenAIAgentsPlugin(
model_params=ModelActivityParameters(
start_to_close_timeout=timedelta(seconds=30)
),
model_provider=LitellmProvider(),
),
],
)

worker = Worker(
client,
task_queue="openai-agents-model-providers-task-queue",
workflows=[
LitellmAutoWorkflow,
],
)
await worker.run()


if __name__ == "__main__":
asyncio.run(main())
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from __future__ import annotations


from agents import Agent, Runner, function_tool, set_tracing_disabled
from temporalio import workflow


@workflow.defn
class LitellmAutoWorkflow:
@workflow.run
async def run(self, prompt: str) -> str:
set_tracing_disabled(disabled=True)

@function_tool
def get_weather(city: str):
return f"The weather in {city} is sunny."

agent = Agent(
name="Assistant",
instructions="You only respond in haikus.",
model="anthropic/claude-3-5-sonnet-20240620",
tools=[get_weather],
)

result = await Runner.run(agent, prompt)
return result.final_output
Loading