Skip to content

Commit 31e9e5a

Browse files
committed
Use synchronous Runner.run_sync and call agent directly from the orchestrator
1 parent 955cdbd commit 31e9e5a

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

samples-v2/openai_agents/basic/hello_world.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
from agents import Agent, Runner
44

5+
# Ensure event loop is created
6+
try:
7+
asyncio.get_running_loop()
8+
except RuntimeError:
9+
loop = asyncio.new_event_loop()
10+
asyncio.set_event_loop(loop)
511

6-
async def main():
12+
def main():
713
agent = Agent(
814
name="Assistant",
915
instructions="You only respond in haikus.",
1016
)
1117

12-
result = await Runner.run(agent, "Tell me about recursion in programming.")
18+
result = Runner.run_sync(agent, "Tell me about recursion in programming.")
1319
return result.final_output
1420
# Function calls itself,
1521
# Looping in smaller pieces,
1622
# Endless by design.
17-
18-
19-
if __name__ == "__main__":
20-
asyncio.run(main())
Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import azure.functions as func
22
import logging
33

4+
45
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
56

67
@app.route(route="orchestrators/{functionName}")
@@ -14,16 +15,7 @@ async def hello_orchestration_starter(req: func.HttpRequest, client):
1415

1516
@app.orchestration_trigger(context_name="context")
1617
def basic_hello_world_orchestrator(context):
17-
result = yield context.call_activity("openai_agent_activity")
18-
return result
19-
20-
# Activity for OpenAI agent execution
21-
@app.activity_trigger(input_name="input")
22-
async def openai_agent_activity(input: str):
23-
# TODO: Instead of wrapping this code in an activity function like this,
24-
# we should be able to invoke it from the orchestrator directly.
25-
# In order to enable this, Runner.run invocations should be implicitly
26-
# wrapped in activity invocations.
2718
from basic.hello_world import main
28-
result = await main()
19+
result = main()
2920
return result
21+

0 commit comments

Comments
 (0)