diff --git a/openai_agents/basic/README.md b/openai_agents/basic/README.md index 56d9f528..e593ee48 100644 --- a/openai_agents/basic/README.md +++ b/openai_agents/basic/README.md @@ -4,6 +4,8 @@ Simple examples to get started with OpenAI Agents SDK integrated with Temporal w *Adapted from [OpenAI Agents SDK basic examples](https://github.com/openai/openai-agents-python/tree/main/examples/basic)* +Before running these examples, be sure to review the [prerequisites and background on the integration](../README.md). + ## Running the Examples First, start the worker (supports all basic examples): @@ -14,12 +16,64 @@ uv run openai_agents/basic/run_worker.py Then run individual examples in separate terminals: ### Hello World Agent +Basic agent that only responds in haikus: ```bash uv run openai_agents/basic/run_hello_world_workflow.py ``` ### Tools Agent -Agent with access to external tools (weather API): +Agent with access to external tools (simulated weather API): ```bash uv run openai_agents/basic/run_tools_workflow.py -``` \ No newline at end of file +``` + +### Agent Lifecycle with Hooks +Demonstrates agent lifecycle events and handoffs between agents: +```bash +uv run openai_agents/basic/run_agent_lifecycle_workflow.py +``` + +### Lifecycle with Usage Tracking +Shows detailed usage tracking with RunHooks (requests, tokens, etc.): +```bash +uv run openai_agents/basic/run_lifecycle_workflow.py +``` + +### Dynamic System Prompts +Agent with dynamic instruction generation based on context (haiku/pirate/robot): +```bash +uv run openai_agents/basic/run_dynamic_system_prompt_workflow.py +``` + +### Non-Strict Output Types +Demonstrates different JSON schema validation approaches: +```bash +uv run openai_agents/basic/run_non_strict_output_workflow.py +``` + +Note: `CustomOutputSchema` is not supported by the Temporal OpenAI Agents SDK integration and is omitted in this example. + +### Image Processing - Local +Process local image files with AI vision: +```bash +uv run openai_agents/basic/run_local_image_workflow.py +``` + +### Image Processing - Remote +Process remote image URLs with AI vision: +```bash +uv run openai_agents/basic/run_remote_image_workflow.py +``` + +### Previous Response ID +Demonstrates conversation continuity using response IDs: +```bash +uv run openai_agents/basic/run_previous_response_id_workflow.py +``` + +## Omitted Examples + +The following examples from the [reference repository](https://github.com/openai/openai-agents-python/tree/main/examples/basic) are not included in this Temporal adaptation: + +- **Session** - Stores state in local SQLite database, not appropriate for distributed workflows +- **Stream Items/Stream Text** - Streaming is not supported in Temporal OpenAI Agents SDK integration \ No newline at end of file diff --git a/openai_agents/basic/activities/image_activities.py b/openai_agents/basic/activities/image_activities.py new file mode 100644 index 00000000..23c770b1 --- /dev/null +++ b/openai_agents/basic/activities/image_activities.py @@ -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 diff --git a/openai_agents/basic/activities/math_activities.py b/openai_agents/basic/activities/math_activities.py new file mode 100644 index 00000000..1e875b23 --- /dev/null +++ b/openai_agents/basic/activities/math_activities.py @@ -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 diff --git a/openai_agents/basic/media/image_bison.jpg b/openai_agents/basic/media/image_bison.jpg new file mode 100644 index 00000000..b113c91f Binary files /dev/null and b/openai_agents/basic/media/image_bison.jpg differ diff --git a/openai_agents/basic/run_agent_lifecycle_workflow.py b/openai_agents/basic/run_agent_lifecycle_workflow.py new file mode 100644 index 00000000..7d3d8619 --- /dev/null +++ b/openai_agents/basic/run_agent_lifecycle_workflow.py @@ -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()) diff --git a/openai_agents/basic/run_dynamic_system_prompt_workflow.py b/openai_agents/basic/run_dynamic_system_prompt_workflow.py new file mode 100644 index 00000000..46c53c6d --- /dev/null +++ b/openai_agents/basic/run_dynamic_system_prompt_workflow.py @@ -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()) diff --git a/openai_agents/basic/run_hello_world_workflow.py b/openai_agents/basic/run_hello_world_workflow.py index 412dbb58..0662a4fa 100644 --- a/openai_agents/basic/run_hello_world_workflow.py +++ b/openai_agents/basic/run_hello_world_workflow.py @@ -20,7 +20,7 @@ async def main(): HelloWorldAgent.run, "Tell me about recursion in programming.", id="my-workflow-id", - task_queue="openai-agents-task-queue", + task_queue="openai-agents-basic-task-queue", ) print(f"Result: {result}") diff --git a/openai_agents/basic/run_lifecycle_workflow.py b/openai_agents/basic/run_lifecycle_workflow.py new file mode 100644 index 00000000..00286ece --- /dev/null +++ b/openai_agents/basic/run_lifecycle_workflow.py @@ -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()) diff --git a/openai_agents/basic/run_local_image_workflow.py b/openai_agents/basic/run_local_image_workflow.py new file mode 100644 index 00000000..6dfec809 --- /dev/null +++ b/openai_agents/basic/run_local_image_workflow.py @@ -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()) diff --git a/openai_agents/basic/run_non_strict_output_workflow.py b/openai_agents/basic/run_non_strict_output_workflow.py new file mode 100644 index 00000000..192e1206 --- /dev/null +++ b/openai_agents/basic/run_non_strict_output_workflow.py @@ -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()) diff --git a/openai_agents/basic/run_previous_response_id_workflow.py b/openai_agents/basic/run_previous_response_id_workflow.py new file mode 100644 index 00000000..ddae5107 --- /dev/null +++ b/openai_agents/basic/run_previous_response_id_workflow.py @@ -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()) diff --git a/openai_agents/basic/run_remote_image_workflow.py b/openai_agents/basic/run_remote_image_workflow.py new file mode 100644 index 00000000..f7c41b9a --- /dev/null +++ b/openai_agents/basic/run_remote_image_workflow.py @@ -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()) diff --git a/openai_agents/basic/run_tools_workflow.py b/openai_agents/basic/run_tools_workflow.py index eb6adcc1..9aed2dbf 100644 --- a/openai_agents/basic/run_tools_workflow.py +++ b/openai_agents/basic/run_tools_workflow.py @@ -20,7 +20,7 @@ async def main(): ToolsWorkflow.run, "What is the weather in Tokio?", id="tools-workflow", - task_queue="openai-agents-task-queue", + task_queue="openai-agents-basic-task-queue", ) print(f"Result: {result}") diff --git a/openai_agents/basic/run_worker.py b/openai_agents/basic/run_worker.py index ec94e907..94d6a882 100644 --- a/openai_agents/basic/run_worker.py +++ b/openai_agents/basic/run_worker.py @@ -8,7 +8,27 @@ from temporalio.worker import Worker from openai_agents.basic.activities.get_weather_activity import get_weather +from openai_agents.basic.activities.image_activities import read_image_as_base64 +from openai_agents.basic.activities.math_activities import ( + multiply_by_two, + random_number, +) +from openai_agents.basic.workflows.agent_lifecycle_workflow import ( + AgentLifecycleWorkflow, +) +from openai_agents.basic.workflows.dynamic_system_prompt_workflow import ( + DynamicSystemPromptWorkflow, +) from openai_agents.basic.workflows.hello_world_workflow import HelloWorldAgent +from openai_agents.basic.workflows.lifecycle_workflow import LifecycleWorkflow +from openai_agents.basic.workflows.local_image_workflow import LocalImageWorkflow +from openai_agents.basic.workflows.non_strict_output_workflow import ( + NonStrictOutputWorkflow, +) +from openai_agents.basic.workflows.previous_response_id_workflow import ( + PreviousResponseIdWorkflow, +) +from openai_agents.basic.workflows.remote_image_workflow import RemoteImageWorkflow from openai_agents.basic.workflows.tools_workflow import ToolsWorkflow @@ -27,13 +47,23 @@ async def main(): worker = Worker( client, - task_queue="openai-agents-task-queue", + task_queue="openai-agents-basic-task-queue", workflows=[ HelloWorldAgent, ToolsWorkflow, + AgentLifecycleWorkflow, + DynamicSystemPromptWorkflow, + NonStrictOutputWorkflow, + LocalImageWorkflow, + RemoteImageWorkflow, + LifecycleWorkflow, + PreviousResponseIdWorkflow, ], activities=[ get_weather, + multiply_by_two, + random_number, + read_image_as_base64, ], ) await worker.run() diff --git a/openai_agents/basic/workflows/agent_lifecycle_workflow.py b/openai_agents/basic/workflows/agent_lifecycle_workflow.py new file mode 100644 index 00000000..61cdad90 --- /dev/null +++ b/openai_agents/basic/workflows/agent_lifecycle_workflow.py @@ -0,0 +1,97 @@ +from typing import Any + +from agents import Agent, AgentHooks, RunContextWrapper, Runner, function_tool +from pydantic import BaseModel +from temporalio import workflow + + +class CustomAgentHooks(AgentHooks): + def __init__(self, display_name: str): + self.event_counter = 0 + self.display_name = display_name + + async def on_start(self, context: RunContextWrapper, agent: Agent) -> None: + self.event_counter += 1 + print( + f"### ({self.display_name}) {self.event_counter}: Agent {agent.name} started" + ) + + async def on_end( + self, context: RunContextWrapper, agent: Agent, output: Any + ) -> None: + self.event_counter += 1 + print( + f"### ({self.display_name}) {self.event_counter}: Agent {agent.name} ended with output {output}" + ) + + async def on_handoff( + self, context: RunContextWrapper, agent: Agent, source: Agent + ) -> None: + self.event_counter += 1 + print( + f"### ({self.display_name}) {self.event_counter}: Agent {source.name} handed off to {agent.name}" + ) + + async def on_tool_start( + self, context: RunContextWrapper, agent: Agent, tool + ) -> None: + self.event_counter += 1 + print( + f"### ({self.display_name}) {self.event_counter}: Agent {agent.name} started tool {tool.name}" + ) + + async def on_tool_end( + self, context: RunContextWrapper, agent: Agent, tool, result: str + ) -> None: + self.event_counter += 1 + print( + f"### ({self.display_name}) {self.event_counter}: Agent {agent.name} ended tool {tool.name} with result {result}" + ) + + +@function_tool +def random_number_tool(max: int) -> int: + """ + Generate a random number up to the provided maximum. + """ + return workflow.random().randint(0, max) + + +@function_tool +def multiply_by_two_tool(x: int) -> int: + """Simple multiplication by two.""" + return x * 2 + + +class FinalResult(BaseModel): + number: int + + +@workflow.defn +class AgentLifecycleWorkflow: + @workflow.run + async def run(self, max_number: int) -> FinalResult: + + multiply_agent = Agent( + name="Multiply Agent", + instructions="Multiply the number by 2 and then return the final result.", + tools=[multiply_by_two_tool], + output_type=FinalResult, + hooks=CustomAgentHooks(display_name="Multiply Agent"), + ) + + start_agent = Agent( + name="Start Agent", + instructions="Generate a random number. If it's even, stop. If it's odd, hand off to the multiply agent.", + tools=[random_number_tool], + output_type=FinalResult, + handoffs=[multiply_agent], + hooks=CustomAgentHooks(display_name="Start Agent"), + ) + + result = await Runner.run( + start_agent, + input=f"Generate a random number between 0 and {max_number}.", + ) + + return result.final_output diff --git a/openai_agents/basic/workflows/dynamic_system_prompt_workflow.py b/openai_agents/basic/workflows/dynamic_system_prompt_workflow.py new file mode 100644 index 00000000..00e77fbb --- /dev/null +++ b/openai_agents/basic/workflows/dynamic_system_prompt_workflow.py @@ -0,0 +1,48 @@ +from typing import Literal, Optional + +from agents import Agent, RunContextWrapper, Runner +from temporalio import workflow + + +class CustomContext: + def __init__(self, style: Literal["haiku", "pirate", "robot"]): + self.style = style + + +def custom_instructions( + run_context: RunContextWrapper[CustomContext], agent: Agent[CustomContext] +) -> str: + context = run_context.context + if context.style == "haiku": + return "Only respond in haikus." + elif context.style == "pirate": + return "Respond as a pirate." + else: + return "Respond as a robot and say 'beep boop' a lot." + + +@workflow.defn +class DynamicSystemPromptWorkflow: + @workflow.run + async def run(self, user_message: str, style: Optional[str] = None) -> str: + if style is None: + selected_style: Literal[ + "haiku", "pirate", "robot" + ] = workflow.random().choice(["haiku", "pirate", "robot"]) + else: + # Validate that the provided style is one of the allowed values + if style not in ["haiku", "pirate", "robot"]: + raise ValueError( + f"Invalid style: {style}. Must be one of: haiku, pirate, robot" + ) + selected_style = style # type: ignore + + context = CustomContext(style=selected_style) + + agent = Agent( + name="Chat agent", + instructions=custom_instructions, + ) + + result = await Runner.run(agent, user_message, context=context) + return f"Style: {selected_style}\nResponse: {result.final_output}" diff --git a/openai_agents/basic/workflows/lifecycle_workflow.py b/openai_agents/basic/workflows/lifecycle_workflow.py new file mode 100644 index 00000000..cf72de4a --- /dev/null +++ b/openai_agents/basic/workflows/lifecycle_workflow.py @@ -0,0 +1,106 @@ +from typing import Any + +from agents import ( + Agent, + RunContextWrapper, + RunHooks, + Runner, + Tool, + Usage, + function_tool, +) +from pydantic import BaseModel +from temporalio import workflow + + +class ExampleHooks(RunHooks): + def __init__(self): + self.event_counter = 0 + + def _usage_to_str(self, usage: Usage) -> str: + return f"{usage.requests} requests, {usage.input_tokens} input tokens, {usage.output_tokens} output tokens, {usage.total_tokens} total tokens" + + async def on_agent_start(self, context: RunContextWrapper, agent: Agent) -> None: + self.event_counter += 1 + print( + f"### {self.event_counter}: Agent {agent.name} started. Usage: {self._usage_to_str(context.usage)}" + ) + + async def on_agent_end( + self, context: RunContextWrapper, agent: Agent, output: Any + ) -> None: + self.event_counter += 1 + print( + f"### {self.event_counter}: Agent {agent.name} ended with output {output}. Usage: {self._usage_to_str(context.usage)}" + ) + + async def on_tool_start( + self, context: RunContextWrapper, agent: Agent, tool: Tool + ) -> None: + self.event_counter += 1 + print( + f"### {self.event_counter}: Tool {tool.name} started. Usage: {self._usage_to_str(context.usage)}" + ) + + async def on_tool_end( + self, context: RunContextWrapper, agent: Agent, tool: Tool, result: str + ) -> None: + self.event_counter += 1 + print( + f"### {self.event_counter}: Tool {tool.name} ended with result {result}. Usage: {self._usage_to_str(context.usage)}" + ) + + async def on_handoff( + self, context: RunContextWrapper, from_agent: Agent, to_agent: Agent + ) -> None: + self.event_counter += 1 + print( + f"### {self.event_counter}: Handoff from {from_agent.name} to {to_agent.name}. Usage: {self._usage_to_str(context.usage)}" + ) + + +@function_tool +def random_number(max: int) -> int: + """Generate a random number up to the provided max.""" + return workflow.random().randint(0, max) + + +@function_tool +def multiply_by_two(x: int) -> int: + """Return x times two.""" + return x * 2 + + +class FinalResult(BaseModel): + number: int + + +@workflow.defn +class LifecycleWorkflow: + @workflow.run + async def run(self, max_number: int) -> FinalResult: + hooks = ExampleHooks() + + multiply_agent = Agent( + name="Multiply Agent", + instructions="Multiply the number by 2 and then return the final result.", + tools=[multiply_by_two], + output_type=FinalResult, + ) + + start_agent = Agent( + name="Start Agent", + instructions="Generate a random number. If it's even, stop. If it's odd, hand off to the multiplier agent.", + tools=[random_number], + output_type=FinalResult, + handoffs=[multiply_agent], + ) + + result = await Runner.run( + start_agent, + hooks=hooks, + input=f"Generate a random number between 0 and {max_number}.", + ) + + print("Done!") + return result.final_output diff --git a/openai_agents/basic/workflows/local_image_workflow.py b/openai_agents/basic/workflows/local_image_workflow.py new file mode 100644 index 00000000..b536f517 --- /dev/null +++ b/openai_agents/basic/workflows/local_image_workflow.py @@ -0,0 +1,54 @@ +from agents import Agent, Runner +from temporalio import workflow + +from openai_agents.basic.activities.image_activities import read_image_as_base64 + + +@workflow.defn +class LocalImageWorkflow: + @workflow.run + async def run( + self, image_path: str, question: str = "What do you see in this image?" + ) -> str: + """ + Process a local image file with an AI agent. + + Args: + image_path: Path to the local image file + question: Question to ask about the image + + Returns: + Agent's response about the image + """ + # Convert image to base64 using activity + b64_image = await workflow.execute_activity( + read_image_as_base64, + image_path, + start_to_close_timeout=workflow.timedelta(seconds=30), + ) + + agent = Agent( + name="Assistant", + instructions="You are a helpful assistant.", + ) + + result = await Runner.run( + agent, + [ + { + "role": "user", + "content": [ + { + "type": "input_image", + "detail": "auto", + "image_url": f"data:image/jpeg;base64,{b64_image}", + } + ], + }, + { + "role": "user", + "content": question, + }, + ], + ) + return result.final_output diff --git a/openai_agents/basic/workflows/non_strict_output_workflow.py b/openai_agents/basic/workflows/non_strict_output_workflow.py new file mode 100644 index 00000000..f56716c1 --- /dev/null +++ b/openai_agents/basic/workflows/non_strict_output_workflow.py @@ -0,0 +1,86 @@ +import json +from dataclasses import dataclass +from typing import Any + +from agents import Agent, AgentOutputSchema, AgentOutputSchemaBase, Runner +from temporalio import workflow + + +@dataclass +class OutputType: + jokes: dict[int, str] + """A list of jokes, indexed by joke number.""" + + +class CustomOutputSchema(AgentOutputSchemaBase): + """A demonstration of a custom output schema.""" + + def is_plain_text(self) -> bool: + return False + + def name(self) -> str: + return "CustomOutputSchema" + + def json_schema(self) -> dict[str, Any]: + return { + "type": "object", + "properties": { + "jokes": {"type": "object", "properties": {"joke": {"type": "string"}}} + }, + } + + def is_strict_json_schema(self) -> bool: + return False + + def validate_json(self, json_str: str) -> Any: + json_obj = json.loads(json_str) + # Just for demonstration, we'll return a list. + return list(json_obj["jokes"].values()) + + +@workflow.defn +class NonStrictOutputWorkflow: + @workflow.run + async def run(self, input_text: str) -> dict[str, Any]: + """ + Demonstrates non-strict output types that require special handling. + + Args: + input_text: The input message to the agent + + Returns: + Dictionary with results from different output type approaches + """ + results = {} + + agent = Agent( + name="Assistant", + instructions="You are a helpful assistant.", + output_type=OutputType, + ) + + # First, try with strict output type (this should fail) + try: + result = await Runner.run(agent, input_text) + results["strict_result"] = "Unexpected success" + except Exception as e: + results["strict_error"] = str(e) + + # Now try with non-strict output type + try: + agent.output_type = AgentOutputSchema(OutputType, strict_json_schema=False) + result = await Runner.run(agent, input_text) + results["non_strict_result"] = result.final_output + except Exception as e: + results["non_strict_error"] = str(e) + + # Finally, try with custom output type + # Not presently supported by Temporal + # try: + # agent.output_type = CustomOutputSchema() + # result = await Runner.run(agent, input_text) + # results["custom_result"] = result.final_output + # except Exception as e: + # results["custom_error"] = str(e) + + return results diff --git a/openai_agents/basic/workflows/previous_response_id_workflow.py b/openai_agents/basic/workflows/previous_response_id_workflow.py new file mode 100644 index 00000000..64015159 --- /dev/null +++ b/openai_agents/basic/workflows/previous_response_id_workflow.py @@ -0,0 +1,48 @@ +from typing import Tuple + +from agents import Agent, Runner +from temporalio import workflow + + +@workflow.defn +class PreviousResponseIdWorkflow: + @workflow.run + async def run( + self, first_question: str, follow_up_question: str + ) -> Tuple[str, str]: + """ + Demonstrates usage of the `previous_response_id` parameter to continue a conversation. + The second run passes the previous response ID to the model, which allows it to continue the + conversation without re-sending the previous messages. + + Notes: + 1. This only applies to the OpenAI Responses API. Other models will ignore this parameter. + 2. Responses are only stored for 30 days as of this writing, so in production you should + store the response ID along with an expiration date; if the response is no longer valid, + you'll need to re-send the previous conversation history. + + Args: + first_question: The initial question to ask + follow_up_question: The follow-up question that references the first response + + Returns: + Tuple of (first_response, second_response) + """ + agent = Agent( + name="Assistant", + instructions="You are a helpful assistant. be VERY concise.", + ) + + # First question + result1 = await Runner.run(agent, first_question) + first_response = result1.final_output + + # Follow-up question using previous response ID + result2 = await Runner.run( + agent, + follow_up_question, + previous_response_id=result1.last_response_id, + ) + second_response = result2.final_output + + return first_response, second_response diff --git a/openai_agents/basic/workflows/remote_image_workflow.py b/openai_agents/basic/workflows/remote_image_workflow.py new file mode 100644 index 00000000..ce07bd21 --- /dev/null +++ b/openai_agents/basic/workflows/remote_image_workflow.py @@ -0,0 +1,45 @@ +from agents import Agent, Runner +from temporalio import workflow + + +@workflow.defn +class RemoteImageWorkflow: + @workflow.run + async def run( + self, image_url: str, question: str = "What do you see in this image?" + ) -> str: + """ + Process a remote image URL with an AI agent. + + Args: + image_url: URL of the remote image + question: Question to ask about the image + + Returns: + Agent's response about the image + """ + agent = Agent( + name="Assistant", + instructions="You are a helpful assistant.", + ) + + result = await Runner.run( + agent, + [ + { + "role": "user", + "content": [ + { + "type": "input_image", + "detail": "auto", + "image_url": image_url, + } + ], + }, + { + "role": "user", + "content": question, + }, + ], + ) + return result.final_output