Skip to content

Commit 5fa7e69

Browse files
committed
Substitute the agent runner with DurableOpenAIRunner
1 parent 31e9e5a commit 5fa7e69

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import asyncio
2+
import logging
3+
from typing import Any, Callable, Optional
4+
import azure.functions as func
5+
6+
import json
7+
import typing
8+
from dataclasses import replace
9+
from typing import Any, Union
10+
11+
from agents import (
12+
Agent,
13+
RunConfig,
14+
RunResult,
15+
RunResultStreaming,
16+
SQLiteSession,
17+
TContext,
18+
Tool,
19+
TResponseInputItem,
20+
)
21+
from agents.run import DEFAULT_AGENT_RUNNER, DEFAULT_MAX_TURNS, AgentRunner
22+
from pydantic_core import to_json
23+
24+
logger = logging.getLogger(__name__)
25+
26+
class DurableOpenAIRunner:
27+
def __init__(self) -> None:
28+
self._runner = DEFAULT_AGENT_RUNNER or AgentRunner()
29+
30+
def run_sync(
31+
self,
32+
starting_agent: Agent[TContext],
33+
input: Union[str, list[TResponseInputItem]],
34+
**kwargs: Any,
35+
) -> RunResult:
36+
# workaround for https://github.com/pydantic/pydantic/issues/9541
37+
# ValidatorIterator returned
38+
input_json = to_json(input)
39+
input = json.loads(input_json)
40+
41+
context = kwargs.get("context")
42+
max_turns = kwargs.get("max_turns", DEFAULT_MAX_TURNS)
43+
hooks = kwargs.get("hooks")
44+
run_config = kwargs.get("run_config")
45+
previous_response_id = kwargs.get("previous_response_id")
46+
session = kwargs.get("session")
47+
48+
if run_config is None:
49+
run_config = RunConfig()
50+
51+
model_name = run_config.model or starting_agent.model
52+
if model_name is not None and not isinstance(model_name, str):
53+
raise ValueError(
54+
"Durable Functions require a model name to be a string in the run config and/or agent."
55+
)
56+
# updated_run_config = replace(
57+
# run_config,
58+
# model=_TemporalModelStub(
59+
# model_name=model_name
60+
# ),
61+
# )
62+
updated_run_config = run_config
63+
64+
return self._runner.run_sync(
65+
starting_agent=starting_agent,
66+
input=input,
67+
context=context,
68+
max_turns=max_turns,
69+
hooks=hooks,
70+
run_config=updated_run_config,
71+
previous_response_id=previous_response_id,
72+
session=session,
73+
)
74+
75+
def run(
76+
self,
77+
starting_agent: Agent[TContext],
78+
input: Union[str, list[TResponseInputItem]],
79+
**kwargs: Any,
80+
) -> RunResult:
81+
raise RuntimeError("Durable Functions do not support asynchronous runs.")
82+
83+
def run_streamed(
84+
self,
85+
starting_agent: Agent[TContext],
86+
input: Union[str, list[TResponseInputItem]],
87+
**kwargs: Any,
88+
) -> RunResultStreaming:
89+
raise RuntimeError("Durable Functions do not support streaming.")

samples-v2/openai_agents/function_app.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import azure.functions as func
22
import logging
33

4+
from agents.run import set_default_agent_runner
5+
from durable_openai_runner import DurableOpenAIRunner
6+
7+
set_default_agent_runner(DurableOpenAIRunner())
8+
49

510
app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)
611

0 commit comments

Comments
 (0)