-
Notifications
You must be signed in to change notification settings - Fork 79
OpenAI Agents basic examples #223
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-basic-examples
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
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,19 @@ | ||
import base64 | ||
|
||
from temporalio import activity | ||
|
||
|
||
@activity.defn | ||
async def read_image_as_base64(image_path: str) -> str: | ||
""" | ||
Read an image file and convert it to base64 string. | ||
|
||
Args: | ||
image_path: Path to the image file | ||
|
||
Returns: | ||
Base64 encoded string of the image | ||
""" | ||
with open(image_path, "rb") as image_file: | ||
encoded_string = base64.b64encode(image_file.read()).decode("utf-8") | ||
return encoded_string |
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,15 @@ | ||
import random | ||
|
||
from temporalio import activity | ||
|
||
|
||
@activity.defn | ||
async def random_number(max_value: int) -> int: | ||
"""Generate a random number up to the provided maximum.""" | ||
return random.randint(0, max_value) | ||
|
||
|
||
@activity.defn | ||
async def multiply_by_two(x: int) -> int: | ||
"""Simple multiplication by two.""" | ||
return x * 2 |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we feel about adding media files to this repository? This one is 235k. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,28 @@ | ||
import asyncio | ||
|
||
from temporalio.client import Client | ||
|
||
from openai_agents.basic.workflows.agent_lifecycle_workflow import ( | ||
AgentLifecycleWorkflow, | ||
) | ||
|
||
|
||
async def main() -> None: | ||
client = await Client.connect("localhost:7233") | ||
|
||
user_input = input("Enter a max number: ") | ||
max_number = int(user_input) | ||
|
||
result = await client.execute_workflow( | ||
AgentLifecycleWorkflow.run, | ||
max_number, | ||
id="agent-lifecycle-workflow", | ||
task_queue="openai-agents-basic-task-queue", | ||
) | ||
|
||
print(f"Final result: {result}") | ||
print("Done!") | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import asyncio | ||
|
||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
||
from openai_agents.basic.workflows.dynamic_system_prompt_workflow import ( | ||
DynamicSystemPromptWorkflow, | ||
) | ||
|
||
|
||
async def main(): | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin(), | ||
], | ||
) | ||
|
||
user_message = "Tell me a joke." | ||
|
||
result = await client.execute_workflow( | ||
DynamicSystemPromptWorkflow.run, | ||
user_message, | ||
id="dynamic-prompt-workflow", | ||
task_queue="openai-agents-basic-task-queue", | ||
) | ||
print(result) | ||
print() | ||
|
||
# Run with specific style | ||
result = await client.execute_workflow( | ||
DynamicSystemPromptWorkflow.run, | ||
args=[user_message, "pirate"], | ||
id="dynamic-prompt-pirate-workflow", | ||
task_queue="openai-agents-basic-task-queue", | ||
) | ||
print(result) | ||
|
||
|
||
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,31 @@ | ||
import asyncio | ||
|
||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
||
from openai_agents.basic.workflows.lifecycle_workflow import LifecycleWorkflow | ||
|
||
|
||
async def main(): | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin(), | ||
], | ||
) | ||
|
||
user_input = input("Enter a max number: ") | ||
max_number = int(user_input) | ||
|
||
result = await client.execute_workflow( | ||
LifecycleWorkflow.run, | ||
max_number, | ||
id="lifecycle-workflow", | ||
task_queue="openai-agents-basic-task-queue", | ||
) | ||
|
||
print(f"Final result: {result}") | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import asyncio | ||
import os | ||
|
||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
||
from openai_agents.basic.workflows.local_image_workflow import LocalImageWorkflow | ||
|
||
|
||
async def main(): | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin(), | ||
], | ||
) | ||
|
||
# Use the media file from the original example | ||
image_path = os.path.join(os.path.dirname(__file__), "media/image_bison.jpg") | ||
|
||
result = await client.execute_workflow( | ||
LocalImageWorkflow.run, | ||
args=[image_path, "What do you see in this image?"], | ||
id="local-image-workflow", | ||
task_queue="openai-agents-basic-task-queue", | ||
) | ||
|
||
print(f"Agent response: {result}") | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import asyncio | ||
|
||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
||
from openai_agents.basic.workflows.non_strict_output_workflow import ( | ||
NonStrictOutputWorkflow, | ||
) | ||
|
||
|
||
async def main(): | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin(), | ||
], | ||
) | ||
|
||
input_message = "Tell me 3 short jokes." | ||
|
||
result = await client.execute_workflow( | ||
NonStrictOutputWorkflow.run, | ||
input_message, | ||
id="non-strict-output-workflow", | ||
task_queue="openai-agents-basic-task-queue", | ||
) | ||
|
||
print("=== Non-Strict Output Type Results ===") | ||
for key, value in result.items(): | ||
print(f"{key}: {value}") | ||
print() | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import asyncio | ||
|
||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
||
from openai_agents.basic.workflows.previous_response_id_workflow import ( | ||
PreviousResponseIdWorkflow, | ||
) | ||
|
||
|
||
async def main(): | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin(), | ||
], | ||
) | ||
|
||
first_question = "What is the largest country in South America?" | ||
follow_up_question = "What is the capital of that country?" | ||
|
||
result = await client.execute_workflow( | ||
PreviousResponseIdWorkflow.run, | ||
args=[first_question, follow_up_question], | ||
id="previous-response-id-workflow", | ||
task_queue="openai-agents-basic-task-queue", | ||
) | ||
|
||
print("\nFinal results:") | ||
print(f"1. {result[0]}") | ||
print(f"2. {result[1]}") | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import asyncio | ||
|
||
from temporalio.client import Client | ||
from temporalio.contrib.openai_agents import OpenAIAgentsPlugin | ||
|
||
from openai_agents.basic.workflows.remote_image_workflow import RemoteImageWorkflow | ||
|
||
|
||
async def main(): | ||
client = await Client.connect( | ||
"localhost:7233", | ||
plugins=[ | ||
OpenAIAgentsPlugin(), | ||
], | ||
) | ||
|
||
# Use the URL from the original example | ||
image_url = ( | ||
"https://upload.wikimedia.org/wikipedia/commons/0/0c/GoldenGateBridge-001.jpg" | ||
) | ||
|
||
result = await client.execute_workflow( | ||
RemoteImageWorkflow.run, | ||
args=[image_url, "What do you see in this image?"], | ||
id="remote-image-workflow", | ||
task_queue="openai-agents-basic-task-queue", | ||
) | ||
|
||
print(f"Agent response: {result}") | ||
|
||
|
||
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
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.
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