-
Notifications
You must be signed in to change notification settings - Fork 1
/
search_engine.py
65 lines (60 loc) · 2.36 KB
/
search_engine.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
# search_engine.py
import json
import requests
from fastapi import HTTPException
from config import SERPER_API, SERPER_SEARCH_ENDPOINT, REFERENCE_COUNT, DEFAULT_SEARCH_ENGINE_TIMEOUT
def search_with_serper(query: str, subscription_key=SERPER_API, prints=False):
"""
Search with serper and return the contexts.
"""
payload = json.dumps({
"q": query,
"num": (
REFERENCE_COUNT
if REFERENCE_COUNT % 10 == 0
else (REFERENCE_COUNT // 10 + 1) * 10
),
})
headers = {"X-API-KEY": subscription_key, "Content-Type": "application/json"}
response = requests.post(
SERPER_SEARCH_ENDPOINT,
headers=headers,
data=payload,
timeout=DEFAULT_SEARCH_ENGINE_TIMEOUT,
)
if not response.ok:
raise HTTPException(response.status_code, "Search engine error.")
json_content = response.json()
if prints:
print(json_content)
print("\n\n\n-------------------------------------------------------------------------------\n\n\n")
try:
# convert to the same format as bing/google
contexts = []
if json_content.get("knowledgeGraph"):
url = json_content["knowledgeGraph"].get("descriptionUrl") or json_content["knowledgeGraph"].get("website")
snippet = json_content["knowledgeGraph"].get("description")
if url and snippet:
contexts.append({
"name": json_content["knowledgeGraph"].get("title",""),
"url": url,
"snippet": snippet
})
if json_content.get("answerBox"):
url = json_content["answerBox"].get("url")
snippet = json_content["answerBox"].get("snippet") or json_content["answerBox"].get("answer")
if url and snippet:
contexts.append({
"name": json_content["answerBox"].get("title",""),
"url": url,
"snippet": snippet
})
contexts += [
{"name": c["title"], "url": c["link"], "snippet": c.get("snippet","")}
for c in json_content["organic"]
]
if prints:
print(contexts[:REFERENCE_COUNT])
return contexts[:REFERENCE_COUNT]
except KeyError:
return []