-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsearch.py
More file actions
51 lines (46 loc) · 1.84 KB
/
search.py
File metadata and controls
51 lines (46 loc) · 1.84 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
45
46
47
48
49
50
51
from prompt import rewrite_search_agent_prompt
from planner import agent
import json
import serpapi
import requests
class BingSearchAPI:
def __init__(self):
with open("keys/bingsearchapi_key.json", "r") as f:
data = json.load(f)
self.api_key = data["api_key"]
self.mkt = data['mkt']
self.endpoint = data['endpoint']
def search(self, query: str):
params = { 'q': query, 'mkt': self.mkt }
headers = { 'Ocp-Apim-Subscription-Key': self.api_key }
try:
response = requests.get(self.endpoint, headers=headers, params=params)
response.raise_for_status()
search_result = [response.json()['webPages']['value'][i]['snippet'] for i in range(10)]
# print(search_result)
prompt = rewrite_search_agent_prompt % (query, search_result)
for i in range(10):
res = agent(prompt)
if isinstance(res['data'], dict):
output = res['data']['response']['choices'][0]['message']['content']
prompt_tokens = res['data']['response']['usage']['prompt_tokens']
completion_tokens = res['data']['response']['usage']['completion_tokens']
break
except:
output = "Search failed."
prompt_tokens = 0
completion_tokens = 0
return output, prompt_tokens, completion_tokens
def search(self, query: str, use_date=False):
params = {
"q": query,
"location": self.location,
"api_key": self.api_key
}
try:
search = serpapi.search(params)
result = search.as_dict()
output = self.process_result(result, query, use_date=use_date)
except:
output = "Search failed."
return output