Skip to content

Commit b5e63be

Browse files
committed
formatting
1 parent 38e1192 commit b5e63be

15 files changed

Lines changed: 141 additions & 75 deletions

openai_agents/run_agents_as_tools_workflow.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ async def main():
1212
client = await Client.connect("localhost:7233")
1313

1414
# Execute a workflow
15-
result = await client.execute_workflow(AgentsAsToolsWorkflow.run,
16-
"Translate to English: '¿Cómo estás?'",
17-
id="my-workflow-id", task_queue="my-task-queue",
18-
id_reuse_policy=WorkflowIDReusePolicy.TERMINATE_IF_RUNNING)
15+
result = await client.execute_workflow(
16+
AgentsAsToolsWorkflow.run,
17+
"Translate to English: '¿Cómo estás?'",
18+
id="my-workflow-id",
19+
task_queue="my-task-queue",
20+
id_reuse_policy=WorkflowIDReusePolicy.TERMINATE_IF_RUNNING,
21+
)
1922

2023
print(f"Result: {result}")
2124

openai_agents/run_customer_service_client.py

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,28 @@
33
from ftplib import print_line
44

55
from temporalio import workflow
6-
from temporalio.client import Client, WorkflowQueryRejectedError, WorkflowUpdateFailedError
7-
from temporalio.common import WorkflowIDReusePolicy, QueryRejectCondition
6+
from temporalio.client import (
7+
Client,
8+
WorkflowQueryRejectedError,
9+
WorkflowUpdateFailedError,
10+
)
11+
from temporalio.common import QueryRejectCondition, WorkflowIDReusePolicy
812
from temporalio.service import RPCError, RPCStatusCode
913

1014
with workflow.unsafe.imports_passed_through():
11-
from temporalio.contrib.openai_agents.open_ai_data_converter import open_ai_data_converter
15+
from temporalio.contrib.openai_agents.open_ai_data_converter import (
16+
open_ai_data_converter,
17+
)
1218

13-
from openai_agents.workflows.customer_service_workflow import CustomerServiceWorkflow, ProcessUserMessageInput
19+
from openai_agents.workflows.customer_service_workflow import (
20+
CustomerServiceWorkflow,
21+
ProcessUserMessageInput,
22+
)
1423

1524

1625
async def main():
1726
parser = argparse.ArgumentParser()
18-
parser.add_argument('--conversation-id', type=str, required=True)
27+
parser.add_argument("--conversation-id", type=str, required=True)
1928
args = parser.parse_args()
2029

2130
# Create client connected to server at the given address
@@ -30,8 +39,10 @@ async def main():
3039
# If the workflow is not open, start a new one
3140
start = False
3241
try:
33-
history = await handle.query(CustomerServiceWorkflow.get_chat_history,
34-
reject_condition=QueryRejectCondition.NOT_OPEN)
42+
history = await handle.query(
43+
CustomerServiceWorkflow.get_chat_history,
44+
reject_condition=QueryRejectCondition.NOT_OPEN,
45+
)
3546
except WorkflowQueryRejectedError as e:
3647
start = True
3748
except RPCError as e:
@@ -40,25 +51,34 @@ async def main():
4051
else:
4152
raise e
4253
if start:
43-
await client.start_workflow(CustomerServiceWorkflow.run,
44-
id=args.conversation_id, task_queue="my-task-queue",
45-
id_reuse_policy=WorkflowIDReusePolicy.ALLOW_DUPLICATE)
54+
await client.start_workflow(
55+
CustomerServiceWorkflow.run,
56+
id=args.conversation_id,
57+
task_queue="my-task-queue",
58+
id_reuse_policy=WorkflowIDReusePolicy.ALLOW_DUPLICATE,
59+
)
4660
history = []
4761
print(*history, sep="\n")
4862

