工具
工具
module-attribute
Tool = Union[
FunctionTool,
FileSearchTool,
WebSearchTool,
ComputerTool,
]
一个可以在代理中使用的工具。
FunctionToolResult
dataclass
Source code in src/agents/tool.py
FunctionTool
dataclass
一个封装函数的工具。在大多数情况下,您应该使用function_tool辅助工具来创建FunctionTool,因为它们可以让您轻松地封装Python函数。
Source code in src/agents/tool.py
on_invoke_tool
instance-attribute
on_invoke_tool: Callable[
[RunContextWrapper[Any], str], Awaitable[Any]
]
一个调用工具并传入给定上下文和参数的函数。传入的参数包括: 1. 工具运行的上下文。 2. 来自LLM的参数,以JSON字符串形式传递。
你必须返回工具输出的字符串表示形式,或者我们可以对其调用str()的内容。
如果出现错误,你可以抛出异常(这将导致运行失败)或返回一个字符串错误消息(该消息将被发送回LLM)。
文件搜索工具
dataclass
一个托管工具,允许LLM在向量存储中进行搜索。目前仅支持使用OpenAI模型,通过Responses API实现。
Source code in src/agents/tool.py
include_search_results
class-attribute
instance-attribute
是否在LLM生成的输出中包含搜索结果。
ranking_options
class-attribute
instance-attribute
搜索结果的排序选项。
网页搜索工具
dataclass
一个托管工具,允许LLM进行网络搜索。目前仅支持OpenAI模型,使用Responses API。
Source code in src/agents/tool.py
计算机工具
dataclass
一个托管工具,让LLM能够控制计算机。
Source code in src/agents/tool.py
default_tool_error_function
default_tool_error_function(
ctx: RunContextWrapper[Any], error: Exception
) -> str
默认的工具错误函数,仅返回通用错误信息。
Source code in src/agents/tool.py
function_tool
function_tool(
func: ToolFunction[...],
*,
name_override: str | None = None,
description_override: str | None = None,
docstring_style: DocstringStyle | None = None,
use_docstring_info: bool = True,
failure_error_function: ToolErrorFunction | None = None,
strict_mode: bool = True,
) -> FunctionTool
function_tool(
*,
name_override: str | None = None,
description_override: str | None = None,
docstring_style: DocstringStyle | None = None,
use_docstring_info: bool = True,
failure_error_function: ToolErrorFunction | None = None,
strict_mode: bool = True,
) -> Callable[[ToolFunction[...]], FunctionTool]
function_tool(
func: ToolFunction[...] | None = None,
*,
name_override: str | None = None,
description_override: str | None = None,
docstring_style: DocstringStyle | None = None,
use_docstring_info: bool = True,
failure_error_function: ToolErrorFunction
| None = default_tool_error_function,
strict_mode: bool = True,
) -> (
FunctionTool
| Callable[[ToolFunction[...]], FunctionTool]
)
用于从函数创建FunctionTool的装饰器。默认情况下,我们将: 1. 解析函数签名以创建工具参数的JSON模式。 2. 使用函数的文档字符串填充工具描述。 3. 使用函数的文档字符串填充参数描述。 文档字符串风格会被自动检测,但您可以覆盖它。
如果函数将RunContextWrapper作为第一个参数,它必须与使用该工具的代理的上下文类型匹配。
参数:
| 名称 | 类型 | 描述 | 默认值 |
|---|---|---|---|
func
|
ToolFunction[...] | None
|
需要封装的函数。 |
None
|
name_override
|
str | None
|
如果提供,将使用此名称作为工具名称,而非函数名称。 |
None
|
description_override
|
str | None
|
如果提供,将使用此描述作为工具说明,而非函数的文档字符串。 |
None
|
docstring_style
|
DocstringStyle | None
|
如果提供,则使用此样式作为工具的文档字符串。如果未提供,我们将尝试自动检测样式。 |
None
|
use_docstring_info
|
bool
|
如果为True,则使用函数的文档字符串来填充工具的 描述和参数描述。 |
True
|
failure_error_function
|
ToolErrorFunction | None
|
如果提供此函数,当工具调用失败时将使用它生成错误消息。该错误消息会发送给LLM。如果传入None,则不会发送错误消息,而是会抛出异常。 |
default_tool_error_function
|
strict_mode
|
bool
|
是否启用工具的JSON模式严格模式。我们强烈建议将其设置为True,因为这会增加正确JSON输入的可能性。如果设为False,则允许非严格的JSON模式。例如,如果参数有默认值,则该参数将变为可选,允许额外属性等。详情请参阅:https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses#supported-schemas |
True
|
Source code in src/agents/tool.py
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 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 305 306 307 308 309 310 | |