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
223 lines (184 loc) · 6.3 KB
/
lifecycle.py
File metadata and controls
223 lines (184 loc) · 6.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
from typing import Any, Generic, Optional
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)
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.
"""
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_authorize(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
tool: Tool,
) -> bool:
"""Called before a tool is executed. Return False to deny the tool call.
When False is returned, the tool is not invoked and the model receives a
denial message instead. All on_tool_start and on_tool_end hooks are
skipped for denied calls.
Default: always returns True (allow all tool calls).
Args:
context: The run context wrapper.
agent: The current agent.
tool: The tool about to be invoked.
Returns:
True to allow the tool call, False to deny it.
"""
return True
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
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.
"""
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_authorize(
self,
context: RunContextWrapper[TContext],
agent: TAgent,
tool: Tool,
) -> bool:
"""Called before a tool is executed. Return False to deny the tool call.
When False is returned, the tool is not invoked and the model receives a
denial message instead. All on_tool_start and on_tool_end hooks are
skipped for denied calls.
Default: always returns True (allow all tool calls).
Args:
context: The run context wrapper.
agent: The current agent.
tool: The tool about to be invoked.
Returns:
True to allow the tool call, False to deny it.
"""
return True
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_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."""