4963
# Loop to send messages to the workflow
5064
while True:
5165
user_input = input("Enter your message: ")
52-
message_input = ProcessUserMessageInput(user_input=user_input, chat_length=len(history))
66+
message_input = ProcessUserMessageInput(
67+
user_input=user_input, chat_length=len(history)
68+
)
5369
try:
54-
new_history = await handle.execute_update(CustomerServiceWorkflow.process_user_message, message_input)
70+
new_history = await handle.execute_update(
71+
CustomerServiceWorkflow.process_user_message, message_input
72+
)
5573
history.extend(new_history)
5674
print(*new_history, sep="\n")
5775
except WorkflowUpdateFailedError:
5876
print_line("** Stale conversation. Reloading...")
5977
length = len(history)
60-
history = await handle.query(CustomerServiceWorkflow.get_chat_history,
61-
reject_condition=QueryRejectCondition.NOT_OPEN)
78+
history = await handle.query(
79+
CustomerServiceWorkflow.get_chat_history,
80+
reject_condition=QueryRejectCondition.NOT_OPEN,
81+
)
6282
print(*history[length:], sep="\n")
6383

6484

openai_agents/run_hello_world_workflow.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
from temporalio.client import Client
44
from temporalio.common import WorkflowIDReusePolicy
5-
6-
from openai_agents.workflows.hello_world_workflow import HelloWorldAgent
7-
85
from temporalio.contrib.openai_agents.temporal_openai_agents import (
96
set_open_ai_agent_temporal_overrides,
107
)
118

9+
from openai_agents.workflows.hello_world_workflow import HelloWorldAgent
10+
11+
1212
async def main():
1313
# Create client connected to server at the given address
1414
client = await Client.connect("localhost:7233")
1515

1616
with set_open_ai_agent_temporal_overrides():
1717
# Execute a workflow
18-
result = await client.execute_workflow(HelloWorldAgent.run, "Tell me about recursion in programming.",
19-
id="my-workflow-id", task_queue="my-task-queue",
20-
id_reuse_policy=WorkflowIDReusePolicy.TERMINATE_IF_RUNNING)
18+
result = await client.execute_workflow(
19+
HelloWorldAgent.run,
20+
"Tell me about recursion in programming.",
21+
id="my-workflow-id",
22+
task_queue="my-task-queue",
23+
id_reuse_policy=WorkflowIDReusePolicy.TERMINATE_IF_RUNNING,
24+
)
2125
print(f"Result: {result}")
2226

2327

openai_agents/run_research_workflow.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@
55

66
from openai_agents.workflows.research_bot_workflow import ResearchWorkflow
77

8+
89
async def main():
910
# Create client connected to server at the given address
1011
client = await Client.connect(
1112
"localhost:7233",
1213
)
1314

1415
# Execute a workflow
15-
result = await client.execute_workflow(ResearchWorkflow.run,
16-
"Caribbean vacation spots in April, optimizing for surfing, hiking and water sports",
17-
id="research-workflow",
18-
task_queue="my-task-queue",
19-
id_reuse_policy=WorkflowIDReusePolicy.TERMINATE_IF_RUNNING)
16+
result = await client.execute_workflow(
17+
ResearchWorkflow.run,
18+
"Caribbean vacation spots in April, optimizing for surfing, hiking and water sports",
19+
id="research-workflow",
20+
task_queue="my-task-queue",
21+
id_reuse_policy=WorkflowIDReusePolicy.TERMINATE_IF_RUNNING,
22+
)
2023

2124
print(f"Result: {result}")
2225

openai_agents/run_tools_workflow.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55

66
from openai_agents.workflows.tools_workflow import ToolsWorkflow
77

8+
89
async def main():
910
# Create client connected to server at the given address
1011
client = await Client.connect("localhost:7233")
1112

1213
# Execute a workflow
13-
result = await client.execute_workflow(ToolsWorkflow.run, "What is the weather in Tokio?", id="tools-workflow",
14-
task_queue="my-task-queue",
15-
id_reuse_policy=WorkflowIDReusePolicy.TERMINATE_IF_RUNNING)
14+
result = await client.execute_workflow(
15+
ToolsWorkflow.run,
16+
"What is the weather in Tokio?",
17+
id="tools-workflow",
18+
task_queue="my-task-queue",
19+
id_reuse_policy=WorkflowIDReusePolicy.TERMINATE_IF_RUNNING,
20+
)
1621

1722
print(f"Result: {result}")
1823

