Skip to content

Commit df33d63

Browse files
committed
Add comments, improve human_in_the_loop sample
1 parent 9bbfb72 commit df33d63

File tree

1 file changed

+16
-33
lines changed

1 file changed

+16
-33
lines changed

samples-v2/openai_agents/function_app.py

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,24 @@
33
from durable_decorators import durable_openai_agent_orchestrator
44
from model_activity import create_invoke_model_activity
55

6-
import basic.hello_world
7-
86
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
97

108

9+
# Regular HTTP trigger
1110
@app.route(route="orchestrators/{functionName}")
1211
@app.durable_client_input(client_name="client")
1312
async def orchestration_starter(req: func.HttpRequest, client):
1413
function_name = req.route_params.get('functionName')
14+
# Starting a new orchestration instance in the most regular way
1515
instance_id = await client.start_new(function_name)
1616
response = client.create_check_status_response(req, instance_id)
1717
return response
1818

1919

20-
@app.orchestration_trigger(context_name="context")
21-
@durable_openai_agent_orchestrator
20+
@app.orchestration_trigger(context_name="context") # Regular orchestrator function
21+
@durable_openai_agent_orchestrator # This is what turns the magic on!
2222
def haiku(context):
23+
# Regular OpenAI Agent code, straight from the most basic sample
2324
agent = Agent(
2425
name="Assistant",
2526
instructions="You only respond in haikus.",
@@ -51,12 +52,13 @@ def haiku_judge(context):
5152

5253
@app.orchestration_trigger(context_name="context")
5354
@durable_openai_agent_orchestrator
54-
def hello_cities(context):
55+
def human_in_the_loop(context):
5556
writer = Agent(
5657
name="Writer",
5758
instructions="You only respond in haikus.",
5859
)
5960

61+
# You can invoke Durable Functions activities, like you normally would
6062
topic = yield context.call_activity("hello", "Seattle")
6163

6264
haiku = Runner.run_sync(writer, f"Tell me about '{topic}'")
@@ -68,39 +70,20 @@ def hello_cities(context):
6870

6971
evaluation = Runner.run_sync(judge, f"Is this a good haiku? {haiku.final_output}")
7072

71-
return [haiku.final_output, "\n\n" + evaluation.final_output]
72-
73-
74-
@app.activity_trigger(input_name="name")
75-
async def hello(name: str):
76-
return f"Hello {name}!"
77-
78-
79-
80-
@app.orchestration_trigger(context_name="context")
81-
@durable_openai_agent_orchestrator
82-
def human_in_the_loop(context):
83-
writer = Agent(
84-
name="Writer",
85-
instructions="You only respond in haikus.",
86-
)
87-
88-
haiku = Runner.run_sync(writer, "Tell me about recursion in programming.")
89-
90-
judge = Agent(
91-
name="Judge",
92-
instructions="You judge haikus.",
93-
)
94-
95-
evaluation = Runner.run_sync(judge, f"Is this a good haiku? {haiku.final_output}")
96-
73+
# You can set custom orchestration status, like you normally would
9774
context.set_custom_status(f"Haiku:\n{haiku.final_output}\n\nEvaluation:\n{evaluation.final_output}\n\nWaiting for approval...")
75+
76+
# You can wait for external events, like you normally would
9877
event_data = yield context.wait_for_external_event("Approve")
99-
context.set_custom_status("")
10078

10179
reaction = Runner.run_sync(writer, f"Your haiku is considered {event_data}. React emotionally.")
10280

103-
return reaction.final_output
81+
return reaction.final_output
82+
83+
84+
@app.activity_trigger(input_name="name")
85+
async def hello(name: str):
86+
return f"Hello {name}!"
10487

10588

10689
#region Implementation details

0 commit comments

Comments
 (0)