pydantic_ai.tools
AgentDepsT module-attribute
AgentDepsT = TypeVar(
"AgentDepsT", default=None, contravariant=True
)
代理依赖项的类型变量。
RunContext dataclass
Bases: Generic[AgentDepsT]
关于当前调用的信息。
Source code in pydantic_ai_slim/pydantic_ai/tools.py
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 |
|
messages class-attribute
instance-attribute
messages: list[ModelMessage] = field(default_factory=list)
到目前为止在对话中交换的消息。
SystemPromptFunc module-attribute
SystemPromptFunc = Union[
Callable[[RunContext[AgentDepsT]], str],
Callable[[RunContext[AgentDepsT]], Awaitable[str]],
Callable[[], str],
Callable[[], Awaitable[str]],
]
一个可能接受或不接受 RunContext
作为参数,并且可能是异步或非异步的函数。
用法 SystemPromptFunc[AgentDepsT]
。
ToolFuncContext module-attribute
ToolFuncContext = Callable[
Concatenate[RunContext[AgentDepsT], ToolParams], Any
]
一个将 RunContext
作为第一个参数的工具函数。
用法 ToolContextFunc[AgentDepsT, ToolParams]
。
ToolFuncPlain module-attribute
ToolFuncPlain = Callable[ToolParams, Any]
一个不将 RunContext
作为第一个参数的工具函数。
用法 ToolPlainFunc[ToolParams]
。
ToolFuncEither module-attribute
ToolFuncEither = Union[
ToolFuncContext[AgentDepsT, ToolParams],
ToolFuncPlain[ToolParams],
]
ToolPrepareFunc module-attribute
ToolPrepareFunc: TypeAlias = (
"Callable[[RunContext[AgentDepsT], ToolDefinition], Awaitable[ToolDefinition | None]]"
)
可以在调用时准备工具定义的函数的定义。
有关更多信息,请参阅 工具文档。
示例 — 此处 only_if_42
作为 ToolPrepareFunc
有效
from typing import Union
from pydantic_ai import RunContext, Tool
from pydantic_ai.tools import ToolDefinition
async def only_if_42(
ctx: RunContext[int], tool_def: ToolDefinition
) -> Union[ToolDefinition, None]:
if ctx.deps == 42:
return tool_def
def hitchhiker(ctx: RunContext[int], answer: str) -> str:
return f'{ctx.deps} {answer}'
hitchhiker = Tool(hitchhiker, prepare=only_if_42)
用法 ToolPrepareFunc[AgentDepsT]
。
DocstringFormat module-attribute
DocstringFormat = Literal[
"google", "numpy", "sphinx", "auto"
]
Tool dataclass
Bases: Generic[AgentDepsT]
代理的工具函数。
Source code in pydantic_ai_slim/pydantic_ai/tools.py
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
|
__init__
__init__(
function: ToolFuncEither[AgentDepsT],
*,
takes_ctx: bool | None = None,
max_retries: int | None = None,
name: str | None = None,
description: str | None = None,
prepare: ToolPrepareFunc[AgentDepsT] | None = None,
docstring_format: DocstringFormat = "auto",
require_parameter_descriptions: bool = False,
schema_generator: type[
GenerateJsonSchema
] = GenerateToolJsonSchema
)
创建一个新的工具实例。
用法示例
from pydantic_ai import Agent, RunContext, Tool
async def my_tool(ctx: RunContext[int], x: int, y: int) -> str:
return f'{ctx.deps} {x} {y}'
agent = Agent('test', tools=[Tool(my_tool)])
或使用自定义准备方法
from typing import Union
from pydantic_ai import Agent, RunContext, Tool
from pydantic_ai.tools import ToolDefinition
async def my_tool(ctx: RunContext[int], x: int, y: int) -> str:
return f'{ctx.deps} {x} {y}'
async def prep_my_tool(
ctx: RunContext[int], tool_def: ToolDefinition
) -> Union[ToolDefinition, None]:
# only register the tool if `deps == 42`
if ctx.deps == 42:
return tool_def
agent = Agent('test', tools=[Tool(my_tool, prepare=prep_my_tool)])
参数
名称 | 类型 | 描述 | 默认值 |
---|---|---|---|
函数
|
ToolFuncEither[AgentDepsT]
|
要作为工具调用的 Python 函数。 |
必需 |
takes_ctx
|
bool | None
|
该函数是否将 |
None
|
max_retries
|
int | None
|
此工具允许的最大重试次数,如果为 |
None
|
名称
|
str | None
|
工具的名称,如果为 |
None
|
描述
|
str | None
|
工具的描述,如果为 |
None
|
prepare
|
ToolPrepareFunc[AgentDepsT] | None
|
用于为每个步骤准备工具定义的自定义方法,返回 |
None
|
docstring_format
|
DocstringFormat
|
文档字符串的格式,请参阅 |
'auto'
|
require_parameter_descriptions
|
bool
|
如果为 True,则在缺少参数描述时引发错误。 默认为 False。 |
False
|
schema_generator
|
type[GenerateJsonSchema]
|
要使用的 JSON schema 生成器类。 默认为 |
GenerateToolJsonSchema
|
Source code in pydantic_ai_slim/pydantic_ai/tools.py
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 |
|
prepare_tool_def async
prepare_tool_def(
ctx: RunContext[AgentDepsT],
) -> ToolDefinition | None
获取工具定义。
默认情况下,此方法创建一个工具定义,然后返回它,或者在设置了 self.prepare
时调用它。
返回
类型 | 描述 |
---|---|
ToolDefinition | None
|
如果不应为此运行注册工具,则返回 |
Source code in pydantic_ai_slim/pydantic_ai/tools.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
run async
run(
message: ToolCallPart,
run_context: RunContext[AgentDepsT],
) -> ToolReturnPart | RetryPromptPart
异步运行工具函数。
Source code in pydantic_ai_slim/pydantic_ai/tools.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
ObjectJsonSchema module-attribute
表示对象 JSON schema 的类型,例如 "type": "object"
的情况。
此类型用于在 ToolDefinition 中定义工具参数(又名参数)。
使用 PEP-728,这应该是一个带有 type: Literal['object']
和 extra_parts=Any
的 TypedDict。
ToolDefinition dataclass
传递给模型的工具的定义。
这用于函数工具和结果工具。
Source code in pydantic_ai_slim/pydantic_ai/tools.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
|
parameters_json_schema instance-attribute
parameters_json_schema: ObjectJsonSchema
工具参数的 JSON schema。
outer_typed_dict_key class-attribute
instance-attribute
outer_typed_dict_key: str | None = None
包装结果工具的外部 [TypedDict] 中的键。
这仅会为没有 object
JSON schema 的结果工具设置。