openai_agents/run_worker.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@
44
import concurrent.futures
55

66
from temporalio import workflow
7-
87
from temporalio.client import Client
9-
from temporalio.worker import Worker
108
from temporalio.contrib.openai_agents.invoke_model_activity import ModelActivity
11-
from temporalio.contrib.openai_agents.open_ai_data_converter import open_ai_data_converter
9+
from temporalio.contrib.openai_agents.open_ai_data_converter import (
10+
open_ai_data_converter,
11+
)
12+
from temporalio.worker import Worker
1213

1314
with workflow.unsafe.imports_passed_through():
1415
from openai_agents.workflows.hello_world_workflow import HelloWorldAgent
1516
from openai_agents.workflows.tools_workflow import ToolsWorkflow
1617
from openai_agents.workflows.research_bot_workflow import ResearchWorkflow
17-
from openai_agents.workflows.customer_service_workflow import CustomerServiceWorkflow
18+
from openai_agents.workflows.customer_service_workflow import (
19+
CustomerServiceWorkflow,
20+
)
1821
from openai_agents.workflows.agents_as_tools_workflow import AgentsAsToolsWorkflow
1922

2023
from openai_agents.workflows.get_weather_activity import get_weather
2124

22-
2325
from temporalio.contrib.openai_agents.temporal_openai_agents import (
2426
set_open_ai_agent_temporal_overrides,
2527
)
@@ -39,16 +41,16 @@ async def main():
3941
client,
4042
task_queue="my-task-queue",
4143
workflows=[
42-
HelloWorldAgent,
43-
ToolsWorkflow,
44-
ResearchWorkflow,
45-
CustomerServiceWorkflow,
46-
AgentsAsToolsWorkflow,
47-
],
44+
HelloWorldAgent,
45+
ToolsWorkflow,
46+
ResearchWorkflow,
47+
CustomerServiceWorkflow,
48+
AgentsAsToolsWorkflow,
49+
],
4850
activities=[
49-
model_activity.invoke_model_activity,
50-
get_weather,
51-
],
51+
model_activity.invoke_model_activity,
52+
get_weather,
53+
],
5254
activity_executor=activity_executor,
5355
)
5456
await worker.run()

openai_agents/workflows/agents_as_tools_workflow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from temporalio import workflow
2-
2+
33
with workflow.unsafe.imports_passed_through():
4-
from agents import Agent, ItemHelpers, MessageOutputItem, Runner, trace, RunConfig
4+
from agents import Agent, ItemHelpers, MessageOutputItem, RunConfig, Runner, trace
55

