-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathweb_search.py
More file actions
44 lines (37 loc) · 1.32 KB
/
web_search.py
File metadata and controls
44 lines (37 loc) · 1.32 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
"""Diffbot web search API."""
from typing import TYPE_CHECKING, Any, Dict, List, Optional
if TYPE_CHECKING:
from .client import Diffbot, DiffbotAsync
WEB_SEARCH_BASE = "https://llm.diffbot.com/api/v1/web_search"
def web_search(
client: "Diffbot",
text: str,
*,
num_results: Optional[int] = None,
max_tokens: Optional[int] = None,
) -> Dict[str, Any]:
headers = {"Authorization": f"Bearer {client.token}"}
params: Dict[str, Any] = {"text": text}
if num_results is not None:
params["size"] = num_results
if max_tokens is not None:
params["maxTokens"] = max_tokens
response = client._http.get(client.web_search_url, headers=headers, params=params)
client._raise_for_status(response)
return response.json()
async def web_search_async(
client: "DiffbotAsync",
text: str,
*,
num_results: Optional[int] = None,
max_tokens: Optional[int] = None,
) -> Dict[str, Any]:
headers = {"Authorization": f"Bearer {client.token}"}
params: Dict[str, Any] = {"text": text}
if num_results is not None:
params["size"] = num_results
if max_tokens is not None:
params["maxTokens"] = max_tokens
response = await client._http.get(client.web_search_url, headers=headers, params=params)
client._raise_for_status(response)
return response.json()