-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathweb_fetch.py
More file actions
76 lines (61 loc) · 2.76 KB
/
web_fetch.py
File metadata and controls
76 lines (61 loc) · 2.76 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from __future__ import annotations
from collections.abc import Awaitable, Callable
from dataclasses import dataclass
from typing import Any, Literal
from pydantic_ai.builtin_tools import WebFetchTool
from pydantic_ai.tools import AgentDepsT, RunContext, Tool
from .builtin_or_local import BuiltinOrLocalTool
@dataclass(init=False)
class WebFetch(BuiltinOrLocalTool[AgentDepsT]):
"""URL fetching capability.
Uses the model's builtin URL fetching when available. No default local
fallback — provide a custom `local` tool if needed.
"""
allowed_domains: list[str] | None
"""Only fetch from these domains. Requires builtin support."""
blocked_domains: list[str] | None
"""Never fetch from these domains. Requires builtin support."""
max_uses: int | None
"""Maximum number of fetches per run. Requires builtin support."""
enable_citations: bool | None
"""Enable citations for fetched content. Builtin-only; ignored by local tools."""
max_content_tokens: int | None
"""Maximum content length in tokens. Builtin-only; ignored by local tools."""
def __init__(
self,
*,
builtin: WebFetchTool
| Callable[[RunContext[AgentDepsT]], Awaitable[WebFetchTool | None] | WebFetchTool | None]
| bool = True,
local: Tool[AgentDepsT] | Callable[..., Any] | Literal[False] | None = None,
allowed_domains: list[str] | None = None,
blocked_domains: list[str] | None = None,
max_uses: int | None = None,
enable_citations: bool | None = None,
max_content_tokens: int | None = None,
) -> None:
self.builtin = builtin
self.local = local
self.allowed_domains = allowed_domains
self.blocked_domains = blocked_domains
self.max_uses = max_uses
self.enable_citations = enable_citations
self.max_content_tokens = max_content_tokens
self.__post_init__()
def _default_builtin(self) -> WebFetchTool:
kwargs: dict[str, Any] = {}
if self.allowed_domains is not None:
kwargs['allowed_domains'] = self.allowed_domains
if self.blocked_domains is not None:
kwargs['blocked_domains'] = self.blocked_domains
if self.max_uses is not None:
kwargs['max_uses'] = self.max_uses
if self.enable_citations is not None:
kwargs['enable_citations'] = self.enable_citations
if self.max_content_tokens is not None:
kwargs['max_content_tokens'] = self.max_content_tokens
return WebFetchTool(**kwargs)
def _builtin_unique_id(self) -> str:
return WebFetchTool.kind
def _requires_builtin(self) -> bool:
return self.allowed_domains is not None or self.blocked_domains is not None or self.max_uses is not None