交接
HandoffInputFilter 模块属性
HandoffInputFilter: TypeAlias = Callable[
[HandoffInputData], MaybeAwaitable[HandoffInputData]
]
过滤传递给下一个代理的输入数据的一个函数。
HandoffHistoryMapper 模块属性
HandoffHistoryMapper: TypeAlias = Callable[
[list[TResponseInputItem]], list[TResponseInputItem]
]
将先前的对话记录映射到嵌套摘要负载的一个函数。
HandoffInputData 数据类
源代码在 src/agents/handoffs/__init__.py
new_items 实例属性
new_items: tuple[RunItem, ...]
在当前 agent 轮次中生成的新项目,包括触发 handoff 的项目以及代表 handoff 输出响应的工具输出消息。
run_context 类属性 实例属性
run_context: RunContextWrapper[Any] | None = None
调用 handoff 时运行上下文。请注意,由于此属性是后来添加的,因此为了向后兼容,它是可选的。
clone
clone(**kwargs: Any) -> HandoffInputData
制作 handoff 输入数据的副本,并更改给定的参数。例如,您可以
源代码在 src/agents/handoffs/__init__.py
Handoff 数据类
基类:Generic[TContext, TAgent]
Handoff 是指一个代理将任务委托给另一个代理。
例如,在客户支持场景中,您可能有一个“分诊代理”,它确定哪个代理应该处理用户的请求,以及专门处理不同领域的子代理,例如计费、帐户管理等。
源代码在 src/agents/handoffs/__init__.py
tool_description 实例属性
The description of the tool that represents the handoff.
on_invoke_handoff 实例属性
on_invoke_handoff: Callable[
[RunContextWrapper[Any], str], Awaitable[TAgent]
]
调用 handoff 的函数。
传递的参数是:(1)handoff 运行上下文,(2)来自 LLM 的参数作为 JSON 字符串(如果 input_json_schema 为空,则为空字符串)。必须返回一个代理。
input_filter 类属性 实例属性
input_filter: HandoffInputFilter | None = None
过滤传递给下一个代理的输入的函数。
默认情况下,新的代理会看到整个对话历史记录。在某些情况下,您可能希望过滤输入(例如,删除较早的输入或删除现有输入中的工具)。该函数接收到到目前为止的整个对话历史记录,包括触发 handoff 的输入项以及表示 handoff 工具输出的工具调用输出项。您可以根据需要修改输入历史记录或新项目。运行的下一个代理将收到 handoff_input_data.all_items。重要提示:在流式传输模式下,我们将不会对该函数的结果进行任何流式传输。之前生成的项目已经流式传输。
nest_handoff_history 类属性 实例属性
覆盖 run 级别的 nest_handoff_history 行为,仅针对此 handoff。
strict_json_schema 类属性 实例属性
输入 JSON 模式是否处于严格模式。我们强烈建议将其设置为 True,因为它增加了正确 JSON 输入的可能性。
is_enabled 类属性 实例属性
is_enabled: (
bool
| Callable[
[RunContextWrapper[Any], AgentBase[Any]],
MaybeAwaitable[bool],
]
) = True
handoff 是否启用。
一个布尔值或一个接受运行上下文和代理并返回 handoff 是否启用的可调用对象。您可以使用此方法根据您的上下文或状态动态启用或禁用 handoff。
default_handoff_history_mapper
default_handoff_history_mapper(
transcript: list[TResponseInputItem],
) -> list[TResponseInputItem]
返回一个总结对话记录的助手消息。
源代码在 src/agents/handoffs/history.py
get_conversation_history_wrappers
nest_handoff_history
nest_handoff_history(
handoff_input_data: HandoffInputData,
*,
history_mapper: HandoffHistoryMapper | None = None,
) -> HandoffInputData
总结下一个代理的先前对话记录。
源代码在 src/agents/handoffs/history.py
reset_conversation_history_wrappers
恢复默认的 <CONVERSATION HISTORY> 标记。
源代码在 src/agents/handoffs/history.py
set_conversation_history_wrappers
覆盖包装生成的对话摘要的标记。
传递 None 以保持任一侧不变。
源代码在 src/agents/handoffs/history.py
handoff
handoff(
agent: Agent[TContext],
*,
tool_name_override: str | None = None,
tool_description_override: str | None = None,
input_filter: Callable[
[HandoffInputData], HandoffInputData
]
| None = None,
nest_handoff_history: bool | None = None,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], Agent[Any]],
MaybeAwaitable[bool],
] = True,
) -> Handoff[TContext, Agent[TContext]]
handoff(
agent: Agent[TContext],
*,
on_handoff: OnHandoffWithInput[THandoffInput],
input_type: type[THandoffInput],
tool_description_override: str | None = None,
tool_name_override: str | None = None,
input_filter: Callable[
[HandoffInputData], HandoffInputData
]
| None = None,
nest_handoff_history: bool | None = None,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], Agent[Any]],
MaybeAwaitable[bool],
] = True,
) -> Handoff[TContext, Agent[TContext]]
handoff(
agent: Agent[TContext],
*,
on_handoff: OnHandoffWithoutInput,
tool_description_override: str | None = None,
tool_name_override: str | None = None,
input_filter: Callable[
[HandoffInputData], HandoffInputData
]
| None = None,
nest_handoff_history: bool | None = None,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], Agent[Any]],
MaybeAwaitable[bool],
] = True,
) -> Handoff[TContext, Agent[TContext]]
handoff(
agent: Agent[TContext],
tool_name_override: str | None = None,
tool_description_override: str | None = None,
on_handoff: OnHandoffWithInput[THandoffInput]
| OnHandoffWithoutInput
| None = None,
input_type: type[THandoffInput] | None = None,
input_filter: Callable[
[HandoffInputData], HandoffInputData
]
| None = None,
nest_handoff_history: bool | None = None,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], Agent[TContext]],
MaybeAwaitable[bool],
] = True,
) -> Handoff[TContext, Agent[TContext]]
从一个代理创建一个 handoff。
参数
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
agent
|
Agent[TContext]
|
要 handoff 的代理。 |
required |
tool_name_override
|
str | None
|
表示 handoff 的工具名称的可选覆盖。 |
None
|
tool_description_override
|
str | None
|
表示 handoff 的工具描述的可选覆盖。 |
None
|
on_handoff
|
OnHandoffWithInput[THandoffInput] | OnHandoffWithoutInput | None
|
调用 handoff 时运行的函数。 |
None
|
input_type
|
type[THandoffInput] | None
|
handoff 的输入类型。如果提供,则输入将根据此类型进行验证。仅当您传递接受输入的函数时才相关。 |
None
|
input_filter
|
Callable[[HandoffInputData], HandoffInputData] | None
|
过滤传递给下一个代理的输入的函数。 |
None
|
nest_handoff_history
|
bool | None
|
RunConfig 级别的 |
None
|
is_enabled
|
bool | Callable[[RunContextWrapper[Any], Agent[TContext]], MaybeAwaitable[bool]]
|
handoff 是否启用。可以是布尔值或接受运行上下文和代理并返回 handoff 是否启用的可调用对象。禁用的 handoff 在运行时对 LLM 隐藏。 |
True
|
源代码在 src/agents/handoffs/__init__.py
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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |