跳转到内容

pydantic_ai.builtin_tools

AbstractBuiltinTool dataclass

基类:ABC

一个可供代理(agent)使用的内置工具。

此类是抽象的,不能直接实例化。

内置工具作为 ModelRequestParameters 的一部分传递给模型。

源代码位于 pydantic_ai_slim/pydantic_ai/builtin_tools.py
12
13
14
15
16
17
18
19
@dataclass(kw_only=True)
class AbstractBuiltinTool(ABC):
    """A builtin tool that can be used by an agent.

    This class is abstract and cannot be instantiated directly.

    The builtin tools are passed to the model as part of the `ModelRequestParameters`.
    """

WebSearchTool dataclass

基类:AbstractBuiltinTool

一个内置工具,允许您的代理在网络上搜索信息。

PydanticAI 传递的参数取决于模型,因为某些模型可能不支持某些参数。

支持的模型: * Anthropic * OpenAI * Groq * Google

源代码位于 pydantic_ai_slim/pydantic_ai/builtin_tools.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
@dataclass(kw_only=True)
class WebSearchTool(AbstractBuiltinTool):
    """A builtin tool that allows your agent to search the web for information.

    The parameters that PydanticAI passes depend on the model, as some parameters may not be supported by certain models.

    Supported by:
    * Anthropic
    * OpenAI
    * Groq
    * Google
    """

    search_context_size: Literal['low', 'medium', 'high'] = 'medium'
    """The `search_context_size` parameter controls how much context is retrieved from the web to help the tool formulate a response.

    Supported by:
    * OpenAI
    """

    user_location: WebSearchUserLocation | None = None
    """The `user_location` parameter allows you to localize search results based on a user's location.

    Supported by:
    * Anthropic
    * OpenAI
    """

    blocked_domains: list[str] | None = None
    """If provided, these domains will never appear in results.

    With Anthropic, you can only use one of `blocked_domains` or `allowed_domains`, not both.

    Supported by:
    * Anthropic (https://docs.anthropic.com/en/docs/build-with-claude/tool-use/web-search-tool#domain-filtering)
    * Groq (https://console.groq.com/docs/agentic-tooling#search-settings)
    """

    allowed_domains: list[str] | None = None
    """If provided, only these domains will be included in results.

    With Anthropic, you can only use one of `blocked_domains` or `allowed_domains`, not both.

    Supported by:
    * Anthropic (https://docs.anthropic.com/en/docs/build-with-claude/tool-use/web-search-tool#domain-filtering)
    * Groq (https://console.groq.com/docs/agentic-tooling#search-settings)
    """

    max_uses: int | None = None
    """If provided, the tool will stop searching the web after the given number of uses.

    Supported by:
    * Anthropic
    """

search_context_size class-attribute instance-attribute

search_context_size: Literal["low", "medium", "high"] = (
    "medium"
)

search_context_size 参数控制从网络检索多少上下文来帮助工具形成响应。

支持的模型: * OpenAI

user_location class-attribute instance-attribute

user_location: WebSearchUserLocation | None = None

user_location 参数允许您根据用户的位置对搜索结果进行本地化。

支持的模型: * Anthropic * OpenAI

blocked_domains class-attribute instance-attribute

blocked_domains: list[str] | None = None

如果提供,这些域名将永远不会出现在结果中。

对于 Anthropic,您只能使用 blocked_domainsallowed_domains 中的一个,不能同时使用两者。

支持的模型: * Anthropic (https://docs.anthropic.com/en/docs/build-with-claude/tool-use/web-search-tool#domain-filtering) * Groq (https://console.groq.com/docs/agentic-tooling#search-settings)

allowed_domains class-attribute instance-attribute

allowed_domains: list[str] | None = None

如果提供,结果中将只包含这些域名。

对于 Anthropic,您只能使用 blocked_domainsallowed_domains 中的一个,不能同时使用两者。

支持的模型: * Anthropic (https://docs.anthropic.com/en/docs/build-with-claude/tool-use/web-search-tool#domain-filtering) * Groq (https://console.groq.com/docs/agentic-tooling#search-settings)

max_uses class-attribute instance-attribute

max_uses: int | None = None

如果提供,该工具将在达到给定的使用次数后停止网络搜索。

支持的模型: * Anthropic

WebSearchUserLocation

基类:TypedDict

允许您根据用户的位置对搜索结果进行本地化。

支持的模型: * Anthropic * OpenAI

源代码位于 pydantic_ai_slim/pydantic_ai/builtin_tools.py
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class WebSearchUserLocation(TypedDict, total=False):
    """Allows you to localize search results based on a user's location.

    Supported by:
    * Anthropic
    * OpenAI
    """

    city: str
    """The city where the user is located."""

    country: str
    """The country where the user is located. For OpenAI, this must be a 2-letter country code (e.g., 'US', 'GB')."""

    region: str
    """The region or state where the user is located."""

    timezone: str
    """The timezone of the user's location."""

city instance-attribute

city: str

用户所在的城市。

country instance-attribute

country: str

用户所在的国家。对于 OpenAI,这必须是 2 个字母的国家代码(例如,'US'、'GB')。

region instance-attribute

region: str

用户所在的地区或州。

timezone instance-attribute

timezone: str

用户所在位置的时区。

CodeExecutionTool dataclass

基类:AbstractBuiltinTool

一个内置工具,允许您的代理执行代码。

支持的模型: * Anthropic * OpenAI * Google

源代码位于 pydantic_ai_slim/pydantic_ai/builtin_tools.py
 99
100
101
102
103
104
105
106
class CodeExecutionTool(AbstractBuiltinTool):
    """A builtin tool that allows your agent to execute code.

    Supported by:
    * Anthropic
    * OpenAI
    * Google
    """

UrlContextTool dataclass

基类:AbstractBuiltinTool

允许您的代理访问 URL 的内容。

支持的模型: * Google

源代码位于 pydantic_ai_slim/pydantic_ai/builtin_tools.py
109
110
111
112
113
114
class UrlContextTool(AbstractBuiltinTool):
    """Allows your agent to access contents from URLs.

    Supported by:
    * Google
    """