66
"""
77
This example shows the agents-as-tools pattern. The frontline agent receives a user message and

openai_agents/workflows/customer_service_workflow.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
from temporalio import workflow
44

55
with workflow.unsafe.imports_passed_through():
6-
from pydantic import BaseModel
76
from agents import (
87
Agent,
98
HandoffOutputItem,
109
ItemHelpers,
1110
MessageOutputItem,
11+
RunConfig,
1212
RunContextWrapper,
1313
Runner,
1414
ToolCallItem,
1515
ToolCallOutputItem,
1616
TResponseInputItem,
1717
function_tool,
1818
handoff,
19-
trace, RunConfig,
19+
trace,
2020
)
2121
from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX
22+
from pydantic import BaseModel
2223

2324

2425
### CONTEXT
@@ -35,7 +36,8 @@ class AirlineAgentContext(BaseModel):
3536

3637

3738
@function_tool(
38-
name_override="faq_lookup_tool", description_override="Lookup frequently asked questions."
39+
name_override="faq_lookup_tool",
40+
description_override="Lookup frequently asked questions.",
3941
)
4042
async def faq_lookup_tool(question: str) -> str:
4143
if "bag" in question or "baggage" in question:
@@ -57,7 +59,9 @@ async def faq_lookup_tool(question: str) -> str:
5759

5860
@function_tool
5961
async def update_seat(
60-
context: RunContextWrapper[AirlineAgentContext], confirmation_number: str, new_seat: str
62+
context: RunContextWrapper[AirlineAgentContext],
63+
confirmation_number: str,
64+
new_seat: str,
6165
) -> str:
6266
"""
6367
Update the seat for a given confirmation number.
@@ -77,13 +81,16 @@ async def update_seat(
7781
### HOOKS
7882

7983

80-
async def on_seat_booking_handoff(context: RunContextWrapper[AirlineAgentContext]) -> None:
84+
async def on_seat_booking_handoff(
85+
context: RunContextWrapper[AirlineAgentContext],
86+
) -> None:
8187
flight_number = f"FLT-{workflow.random().randint(100, 999)}"
8288
context.context.flight_number = flight_number
8389

8490

8591
### AGENTS
8692

93+
8794
def init_agents() -> Agent[AirlineAgentContext]:
8895
"""
8996
Initialize the agents for the airline customer service workflow.
@@ -141,7 +148,6 @@ class ProcessUserMessageInput(BaseModel):
141148

142149
@workflow.defn
143150
class CustomerServiceWorkflow:
144-
145151
def __init__(self, input_items: list[TResponseInputItem] = None):
146152
self.run_config = RunConfig()
147153
self.chat_history = []
@@ -152,7 +158,9 @@ def __init__(self, input_items: list[TResponseInputItem] = None):
152158
@workflow.run
153159
async def run(self, input_items: list[TResponseInputItem] = None):
154160
await workflow.wait_condition(
155-
lambda: workflow.info().is_continue_as_new_suggested() and workflow.all_handlers_finished())
161+
lambda: workflow.info().is_continue_as_new_suggested()
162+
and workflow.all_handlers_finished()
163+
)
156164
workflow.continue_as_new(self.input_items)
157165

158166
@workflow.query
@@ -165,23 +173,33 @@ async def process_user_message(self, input: ProcessUserMessageInput) -> list[str
165173
self.chat_history.append(f"User: {input.user_input}")
166174
with trace("Customer service", group_id=workflow.info().workflow_id):
167175
self.input_items.append({"content": input.user_input, "role": "user"})
168-
result = await Runner.run(self.current_agent, self.input_items, context=self.context,
169-
run_config=self.run_config)
176+
result = await Runner.run(
177+
self.current_agent,
178+
self.input_items,
179+
context=self.context,
180+
run_config=self.run_config,
181+
)
170182

171183
for new_item in result.new_items:
172184
agent_name = new_item.agent.name
173185
if isinstance(new_item, MessageOutputItem):
174-
self.chat_history.append(f"{agent_name}: {ItemHelpers.text_message_output(new_item)}")
186+
self.chat_history.append(
187+
f"{agent_name}: {ItemHelpers.text_message_output(new_item)}"
188+
)
175189
elif isinstance(new_item, HandoffOutputItem):
176190
self.chat_history.append(
177191
f"Handed off from {new_item.source_agent.name} to {new_item.target_agent.name}"
178192
)
179193
elif isinstance(new_item, ToolCallItem):
180194
self.chat_history.append(f"{agent_name}: Calling a tool")
181195
elif isinstance(new_item, ToolCallOutputItem):
182-
self.chat_history.append(f"{agent_name}: Tool call output: {new_item.output}")
196+
self.chat_history.append(
197+
f"{agent_name}: Tool call output: {new_item.output}"
198+
)
183199
else:
184-
self.chat_history.append(f"{agent_name}: Skipping item: {new_item.__class__.__name__}")
200+
self.chat_history.append(
201+
f"{agent_name}: Skipping item: {new_item.__class__.__name__}"
202+
)
185203
self.input_items = result.to_input_list()
186204
self.current_agent = result.last_agent
187205
workflow.set_current_details("\n\n".join(self.chat_history))

openai_agents/workflows/hello_world_workflow.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
with workflow.unsafe.imports_passed_through():
55
from agents import Agent, Runner
66

7+
78
@workflow.defn
89
class HelloWorldAgent:
910
@workflow.run

openai_agents/workflows/research_agents/planner_agent.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from pydantic import BaseModel
2-
31
from agents import Agent
2+
from pydantic import BaseModel
43

54
PROMPT = (
65
"You are a helpful research assistant. Given a query, come up with a set of web searches "

0 commit comments

Comments
 (0)