|
1 | | -from dataclasses import dataclass |
2 | | -from typing import Literal |
| 1 | +from dataclasses import KW_ONLY, dataclass |
| 2 | +from functools import partial |
| 3 | +from inspect import signature |
| 4 | +from typing import Literal, overload |
3 | 5 |
|
4 | 6 | from pydantic import TypeAdapter |
5 | 7 | from typing_extensions import Any, TypedDict |
|
16 | 18 |
|
17 | 19 | __all__ = ('tavily_search_tool',) |
18 | 20 |
|
| 21 | +_UNSET: Any = object() |
| 22 | +"""Sentinel to distinguish "not provided" from None in factory kwargs.""" |
| 23 | + |
19 | 24 |
|
20 | 25 | class TavilySearchResult(TypedDict): |
21 | 26 | """A Tavily search result. |
@@ -44,38 +49,133 @@ class TavilySearchTool: |
44 | 49 | client: AsyncTavilyClient |
45 | 50 | """The Tavily search client.""" |
46 | 51 |
|
| 52 | + _: KW_ONLY |
| 53 | + |
| 54 | + max_results: int | None = None |
| 55 | + """The maximum number of results. If None, the Tavily default is used.""" |
| 56 | + |
47 | 57 | async def __call__( |
48 | 58 | self, |
49 | 59 | query: str, |
50 | | - search_deep: Literal['basic', 'advanced'] = 'basic', |
51 | | - topic: Literal['general', 'news'] = 'general', |
52 | | - time_range: Literal['day', 'week', 'month', 'year', 'd', 'w', 'm', 'y'] | None = None, |
| 60 | + search_depth: Literal['basic', 'advanced', 'fast', 'ultra-fast'] = 'basic', |
| 61 | + topic: Literal['general', 'news', 'finance'] = 'general', |
| 62 | + time_range: Literal['day', 'week', 'month', 'year'] | None = None, |
| 63 | + include_domains: list[str] | None = None, |
| 64 | + exclude_domains: list[str] | None = None, |
53 | 65 | ) -> list[TavilySearchResult]: |
54 | 66 | """Searches Tavily for the given query and returns the results. |
55 | 67 |
|
56 | 68 | Args: |
57 | 69 | query: The search query to execute with Tavily. |
58 | | - search_deep: The depth of the search. |
| 70 | + search_depth: The depth of the search. |
59 | 71 | topic: The category of the search. |
60 | 72 | time_range: The time range back from the current date to filter results. |
| 73 | + include_domains: List of domains to specifically include in the search results. |
| 74 | + exclude_domains: List of domains to specifically exclude from the search results. |
61 | 75 |
|
62 | 76 | Returns: |
63 | 77 | A list of search results from Tavily. |
64 | 78 | """ |
65 | | - results = await self.client.search(query, search_depth=search_deep, topic=topic, time_range=time_range) # type: ignore[reportUnknownMemberType] |
| 79 | + results: dict[str, Any] = await self.client.search( # pyright: ignore[reportUnknownMemberType,reportUnknownVariableType] |
| 80 | + query, |
| 81 | + search_depth=search_depth, |
| 82 | + topic=topic, |
| 83 | + time_range=time_range, # pyright: ignore[reportArgumentType] |
| 84 | + max_results=self.max_results, # pyright: ignore[reportArgumentType] |
| 85 | + include_domains=include_domains, # pyright: ignore[reportArgumentType] |
| 86 | + exclude_domains=exclude_domains, # pyright: ignore[reportArgumentType] |
| 87 | + ) |
66 | 88 | return tavily_search_ta.validate_python(results['results']) |
67 | 89 |
|
68 | 90 |
|
69 | | -def tavily_search_tool(api_key: str): |
| 91 | +@overload |
| 92 | +def tavily_search_tool( |
| 93 | + api_key: str, |
| 94 | + *, |
| 95 | + max_results: int | None = None, |
| 96 | + search_depth: Literal['basic', 'advanced', 'fast', 'ultra-fast'] = _UNSET, |
| 97 | + topic: Literal['general', 'news', 'finance'] = _UNSET, |
| 98 | + time_range: Literal['day', 'week', 'month', 'year'] | None = _UNSET, |
| 99 | + include_domains: list[str] | None = _UNSET, |
| 100 | + exclude_domains: list[str] | None = _UNSET, |
| 101 | +) -> Tool[Any]: ... |
| 102 | + |
| 103 | + |
| 104 | +@overload |
| 105 | +def tavily_search_tool( |
| 106 | + *, |
| 107 | + client: AsyncTavilyClient, |
| 108 | + max_results: int | None = None, |
| 109 | + search_depth: Literal['basic', 'advanced', 'fast', 'ultra-fast'] = _UNSET, |
| 110 | + topic: Literal['general', 'news', 'finance'] = _UNSET, |
| 111 | + time_range: Literal['day', 'week', 'month', 'year'] | None = _UNSET, |
| 112 | + include_domains: list[str] | None = _UNSET, |
| 113 | + exclude_domains: list[str] | None = _UNSET, |
| 114 | +) -> Tool[Any]: ... |
| 115 | + |
| 116 | + |
| 117 | +def tavily_search_tool( |
| 118 | + api_key: str | None = None, |
| 119 | + *, |
| 120 | + client: AsyncTavilyClient | None = None, |
| 121 | + max_results: int | None = None, |
| 122 | + search_depth: Literal['basic', 'advanced', 'fast', 'ultra-fast'] = _UNSET, |
| 123 | + topic: Literal['general', 'news', 'finance'] = _UNSET, |
| 124 | + time_range: Literal['day', 'week', 'month', 'year'] | None = _UNSET, |
| 125 | + include_domains: list[str] | None = _UNSET, |
| 126 | + exclude_domains: list[str] | None = _UNSET, |
| 127 | +) -> Tool[Any]: |
70 | 128 | """Creates a Tavily search tool. |
71 | 129 |
|
| 130 | + `max_results` is always developer-controlled and does not appear in the LLM tool schema. |
| 131 | + Other parameters, when provided, are fixed for all searches and hidden from the LLM's |
| 132 | + tool schema. Parameters left unset remain available for the LLM to set per-call. |
| 133 | +
|
72 | 134 | Args: |
73 | | - api_key: The Tavily API key. |
| 135 | + api_key: The Tavily API key. Required if `client` is not provided. |
74 | 136 |
|
75 | 137 | You can get one by signing up at [https://app.tavily.com/home](https://app.tavily.com/home). |
| 138 | + client: An existing AsyncTavilyClient. If provided, `api_key` is ignored. |
| 139 | + This is useful for sharing a client across multiple tool instances. |
| 140 | + max_results: The maximum number of results. If None, the Tavily default is used. |
| 141 | + search_depth: The depth of the search. |
| 142 | + topic: The category of the search. |
| 143 | + time_range: The time range back from the current date to filter results. |
| 144 | + include_domains: List of domains to specifically include in the search results. |
| 145 | + exclude_domains: List of domains to specifically exclude from the search results. |
76 | 146 | """ |
| 147 | + if client is None: |
| 148 | + if api_key is None: |
| 149 | + raise ValueError('Either api_key or client must be provided') |
| 150 | + client = AsyncTavilyClient(api_key) |
| 151 | + func = TavilySearchTool(client=client, max_results=max_results).__call__ |
| 152 | + |
| 153 | + kwargs: dict[str, Any] = {} |
| 154 | + if search_depth is not _UNSET: |
| 155 | + kwargs['search_depth'] = search_depth |
| 156 | + if topic is not _UNSET: |
| 157 | + kwargs['topic'] = topic |
| 158 | + if time_range is not _UNSET: |
| 159 | + kwargs['time_range'] = time_range |
| 160 | + if include_domains is not _UNSET: |
| 161 | + kwargs['include_domains'] = include_domains |
| 162 | + if exclude_domains is not _UNSET: |
| 163 | + kwargs['exclude_domains'] = exclude_domains |
| 164 | + |
| 165 | + if kwargs: |
| 166 | + original = func |
| 167 | + func = partial(func, **kwargs) |
| 168 | + func.__name__ = original.__name__ # type: ignore[union-attr] |
| 169 | + func.__qualname__ = original.__qualname__ |
| 170 | + # partial with keyword args only updates defaults, not removes params. |
| 171 | + # Set __signature__ explicitly to exclude bound params from the tool schema. |
| 172 | + orig_sig = signature(original) |
| 173 | + func.__signature__ = orig_sig.replace( # type: ignore[attr-defined] |
| 174 | + parameters=[p for name, p in orig_sig.parameters.items() if name not in kwargs] |
| 175 | + ) |
| 176 | + |
77 | 177 | return Tool[Any]( |
78 | | - TavilySearchTool(client=AsyncTavilyClient(api_key)).__call__, |
| 178 | + func, # pyright: ignore[reportArgumentType] |
79 | 179 | name='tavily_search', |
80 | 180 | description='Searches Tavily for the given query and returns the results.', |
81 | 181 | ) |
0 commit comments