qibin0506 commited on
Commit
24db8bd
·
verified ·
1 Parent(s): 672ed15

Delete search.py

Browse files
Files changed (1) hide show
  1. search.py +0 -64
search.py DELETED
@@ -1,64 +0,0 @@
1
- import urllib.parse
2
- import requests
3
- import json
4
- import os
5
-
6
-
7
- def get_brave_search_api():
8
- headers = {
9
- 'Accept': 'application/json',
10
- 'Accept-Encoding': 'gzip',
11
- 'X-Subscription-Token': '<YOUR BRAVE API KEY>'
12
- }
13
-
14
- def search(q: str):
15
- q = urllib.parse.quote(q, safe='')
16
- url = f'https://api.search.brave.com/res/v1/web/search?q={q}&summary=1&search_lang=zh-hans'
17
- response = requests.get(url, headers=headers)
18
- if response.status_code != 200:
19
- return None
20
-
21
- json_ = json.loads(response.text)
22
- web_results = json_['web']['results'][:5]
23
-
24
- search_result = []
25
- for idx, web_result in enumerate(web_results):
26
- search_result.append("{}. {}\n".format(idx + 1, web_result['description']))
27
-
28
- return ''.join(search_result)
29
-
30
- return search
31
-
32
-
33
- def get_bochaai_search_api():
34
- headers = {
35
- 'Authorization': os.environ.get("SEARCH_KEY"),
36
- 'Content-Type': 'application/json'
37
- }
38
-
39
- def search(q: str):
40
- payload = json.dumps({
41
- "query": f"{q}",
42
- "summary": True,
43
- "freshness": "noLimit",
44
- "count": 1
45
- })
46
-
47
- response = requests.post('https://api.bochaai.com/v1/web-search', headers=headers, data=payload)
48
- if response.status_code != 200:
49
- return None
50
-
51
- json_ = json.loads(response.text)
52
- if json_['code'] != 200:
53
- return None
54
-
55
- search_result = []
56
- for idx, web_page in enumerate(json_['data']['webPages']['value']):
57
- search_result.append("{}. {}\n".format(idx + 1, web_page['summary'].replace('\n', ' ')))
58
-
59
- return ''.join(search_result)
60
-
61
- return search
62
-
63
- def get_search_api():
64
- return get_bochaai_search_api()