-
Notifications
You must be signed in to change notification settings - Fork 79
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
jssmith
wants to merge
1
commit into
jssmith/openai-samples-reorg
Choose a base branch
from
jssmith/openai-model-providers
base: jssmith/openai-samples-reorg
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
openai_agents/model_providers/run_litellm_auto_workflow.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
openai_agents/model_providers/run_worker_litellm_provider.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
26 changes: 26 additions & 0 deletions
26
openai_agents/model_providers/workflows/litellm_auto_workflow.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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