-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathbuiltin_tool.py
More file actions
52 lines (38 loc) · 1.8 KB
/
builtin_tool.py
File metadata and controls
52 lines (38 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
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
43
44
45
46
47
48
49
50
51
52
from __future__ import annotations
from collections.abc import Sequence
from dataclasses import dataclass
from typing import Any
import pydantic
from pydantic_ai.builtin_tools import AbstractBuiltinTool
from pydantic_ai.tools import AgentBuiltinTool, AgentDepsT
from .abstract import AbstractCapability
_BUILTIN_TOOL_ADAPTER = pydantic.TypeAdapter(AbstractBuiltinTool)
@dataclass
class BuiltinTool(AbstractCapability[AgentDepsT]):
"""A capability that registers a builtin tool with the agent.
Wraps a single [`AgentBuiltinTool`][pydantic_ai.tools.AgentBuiltinTool] — either a static
[`AbstractBuiltinTool`][pydantic_ai.builtin_tools.AbstractBuiltinTool] instance or a callable
that dynamically produces one.
When `builtin_tools` is passed to [`Agent.__init__`][pydantic_ai.agent.Agent.__init__], each item is
automatically wrapped in a `BuiltinTool` capability.
"""
tool: AgentBuiltinTool[AgentDepsT]
def get_builtin_tools(self) -> Sequence[AgentBuiltinTool[AgentDepsT]]:
return [self.tool]
@classmethod
def from_spec(cls, tool: AbstractBuiltinTool | None = None, **kwargs: Any) -> BuiltinTool[Any]:
"""Create from spec.
Supports two YAML forms:
- Flat: `{BuiltinTool: {kind: web_search, search_context_size: high}}`
- Explicit: `{BuiltinTool: {tool: {kind: web_search}}}`
"""
if tool is not None:
validated = _BUILTIN_TOOL_ADAPTER.validate_python(tool)
elif kwargs:
validated = _BUILTIN_TOOL_ADAPTER.validate_python(kwargs)
else:
raise TypeError(
'`BuiltinTool.from_spec()` requires either a `tool` argument or keyword arguments'
' specifying the builtin tool type (e.g. `kind="web_search"`)'
)
return cls(tool=validated)