-
Notifications
You must be signed in to change notification settings - Fork 94
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b4d763b
update for plugins
jssmith ef7a00d
formatting
jssmith 38375ca
reference main branch
jssmith 92286fd
cleanup
jssmith 1b6b026
switch to plugins on the runners
jssmith c157941
move around samples
jssmith 815c14b
update README files
jssmith 6c9213f
formatting update
jssmith 923a919
formatting
jssmith 396fb5c
timeout adjustments
jssmith b946e41
litellm model provider
jssmith 73d7071
Merge remote-tracking branch 'origin/main' into jssmith/openai-model-…
tconley1428 2b64207
Merge branch 'main' into jssmith/openai-model-providers
tconley1428 0284ce5
Format
tconley1428 763d8ff
Merge branch 'jssmith/openai-model-providers' of https://github.com/t…
tconley1428 e5dc6c1
Merge branch 'main' into jssmith/openai-model-providers
tconley1428 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 |
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
| 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()) |
39 changes: 39 additions & 0 deletions
39
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,39 @@ | ||
| 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()) |
25 changes: 25 additions & 0 deletions
25
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,25 @@ | ||
| 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