-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
280 lines (232 loc) · 8.29 KB
/
search.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
276
277
278
279
280
import aiohttp
from bs4 import BeautifulSoup
import json
import asyncio
import google.generativeai as genai
from pprint import pprint
from PIL import Image
from prompts import prompts
from ai import Model
from urllib.parse import urlparse, ParseResult
from context import get_context
import json
from dotenv import load_dotenv
from os import getenv
load_dotenv()
GOOGLE_SEARCH: str = getenv("GOOGLE_SEARCH")
def get_domain_name(url: str) -> str:
parsed_url: ParseResult = urlparse(url)
domain: str = parsed_url.netloc
# Remove 'www.' if present
if domain.startswith("www."):
domain = domain[4:]
return domain
async def fetch(session: aiohttp.ClientSession, url: str) -> str:
"""
Fetches the content of a webpage.
Args:
session (aiohttp.ClientSession): The aiohttp session to use for the request.
url (str): The URL of the webpage to fetch.
Returns:
str: The content of the webpage.
"""
try:
async with session.get(url) as response:
return await response.text(encoding="utf-8", errors="ignore")
except Exception as e:
print("ERROR", url, e)
return ""
async def search_with_serper(
query: str,
) -> dict[str, dict[str, str] | list[dict[str, str]]]:
url = "https://google.serper.dev/search"
payload = json.dumps(
{
"q": query,
"num": 10,
}
)
headers = {"X-API-KEY": GOOGLE_SEARCH, "Content-Type": "application/json"}
async with aiohttp.ClientSession() as session:
async with session.post(
url, headers=headers, data=payload, timeout=10
) as response:
json_content = await response.json(encoding="utf-8")
json_content = json.dumps(json_content)
json_content = json.loads(json_content)
return json_content
async def search_google(query: str):
"""
Search with serper and return the contexts.
"""
json_content = await search_with_serper(query)
try:
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"]
]
return contexts[:8]
except KeyError as e:
return []
# asyncio.run(search_google("What is the capital of France?"))
async def get_main_content(url: str) -> str:
"""
Get the main content of a webpage.
Args:
url (str): The URL of the webpage to get the main content from.
Returns:
str: The main content of the webpage.
"""
print("Fetching text", end=" - ")
headers = {"User-Agent": "Mozilla/5.0"}
async with aiohttp.ClientSession(headers=headers) as session:
html = await fetch(session, url)
soup = BeautifulSoup(html, "lxml")
# Remove script and style elements
for script in soup(["script", "style"]):
script.extract()
# Get text
text = soup.get_text(separator="\n", strip=True)
print("Done fetching text", end=" - ")
return text
filters = {
"404 Not Found",
"Not Found\nPage not foundThe page you are looking for doesn't exist or has been moved.",
}
async def cleanup(question: str, text: str, model: Model, pic) -> str:
"""
Cleans up the text and only sends the important text relating to the question.
Args:
question (str): The question.
text (str): The text to clean up.
model (genai.GenerativeModel): The GenerativeModel instance to use for generating content.
Returns:
str: The cleaned up text.
"""
print("Clean up", end=" - ")
message = "[search][cleanup]"
try:
response = await model.prompt(message, pic, question=question, text=text)
except Exception as e:
print("ERROR", e)
return
print("Done cleaning up", end=" - ")
return response
async def search(
question: str, model: Model, pic: Image.Image | None
) -> tuple[str, list[dict[str, str]]] | tuple[None, None]:
"""
Searches Google for the given question and returns the search results.
Args:
question (str): The question to search Google for.
model (genai.GenerativeModel): The GenerativeModel instance to use for generating content.
Returns:
tuple[str, list[dict[str, str]]] | tuple[None, None]: The formatted search results and the search results.
"""
if pic is not None:
prompt = "[search][search_query_img]"
else:
prompt = "[search][search_query]"
search_query = await model.prompt(prompt, pic, question=question)
print("Search query:", search_query)
print()
# search google
results: list[dict[str, str]] = await search_google(search_query)
print("Results:", bool(results), len(results))
"""
results = [
{
"name": "Title of the search result",
"snippet": "Snippet of the search result",
"url": "URL of the search result"
}, ...
]
"""
formatted_results = "\n".join(
[
f"{i}: {r['name']}\n{get_domain_name(r['url'])}\n{r['snippet']}\n"
for i, r in enumerate(results, 1)
]
)
prompt = "[search][followup]"
response = await model.prompt(
prompt,
pic,
question=question,
formatted_results=formatted_results,
context=get_context(),
)
if "yes" in response.lower():
print("Yes, the search results are informative enough")
elif "more information" in response.lower():
print("More information required from the search results")
print(response)
response = response.lower().split("\n")[0]
web_num = int(response.split(":")[0]) - 1
try:
url = results[web_num]["url"]
except IndexError:
print("Invalid web number")
return None, None
content = await get_main_content(url)
cleaned_content = await cleanup(question, content, model, pic)
results[web_num]["content"] = cleaned_content
n = "\n"
formatted_results = "\n".join(
[
f"{i}: {r['name']}\n{r['url']}\n{r['snippet']}\n{r.get('content', '')}{n if r.get('content', '') else ''}"
for i, r in enumerate(results, 1)
]
)
else:
print(f"No, the search results are not informative enough.")
print("\n\nSearch results:")
print("--------------------------------------------------")
try:
print(formatted_results)
print("yayayayayayay")
except Exception:
if formatted_results:
formatted_results_copy = str(
formatted_results.encode("latin-1", "replace")
)
print(formatted_results_copy)
print("--------------------------------------------------")
# return None, None
print("\n\nSearch results:")
print("--------------------------------------------------")
try:
print(formatted_results)
except Exception:
if formatted_results:
formatted_results_copy = str(formatted_results.encode("latin-1", "replace"))
print(formatted_results_copy)
print("--------------------------------------------------")
return formatted_results, results