Agents
ToolsToFinalOutputFunction
module-attribute
ToolsToFinalOutputFunction: TypeAlias = Callable[
[RunContextWrapper[TContext], list[FunctionToolResult]],
MaybeAwaitable[ToolsToFinalOutputResult],
]
一个接收运行上下文和工具结果列表的函数,返回一个ToolToFinalOutputResult。
工具最终输出结果
dataclass
Source code in src/agents/agent.py
StopAtTools
智能体
dataclass
基类: Generic[TContext]
代理(agent)是一种配置了指令、工具、防护措施、交接机制等的AI模型。
我们强烈建议传递instructions参数,这是代理的"系统提示"。此外,您可以传递handoff_description参数,这是一个人类可读的代理描述,当代理在工具/交接中使用时会用到。
代理(Agents)在上下文类型上是通用的。上下文是您创建的(可变)对象。它会被传递给工具函数、交接处理、防护机制等。
Source code in src/agents/agent.py
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 | |
使用说明
class-attribute
instance-attribute
instructions: (
str
| Callable[
[RunContextWrapper[TContext], Agent[TContext]],
MaybeAwaitable[str],
]
| None
) = None
代理的指令。当调用此代理时,将用作"系统提示"。描述代理应该做什么以及如何响应。
可以是一个字符串,也可以是一个为智能体动态生成指令的函数。如果提供的是函数,该函数将被传入上下文和智能体实例进行调用。它必须返回一个字符串。
交接描述
class-attribute
instance-attribute
代理的描述。当代理被用作交接时,这会用于让LLM知道它的功能以及何时调用它。
任务交接
class-attribute
instance-attribute
交接代理是主代理可以委派的子代理。您可以提供一个交接代理列表,主代理在相关情况下可以选择将任务委派给它们。这有助于实现关注点分离和模块化。
模型
class-attribute
instance-attribute
model: str | Model | None = None
调用LLM时使用的模型实现。
默认情况下,如果未设置,代理将使用model_settings.DEFAULT_MODEL中配置的默认模型。
模型设置
class-attribute
instance-attribute
model_settings: ModelSettings = field(
default_factory=ModelSettings
)
配置模型特定的调优参数(例如温度、top_p)。
mcp_servers
class-attribute
instance-attribute
mcp_servers: list[MCPServer] = field(default_factory=list)
一个Model Context Protocol服务器列表,代理可以使用这些服务器。每次代理运行时,都会从这些服务器中包含工具到可用工具列表中。
注意:您需要管理这些服务器的生命周期。具体来说,在将服务器传递给代理之前必须调用server.connect(),当不再需要服务器时必须调用server.cleanup()。
输入防护栏
class-attribute
instance-attribute
input_guardrails: list[InputGuardrail[TContext]] = field(
default_factory=list
)
在生成响应之前,与代理执行并行运行的一系列检查。仅当代理是链中的第一个代理时才会运行。
输出防护栏
class-attribute
instance-attribute
output_guardrails: list[OutputGuardrail[TContext]] = field(
default_factory=list
)
在生成响应后,对代理最终输出运行的一系列检查。 仅在代理产生最终输出时运行。
钩子
class-attribute
instance-attribute
hooks: AgentHooks[TContext] | None = None
一个接收该代理各种生命周期事件回调的类。
工具使用行为
class-attribute
instance-attribute
tool_use_behavior: (
Literal["run_llm_again", "stop_on_first_tool"]
| StopAtTools
| ToolsToFinalOutputFunction
) = "run_llm_again"
这允许您配置工具使用的处理方式。
- "run_llm_again":默认行为。工具运行后,LLM会接收结果并生成响应。
- "stop_on_first_tool":第一个工具调用的输出将作为最终输出。这意味着LLM不会处理工具调用的结果。
- 工具名称列表:如果调用了列表中的任何工具,代理将停止运行。最终输出将是第一个匹配工具调用的输出。LLM不会处理工具调用的结果。
- 函数:如果传入一个函数,它将被调用并传入运行上下文和工具结果列表。它必须返回一个ToolToFinalOutputResult,用于确定工具调用是否产生最终输出。
注意:此配置仅适用于FunctionTools。托管工具,如文件搜索、网络搜索等,始终由LLM处理。
reset_tool_choice
class-attribute
instance-attribute
是否在调用工具后将工具选择重置为默认值。默认为True。这可以确保代理不会陷入工具使用的无限循环。
克隆
clone(**kwargs: Any) -> Agent[TContext]
复制代理,并修改给定的参数。例如,您可以这样做:
Source code in src/agents/agent.py
as_tool
as_tool(
tool_name: str | None,
tool_description: str | None,
custom_output_extractor: Callable[
[RunResult], Awaitable[str]
]
| None = None,
) -> Tool
将此代理转换为工具,可供其他代理调用。
这与交接有以下两点不同: 1. 在交接过程中,新代理会接收对话历史记录。而在此工具中,新代理接收的是生成的输入。 2. 在交接过程中,新代理会接管对话。而在此工具中,新代理是被作为工具调用的,对话仍由原始代理继续。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
tool_name
|
str | None
|
工具的名称。如果未提供,将使用代理的名称。 |
required |
tool_description
|
str | None
|
该工具的说明,应指明其功能及适用场景。 |
required |
custom_output_extractor
|
Callable[[RunResult], Awaitable[str]] | None
|
一个用于从代理中提取输出的函数。如果未提供,则将使用代理的最后一条消息。 |
None
|
Source code in src/agents/agent.py
get_system_prompt
async
get_system_prompt(
run_context: RunContextWrapper[TContext],
) -> str | None
获取该代理的系统提示。
Source code in src/agents/agent.py
get_mcp_tools
async
get_mcp_tools() -> list[Tool]