-
Notifications
You must be signed in to change notification settings - Fork 49
/
tavily.py
275 lines (237 loc) · 10.7 KB
/
tavily.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
import requests
import json
import warnings
import os
from typing import Literal, Sequence, Optional, List, Union
from concurrent.futures import ThreadPoolExecutor, as_completed
from .utils import get_max_items_from_list
from .errors import UsageLimitExceededError, InvalidAPIKeyError, MissingAPIKeyError, BadRequestError
class TavilyClient:
"""
Tavily API client class.
"""
def __init__(self, api_key: Optional[str] = None):
if api_key is None:
api_key = os.getenv("TAVILY_API_KEY")
if not api_key:
raise MissingAPIKeyError()
self.base_url = "https://api.tavily.com"
self.api_key = api_key
self.headers = {
"Content-Type": "application/json",
}
def _search(self,
query: str,
search_depth: Literal["basic", "advanced"] = "basic",
topic: Literal["general", "news"] = "general",
days: int = 3,
max_results: int = 5,
include_domains: Sequence[str] = None,
exclude_domains: Sequence[str] = None,
include_answer: bool = False,
include_raw_content: bool = False,
include_images: bool = False,
**kwargs
) -> dict:
"""
Internal search method to send the request to the API.
"""
data = {
"query": query,
"search_depth": search_depth,
"topic": topic,
"days": days,
"include_answer": include_answer,
"include_raw_content": include_raw_content,
"max_results": max_results,
"include_domains": include_domains,
"exclude_domains": exclude_domains,
"include_images": include_images,
"api_key": self.api_key,
}
if kwargs:
data.update(kwargs)
response = requests.post(self.base_url + "/search", data=json.dumps(data), headers=self.headers, timeout=100)
if response.status_code == 200:
return response.json()
elif response.status_code == 429:
detail = 'Too many requests.'
try:
detail = response.json()['detail']['error']
except:
pass
raise UsageLimitExceededError(detail)
elif response.status_code == 401:
raise InvalidAPIKeyError()
else:
response.raise_for_status() # Raises a HTTPError if the HTTP request returned an unsuccessful status code
def search(self,
query: str,
search_depth: Literal["basic", "advanced"] = "basic",
topic: Literal["general", "news"] = "general",
days: int = 3,
max_results: int = 5,
include_domains: Sequence[str] = None,
exclude_domains: Sequence[str] = None,
include_answer: bool = False,
include_raw_content: bool = False,
include_images: bool = False,
**kwargs, # Accept custom arguments
) -> dict:
"""
Combined search method.
"""
response_dict = self._search(query,
search_depth=search_depth,
topic=topic,
days=days,
max_results=max_results,
include_domains=include_domains,
exclude_domains=exclude_domains,
include_answer=include_answer,
include_raw_content=include_raw_content,
include_images=include_images,
**kwargs,
)
tavily_results = response_dict.get("results", [])
response_dict["results"] = tavily_results
return response_dict
def _extract(self,
urls: Union[List[str], str],
**kwargs
) -> dict:
"""
Internal extract method to send the request to the API.
"""
data = {
"urls": urls,
"api_key": self.api_key
}
if kwargs:
data.update(kwargs)
response = requests.post(self.base_url + "/extract", data=json.dumps(data), headers=self.headers, timeout=100)
if response.status_code == 200:
return response.json()
elif response.status_code == 400:
detail = 'Bad request. The request was invalid or cannot be served.'
try:
detail = response.json()['detail']['error']
except KeyError:
pass
raise BadRequestError(detail)
elif response.status_code == 401:
raise InvalidAPIKeyError()
elif response.status_code == 429:
detail = 'Too many requests.'
try:
detail = response.json()['detail']['error']
except:
pass
raise UsageLimitExceededError(detail)
else:
response.raise_for_status() # Raises a HTTPError if the HTTP request returned an unsuccessful status code
def extract(self,
urls: Union[List[str], str], # Accept a list of URLs or a single URL
**kwargs, # Accept custom arguments
) -> dict:
"""
Combined extract method.
"""
response_dict = self._extract(urls,
**kwargs)
tavily_results = response_dict.get("results", [])
failed_results = response_dict.get("failed_results", [])
response_dict["results"] = tavily_results
response_dict["failed_results"] = failed_results
return response_dict
def get_search_context(self,
query: str,
search_depth: Literal["basic", "advanced"] = "basic",
topic: Literal["general", "news"] = "general",
days: int = 3,
max_results: int = 5,
include_domains: Sequence[str] = None,
exclude_domains: Sequence[str] = None,
max_tokens: int = 4000,
**kwargs, # Accept custom arguments
) -> str:
"""
Get the search context for a query. Useful for getting only related content from retrieved websites
without having to deal with context extraction and limitation yourself.
max_tokens: The maximum number of tokens to return (based on openai token compute). Defaults to 4000.
Returns a string of JSON containing the search context up to context limit.
"""
response_dict = self._search(query,
search_depth=search_depth,
topic=topic,
days=days,
max_results=max_results,
include_domains=include_domains,
exclude_domains=exclude_domains,
include_answer=False,
include_raw_content=False,
include_images=False,
**kwargs,
)
sources = response_dict.get("results", [])
context = [{"url": source["url"], "content": source["content"]} for source in sources]
return json.dumps(get_max_items_from_list(context, max_tokens))
def qna_search(self,
query: str,
search_depth: Literal["basic", "advanced"] = "advanced",
topic: Literal["general", "news"] = "general",
days: int = 3,
max_results: int = 5,
include_domains: Sequence[str] = None,
exclude_domains: Sequence[str] = None,
**kwargs, # Accept custom arguments
) -> str:
"""
Q&A search method. Search depth is advanced by default to get the best answer.
"""
response_dict = self._search(query,
search_depth=search_depth,
topic=topic,
days=days,
max_results=max_results,
include_domains=include_domains,
exclude_domains=exclude_domains,
include_raw_content=False,
include_images=False,
include_answer=True,
**kwargs,
)
return response_dict.get("answer", "")
def get_company_info(self,
query: str,
search_depth: Literal["basic", "advanced"] = "advanced",
max_results: int = 5,
) -> Sequence[dict]:
""" Company information search method. Search depth is advanced by default to get the best answer. """
def _perform_search(topic):
return self._search(query,
search_depth=search_depth,
topic=topic,
max_results=max_results,
include_answer=False, )
with ThreadPoolExecutor() as executor:
# Initiate the search for each topic in parallel
future_to_topic = {executor.submit(_perform_search, topic): topic for topic in
["news", "general", "finance"]}
all_results = []
# Process the results as they become available
for future in as_completed(future_to_topic):
data = future.result()
if 'results' in data:
all_results.extend(data['results'])
# Sort all the results by score in descending order and take the top 'max_results' items
sorted_results = sorted(all_results, key=lambda x: x['score'], reverse=True)[:max_results]
return sorted_results
class Client(TavilyClient):
"""
Tavily API client class.
WARNING! This class is deprecated. Please use TavilyClient instead.
"""
def __init__(self, kwargs):
warnings.warn("Client is deprecated, please use TavilyClient instead", DeprecationWarning, stacklevel=2)
super().__init__(kwargs)