pydantic_ai.toolsets
AbstractToolset
基类:ABC
, Generic[AgentDepsT]
工具集是代理(agent)可以使用的一系列工具的集合。
它负责
- 列出其包含的工具
- 验证工具的参数
- 调用工具
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
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 |
|
id abstractmethod
property
id: str | None
工具集的 ID,在注册到同一代理的所有工具集中必须是唯一的。
如果您正在实现一个用户可以多次实例化的具体实现,您应该让他们可以选择性地向构造函数传递一个自定义 ID,并在此处返回该 ID。
工具集需要有一个 ID 才能在像 Temporal 这样的持久化执行环境中使用,在这种情况下,ID 将用于标识工作流中工具集的活动。
__aenter__ 异步
__aenter__() -> Self
进入工具集上下文。
您可以在具体实现中在此处设置网络连接。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
98 99 100 101 102 103 |
|
__aexit__ async
退出工具集上下文。
您可以在具体实现中在此处拆除网络连接。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
105 106 107 108 109 110 |
|
get_tools abstractmethod
async
get_tools(
ctx: RunContext[AgentDepsT],
) -> dict[str, ToolsetTool[AgentDepsT]]
此工具集中可用的工具。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
112 113 114 115 |
|
call_tool abstractmethod
async
call_tool(
name: str,
tool_args: dict[str, Any],
ctx: RunContext[AgentDepsT],
tool: ToolsetTool[AgentDepsT],
) -> Any
使用给定的参数调用一个工具。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
name
|
str
|
要调用的工具的名称。 |
必需 |
tool_args
|
dict[str, Any]
|
传递给工具的参数。 |
必需 |
ctx
|
RunContext[AgentDepsT]
|
运行上下文。 |
必需 |
工具
|
ToolsetTool[AgentDepsT]
|
被调用的由 |
必需 |
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
117 118 119 120 121 122 123 124 125 126 127 128 129 |
|
应用
apply(
visitor: Callable[[AbstractToolset[AgentDepsT]], None],
) -> None
在所有“叶”工具集(即那些实现自有工具列表和调用的工具集)上运行一个访问者函数。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
131 132 133 |
|
访问并替换
visit_and_replace(
visitor: Callable[
[AbstractToolset[AgentDepsT]],
AbstractToolset[AgentDepsT],
],
) -> AbstractToolset[AgentDepsT]
在所有“叶”工具集(即那些实现自有工具列表和调用的工具集)上运行一个访问者函数,并用该函数的结果替换它们在层级结构中的位置。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
135 136 137 138 139 |
|
已过滤的
filtered(
filter_func: Callable[
[RunContext[AgentDepsT], ToolDefinition], bool
],
) -> FilteredToolset[AgentDepsT]
返回一个新的工具集,该工具集使用一个接受代理上下文和工具定义的过滤函数来过滤此工具集的工具。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
141 142 143 144 145 146 147 148 149 150 |
|
带前缀的
prefixed(prefix: str) -> PrefixedToolset[AgentDepsT]
返回一个新的工具集,该工具集为此工具集的工具名称添加前缀。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
152 153 154 155 156 157 158 159 |
|
已准备的
prepared(
prepare_func: ToolsPrepareFunc[AgentDepsT],
) -> PreparedToolset[AgentDepsT]
返回一个新的工具集,该工具集使用一个接受代理上下文和原始工具定义的准备函数来准备此工具集的工具。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
161 162 163 164 165 166 167 168 |
|
已重命名的
renamed(
name_map: dict[str, str],
) -> RenamedToolset[AgentDepsT]
返回一个新的工具集,该工具集使用一个将新名称映射到原始名称的字典来重命名此工具集的工具。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
170 171 172 173 174 175 176 177 |
|
需要批准的
approval_required(
approval_required_func: Callable[
[
RunContext[AgentDepsT],
ToolDefinition,
dict[str, Any],
],
bool,
] = lambda ctx, tool_def, tool_args: True
) -> ApprovalRequiredToolset[AgentDepsT]
返回一个新的工具集,该工具集要求对其包含的工具的(某些)调用需要经过批准。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/abstract.py
179 180 181 182 183 184 185 186 187 188 189 190 191 |
|
CombinedToolset dataclass
基类:AbstractToolset[AgentDepsT]
一个组合了多个工具集的工具集。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/combined.py
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 |
|
外部工具集
基类:AbstractToolset[AgentDepsT]
一种工具集,它包含的工具其结果将在调用它们的 Pydantic AI 代理运行之外产生。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/external.py
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 |
|
ApprovalRequiredToolset dataclass
一种工具集,它要求对其包含的工具的(某些)调用需要经过批准。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/approval_required.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
|
FilteredToolset dataclass
一种工具集,它使用一个接受代理上下文和工具定义的过滤函数来过滤其包含的工具。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/filtered.py
12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
函数工具集
基类:AbstractToolset[AgentDepsT]
一种工具集,允许将 Python 函数用作工具。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/function.py
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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
__init__
__init__(
tools: Sequence[
Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]
] = [],
*,
max_retries: int = 1,
id: str | None = None,
docstring_format: DocstringFormat = "auto",
require_parameter_descriptions: bool = False,
schema_generator: type[
GenerateJsonSchema
] = GenerateToolJsonSchema
)
构建一个新的函数工具集。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
工具
|
Sequence[Tool[AgentDepsT] | ToolFuncEither[AgentDepsT, ...]]
|
要添加到工具集中的工具。 |
[]
|
max_retries
|
int
|
在一次运行中,每个工具的最大重试次数。 |
1
|
id
|
str | None
|
一个可选的工具集唯一 ID。工具集需要有一个 ID 才能在像 Temporal 这样的持久化执行环境中使用,在这种情况下,ID 将用于标识工作流中工具集的活动。 |
None
|
docstring_format
|
DocstringFormat
|
工具文档字符串的格式,参见 |
'auto'
|
require_parameter_descriptions
|
bool
|
如果为 True,则在缺少参数描述时引发错误。默认为 False。适用于所有工具,除非在添加工具时被覆盖。 |
False
|
schema_generator
|
type[GenerateJsonSchema]
|
用于此工具的 JSON schema 生成器类。默认为 |
GenerateToolJsonSchema
|
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/function.py
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 |
|
工具
tool(
func: ToolFuncEither[AgentDepsT, ToolParams],
) -> ToolFuncEither[AgentDepsT, ToolParams]
tool(
*,
name: str | None = None,
retries: int | None = None,
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
docstring_format: DocstringFormat | None = None,
require_parameter_descriptions: bool | None = None,
schema_generator: (
type[GenerateJsonSchema] | None
) = None,
strict: bool | None = None,
requires_approval: bool = False
) -> Callable[
[ToolFuncEither[AgentDepsT, ToolParams]],
ToolFuncEither[AgentDepsT, ToolParams],
]
tool(
func: (
ToolFuncEither[AgentDepsT, ToolParams] | None
) = None,
/,
*,
name: str | None = None,
retries: int | None = None,
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
docstring_format: DocstringFormat | None = None,
require_parameter_descriptions: bool | None = None,
schema_generator: (
type[GenerateJsonSchema] | None
) = None,
strict: bool | None = None,
requires_approval: bool = False,
) -> Any
用于注册工具函数的装饰器,该函数接受 RunContext
作为其第一个参数。
可以装饰同步或异步函数。
通过检查文档字符串来提取工具描述和每个参数的描述,了解更多。
我们无法为每个可能的工具签名添加重载,因为返回类型是一个递归联合体,所以用 @toolset.tool
装饰的函数的签名是模糊的。
示例
from pydantic_ai import Agent, RunContext
from pydantic_ai.toolsets.function import FunctionToolset
toolset = FunctionToolset()
@toolset.tool
def foobar(ctx: RunContext[int], x: int) -> int:
return ctx.deps + x
@toolset.tool(retries=2)
async def spam(ctx: RunContext[str], y: float) -> float:
return ctx.deps + y
agent = Agent('test', toolsets=[toolset], deps_type=int)
result = agent.run_sync('foobar', deps=1)
print(result.output)
#> {"foobar":1,"spam":1.0}
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
func
|
ToolFuncEither[AgentDepsT, ToolParams] | None
|
要注册的工具函数。 |
None
|
name
|
str | None
|
工具的名称,默认为函数名。 |
None
|
retries
|
int | None
|
允许此工具的重试次数,默认为代理的默认重试次数,即 1。 |
None
|
prepare
|
ToolPrepareFunc[AgentDepsT] | None
|
为每个步骤准备工具定义的自定义方法,返回 |
None
|
docstring_format
|
DocstringFormat | None
|
文档字符串的格式,参见 |
None
|
require_parameter_descriptions
|
bool | None
|
如果为 True,则在缺少参数描述时引发错误。如果为 |
None
|
schema_generator
|
type[GenerateJsonSchema] | None
|
用于此工具的 JSON schema 生成器类。如果为 |
None
|
strict
|
bool | None
|
是否强制执行 JSON schema 合规性(仅影响 OpenAI)。有关更多信息,请参阅 |
None
|
requires_approval
|
bool
|
此工具是否需要人在回路中批准。默认为 False。更多信息请参见工具文档。 |
False
|
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/function.py
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 |
|
添加函数
add_function(
func: ToolFuncEither[AgentDepsT, ToolParams],
takes_ctx: bool | None = None,
name: str | None = None,
retries: int | None = None,
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
docstring_format: DocstringFormat | None = None,
require_parameter_descriptions: bool | None = None,
schema_generator: (
type[GenerateJsonSchema] | None
) = None,
strict: bool | None = None,
requires_approval: bool = False,
) -> None
将一个函数作为工具添加到工具集中。
可以接受同步或异步函数。
通过检查文档字符串来提取工具描述和每个参数的描述,了解更多。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
func
|
ToolFuncEither[AgentDepsT, ToolParams]
|
要注册的工具函数。 |
必需 |
takes_ctx
|
bool | None
|
函数是否接受 |
None
|
name
|
str | None
|
工具的名称,默认为函数名。 |
None
|
retries
|
int | None
|
允许此工具的重试次数,默认为代理的默认重试次数,即 1。 |
None
|
prepare
|
ToolPrepareFunc[AgentDepsT] | None
|
为每个步骤准备工具定义的自定义方法,返回 |
None
|
docstring_format
|
DocstringFormat | None
|
文档字符串的格式,参见 |
None
|
require_parameter_descriptions
|
bool | None
|
如果为 True,则在缺少参数描述时引发错误。如果为 |
None
|
schema_generator
|
type[GenerateJsonSchema] | None
|
用于此工具的 JSON schema 生成器类。如果为 |
None
|
strict
|
bool | None
|
是否强制执行 JSON schema 合规性(仅影响 OpenAI)。有关更多信息,请参阅 |
None
|
requires_approval
|
bool
|
此工具是否需要人在回路中批准。默认为 False。更多信息请参见工具文档。 |
False
|
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/function.py
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 |
|
添加工具
add_tool(tool: Tool[AgentDepsT]) -> None
将一个工具添加到工具集中。
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
工具
|
Tool[AgentDepsT]
|
要添加的工具。 |
必需 |
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/function.py
249 250 251 252 253 254 255 256 257 258 259 |
|
PrefixedToolset dataclass
一种工具集,它为其包含的工具名称添加前缀。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/prefixed.py
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 |
|
RenamedToolset dataclass
一种工具集,它使用一个将新名称映射到原始名称的字典来重命名其包含的工具。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/renamed.py
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 |
|
PreparedToolset dataclass
一种工具集,它使用一个接受代理上下文和原始工具定义的准备函数来准备其包含的工具。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/prepared.py
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 |
|
WrapperToolset dataclass
基类:AbstractToolset[AgentDepsT]
一种包装另一个工具集并委托给它的工具集。
有关更多信息,请参阅工具集文档。
源代码位于 pydantic_ai_slim/pydantic_ai/toolsets/wrapper.py
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 |
|
ToolsetFunc module-attribute
ToolsetFunc: TypeAlias = Callable[
[RunContext[AgentDepsT]],
AbstractToolset[AgentDepsT]
| None
| Awaitable[AbstractToolset[AgentDepsT] | None],
]
一个接受运行上下文并返回工具集的同步/异步函数。