-
Notifications
You must be signed in to change notification settings - Fork 94
Reorganize OpenAI samples #221
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
12 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 d9b03d3
Merge remote-tracking branch 'origin/main' into jssmith/openai-sample…
tconley1428 500b124
Reset uv.lock
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
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,68 @@ | ||
| # Agent Patterns | ||
|
|
||
| Common agentic patterns extended with Temporal's durable execution capabilities. | ||
|
|
||
| *Adapted from [OpenAI Agents SDK agent patterns](https://github.com/openai/openai-agents-python/tree/main/examples/agent_patterns)* | ||
|
|
||
| ## Running the Examples | ||
|
|
||
| First, start the worker (supports all patterns): | ||
| ```bash | ||
| uv run openai_agents/agent_patterns/run_worker.py | ||
| ``` | ||
|
|
||
| Then run individual examples in separate terminals: | ||
|
|
||
| ## Deterministic Flows | ||
|
|
||
| **TODO** | ||
|
|
||
| A common tactic is to break down a task into a series of smaller steps. Each task can be performed by an agent, and the output of one agent is used as input to the next. For example, if your task was to generate a story, you could break it down into the following steps: | ||
|
|
||
| 1. Generate an outline | ||
| 2. Generate the story | ||
| 3. Generate the ending | ||
|
|
||
| Each of these steps can be performed by an agent. The output of one agent is used as input to the next. | ||
|
|
||
| ## Handoffs and Routing | ||
|
|
||
| **TODO** | ||
|
|
||
| In many situations, you have specialized sub-agents that handle specific tasks. You can use handoffs to route the task to the right agent. | ||
|
|
||
| For example, you might have a frontline agent that receives a request, and then hands off to a specialized agent based on the language of the request. | ||
|
|
||
| ## Agents as Tools | ||
|
|
||
| The mental model for handoffs is that the new agent "takes over". It sees the previous conversation history, and owns the conversation from that point onwards. However, this is not the only way to use agents. You can also use agents as a tool - the tool agent goes off and runs on its own, and then returns the result to the original agent. | ||
|
|
||
| For example, you could model a translation task as tool calls instead: rather than handing over to the language-specific agent, you could call the agent as a tool, and then use the result in the next step. This enables things like translating multiple languages at once. | ||
|
|
||
| ```bash | ||
| uv run openai_agents/agent_patterns/run_agents_as_tools_workflow.py | ||
| ``` | ||
|
|
||
| ## LLM-as-a-Judge | ||
|
|
||
| **TODO** | ||
|
|
||
| LLMs can often improve the quality of their output if given feedback. A common pattern is to generate a response using a model, and then use a second model to provide feedback. You can even use a small model for the initial generation and a larger model for the feedback, to optimize cost. | ||
|
|
||
| For example, you could use an LLM to generate an outline for a story, and then use a second LLM to evaluate the outline and provide feedback. You can then use the feedback to improve the outline, and repeat until the LLM is satisfied with the outline. | ||
|
|
||
| ## Parallelization | ||
|
|
||
| **TODO** | ||
|
|
||
| Running multiple agents in parallel is a common pattern. This can be useful for both latency (e.g. if you have multiple steps that don't depend on each other) and also for other reasons e.g. generating multiple responses and picking the best one. | ||
|
|
||
| ## Guardrails | ||
|
|
||
| **TODO** | ||
|
|
||
| Related to parallelization, you often want to run input guardrails to make sure the inputs to your agents are valid. For example, if you have a customer support agent, you might want to make sure that the user isn't trying to ask for help with a math problem. | ||
|
|
||
| You can definitely do this without any special Agents SDK features by using parallelization, but we support a special guardrail primitive. Guardrails can have a "tripwire" - if the tripwire is triggered, the agent execution will immediately stop and a `GuardrailTripwireTriggered` exception will be raised. | ||
|
|
||
| This is really useful for latency: for example, you might have a very fast model that runs the guardrail and a slow model that runs the actual agent. You wouldn't want to wait for the slow model to finish, so guardrails let you quickly reject invalid inputs. |
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
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 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from datetime import timedelta | ||
|
|
||
| from temporalio.client import Client | ||
| from temporalio.contrib.openai_agents import ModelActivityParameters, OpenAIAgentsPlugin | ||
| from temporalio.worker import Worker | ||
|
|
||
| from openai_agents.agent_patterns.workflows.agents_as_tools_workflow import ( | ||
| AgentsAsToolsWorkflow, | ||
| ) | ||
|
|
||
|
|
||
| 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) | ||
| ) | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| worker = Worker( | ||
| client, | ||
| task_queue="openai-agents-task-queue", | ||
| workflows=[ | ||
| AgentsAsToolsWorkflow, | ||
| ], | ||
| ) | ||
| await worker.run() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
File renamed without changes.
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 @@ | ||
| # Basic Agent Examples | ||
|
|
||
| Simple examples to get started with OpenAI Agents SDK integrated with Temporal workflows. | ||
|
|
||
| *Adapted from [OpenAI Agents SDK basic examples](https://github.com/openai/openai-agents-python/tree/main/examples/basic)* | ||
|
|
||
| ## Running the Examples | ||
|
|
||
| First, start the worker (supports all basic examples): | ||
| ```bash | ||
| uv run openai_agents/basic/run_worker.py | ||
| ``` | ||
|
|
||
| Then run individual examples in separate terminals: | ||
|
|
||
| ### Hello World Agent | ||
| ```bash | ||
| uv run openai_agents/basic/run_hello_world_workflow.py | ||
| ``` | ||
|
|
||
| ### Tools Agent | ||
| Agent with access to external tools (weather API): | ||
| ```bash | ||
| uv run openai_agents/basic/run_tools_workflow.py | ||
| ``` |
File renamed without changes.
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
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
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
File renamed without changes.
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
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,21 @@ | ||
| # Customer Service | ||
|
|
||
| Interactive customer service agent with escalation capabilities, extended with Temporal's durable conversational workflows. | ||
|
|
||
| *Adapted from [OpenAI Agents SDK customer service](https://github.com/openai/openai-agents-python/tree/main/examples/customer_service)* | ||
|
|
||
| This example demonstrates how to build persistent, stateful conversations where each conversation maintains state across multiple interactions and can survive system restarts and failures. | ||
|
|
||
| ## Running the Example | ||
|
|
||
| First, start the worker: | ||
| ```bash | ||
| uv run openai_agents/customer_service/run_worker.py | ||
| ``` | ||
|
|
||
| Then start a customer service conversation: | ||
| ```bash | ||
| uv run openai_agents/customer_service/run_customer_service_client.py --conversation-id my-conversation-123 | ||
| ``` | ||
|
|
||
| You can start a new conversation with any unique conversation ID, or resume existing conversations by using the same conversation ID. The conversation state is persisted in the Temporal workflow, allowing you to resume conversations even after restarting the client. |
File renamed without changes.
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
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 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import asyncio | ||
| from datetime import timedelta | ||
|
|
||
| from temporalio.client import Client | ||
| from temporalio.contrib.openai_agents import ModelActivityParameters, OpenAIAgentsPlugin | ||
| from temporalio.worker import Worker | ||
|
|
||
| from openai_agents.customer_service.workflows.customer_service_workflow import ( | ||
| CustomerServiceWorkflow, | ||
| ) | ||
|
|
||
|
|
||
| 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) | ||
| ) | ||
| ), | ||
| ], | ||
| ) | ||
|
|
||
| worker = Worker( | ||
| client, | ||
| task_queue="openai-agents-task-queue", | ||
| workflows=[ | ||
| CustomerServiceWorkflow, | ||
| ], | ||
| ) | ||
| await worker.run() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.run(main()) |
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
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,35 @@ | ||
| # Research Bot | ||
|
|
||
| Multi-agent research system with specialized roles, extended with Temporal's durable execution. | ||
|
|
||
| *Adapted from [OpenAI Agents SDK research bot](https://github.com/openai/openai-agents-python/tree/main/examples/research_bot)* | ||
|
|
||
| ## Architecture | ||
|
|
||
| The flow is: | ||
|
|
||
| 1. User enters their research topic | ||
| 2. `planner_agent` comes up with a plan to search the web for information. The plan is a list of search queries, with a search term and a reason for each query. | ||
| 3. For each search item, we run a `search_agent`, which uses the Web Search tool to search for that term and summarize the results. These all run in parallel. | ||
| 4. Finally, the `writer_agent` receives the search summaries, and creates a written report. | ||
|
|
||
| ## Running the Example | ||
|
|
||
| First, start the worker: | ||
| ```bash | ||
| uv run openai_agents/research_bot/run_worker.py | ||
| ``` | ||
|
|
||
| Then run the research workflow: | ||
| ```bash | ||
| uv run openai_agents/research_bot/run_research_workflow.py | ||
| ``` | ||
|
|
||
| ## Suggested Improvements | ||
|
|
||
| If you're building your own research bot, some ideas to add to this are: | ||
|
|
||
| 1. Retrieval: Add support for fetching relevant information from a vector store. You could use the File Search tool for this. | ||
| 2. Image and file upload: Allow users to attach PDFs or other files, as baseline context for the research. | ||
| 3. More planning and thinking: Models often produce better results given more time to think. Improve the planning process to come up with a better plan, and add an evaluation step so that the model can choose to improve its results, search for more stuff, etc. | ||
| 4. Code execution: Allow running code, which is useful for data analysis. |
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Just noticed there is no reference to these samples from the primary repo README like there are others, may want to add (just adding this general comment, deferring code review to Dan/Tim)
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.
Yes, we should add some readme cleanup + linking. Since there are a lot of changes I've been cutting them up into separate PRs. We should add that once we bring them all together.