forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle.py
More file actions
275 lines (229 loc) · 8.3 KB
/
lifecycle.py
File metadata and controls
275 lines (229 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from __future__ import annotations
from typing import Any, Generic, Literal, Optional, Union
from typing_extensions import TypeVar
from .agent import Agent, AgentBase
from .items import ModelResponse, TResponseInputItem
from .run_context import AgentHookContext, RunContextWrapper, TContext
from .tool import Tool
TAgent = TypeVar("TAgent", bound=AgentBase, default=AgentBase)
TurnControl = Literal["continue", "stop"]
"""Return value for :meth:`RunHooksBase.on_turn_start` / :meth:`AgentHooksBase.on_turn_start`.
* ``"continue"`` (default / ``None``) – proceed with the turn as normal.
* ``"stop"`` – abort the run gracefully after this hook returns, exactly as if
``max_turns`` had been reached. The model is **not** called for this turn and
:meth:`on_turn_end` is **not** fired.
"""
class RunHooksBase(Generic[TContext, TAgent]):
"""A class that receives callbacks on various lifecycle events in an agent run. Subclass and
override the methods you need.
Turn-lifecycle hooks
--------------------
:meth:`on_turn_start` and :meth:`on_turn_end` fire once per iteration of the
agent loop. :meth:`on_turn_start` may return ``"stop"`` to halt the run
gracefully before the LLM is called for that turn (useful for implementing
custom turn-budget logic, external kill-switches, etc.).
"""
async def on_llm_start(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
system_prompt: Optional[str],
input_items: list[TResponseInputItem],
) -> None:
"""Called just before invoking the LLM for this agent."""
pass
async def on_llm_end(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
response: ModelResponse,
) -> None:
"""Called immediately after the LLM call returns for this agent."""
pass
async def on_agent_start(self, context: AgentHookContext[TContext], agent: TAgent) -> None:
"""Called before the agent is invoked. Called each time the current agent changes.
Args:
context: The agent hook context.
agent: The agent that is about to be invoked.
"""
pass
async def on_agent_end(
self,
context: AgentHookContext[TContext],
agent: TAgent,
output: Any,
) -> None:
"""Called when the agent produces a final output.
Args:
context: The agent hook context.
agent: The agent that produced the output.
output: The final output produced by the agent.
"""
pass
async def on_handoff(
self,
context: RunContextWrapper[TContext],
from_agent: TAgent,
to_agent: TAgent,
) -> None:
"""Called when a handoff occurs."""
pass
async def on_tool_start(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
tool: Tool,
) -> None:
"""Called immediately before a local tool is invoked."""
pass
async def on_tool_end(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
tool: Tool,
result: str,
) -> None:
"""Called immediately after a local tool is invoked."""
pass
async def on_turn_start(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
turn_number: int,
) -> Union[TurnControl, None]:
"""Called at the start of each agent turn, before the LLM is invoked.
Returning ``"stop"`` (or raising :class:`StopAgentRun`) will halt the run
gracefully — the model is **not** called for this turn and
:meth:`on_turn_end` is **not** fired. Returning ``None`` or ``"continue"``
proceeds normally.
Args:
context: The run context wrapper.
agent: The current agent.
turn_number: The 1-indexed turn number (increments each time through the
agent loop).
Returns:
``None`` / ``"continue"`` to proceed, or ``"stop"`` to halt the run.
"""
return None
async def on_turn_end(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
turn_number: int,
) -> None:
"""Called at the end of each agent turn, after all tool calls for that turn complete.
Args:
context: The run context wrapper.
agent: The current agent.
turn_number: The 1-indexed turn number.
"""
pass
class AgentHooksBase(Generic[TContext, TAgent]):
"""A class that receives callbacks on various lifecycle events for a specific agent. You can
set this on `agent.hooks` to receive events for that specific agent.
Subclass and override the methods you need.
Turn-lifecycle hooks
--------------------
:meth:`on_turn_start` and :meth:`on_turn_end` fire once per iteration of the
agent loop. :meth:`on_turn_start` may return ``"stop"`` to halt the run
gracefully before the LLM is called for that turn.
"""
async def on_start(self, context: AgentHookContext[TContext], agent: TAgent) -> None:
"""Called before the agent is invoked. Called each time the running agent is changed to this
agent.
Args:
context: The agent hook context.
agent: This agent instance.
"""
pass
async def on_end(
self,
context: AgentHookContext[TContext],
agent: TAgent,
output: Any,
) -> None:
"""Called when the agent produces a final output.
Args:
context: The agent hook context.
agent: This agent instance.
output: The final output produced by the agent.
"""
pass
async def on_handoff(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
source: TAgent,
) -> None:
"""Called when the agent is being handed off to. The `source` is the agent that is handing
off to this agent."""
pass
async def on_tool_start(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
tool: Tool,
) -> None:
"""Called immediately before a local tool is invoked."""
pass
async def on_tool_end(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
tool: Tool,
result: str,
) -> None:
"""Called immediately after a local tool is invoked."""
pass
async def on_turn_start(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
turn_number: int,
) -> Union[TurnControl, None]:
"""Called at the start of each agent turn, before the LLM is invoked.
Returning ``"stop"`` halts the run gracefully before the model is called.
Returning ``None`` or ``"continue"`` proceeds normally.
Args:
context: The run context wrapper.
agent: The current agent.
turn_number: The 1-indexed turn number (increments each time through the
agent loop).
Returns:
``None`` / ``"continue"`` to proceed, or ``"stop"`` to halt the run.
"""
return None
async def on_turn_end(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
turn_number: int,
) -> None:
"""Called at the end of each agent turn, after all tool calls for that turn complete.
Args:
context: The run context wrapper.
agent: The current agent.
turn_number: The 1-indexed turn number.
"""
pass
async def on_llm_start(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
system_prompt: Optional[str],
input_items: list[TResponseInputItem],
) -> None:
"""Called immediately before the agent issues an LLM call."""
pass
async def on_llm_end(
self,
context: RunContextWrapper[TContext],
agent: Agent[TContext],
response: ModelResponse,
) -> None:
"""Called immediately after the agent receives the LLM response."""
pass
RunHooks = RunHooksBase[TContext, Agent]
"""Run hooks when using `Agent`."""
AgentHooks = AgentHooksBase[TContext, Agent]
"""Agent hooks for `Agent`s."""