Feat/add bing search (#2379)

This commit is contained in:
Yeuoly 2024-02-05 12:38:47 +08:00 committed by GitHub
parent 56c25bfb78
commit 6278ff0f30
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 63 additions and 10 deletions

View File

@ -28,6 +28,9 @@ class BingSearchTool(BuiltinTool):
if not query: if not query:
raise Exception('query is required') raise Exception('query is required')
limit = min(tool_parameters.get('limit', 5), 10)
result_type = tool_parameters.get('result_type', 'text') or 'text'
market = tool_parameters.get('market', 'US') market = tool_parameters.get('market', 'US')
lang = tool_parameters.get('language', 'en') lang = tool_parameters.get('language', 'en')
@ -49,13 +52,24 @@ class BingSearchTool(BuiltinTool):
raise Exception(f'Error {response.status_code}: {response.text}') raise Exception(f'Error {response.status_code}: {response.text}')
response = response.json() response = response.json()
# get the first 5 results search_results = response['webPages']['value'][:limit]
search_results = response['webPages']['value'][:5]
results = []
for result in search_results:
results.append(self.create_text_message(
text=f'{result["name"]}: {result["url"]}'
))
return results if result_type == 'link':
results = []
for result in search_results:
results.append(self.create_text_message(
text=f'{result["name"]}: {result["url"]}'
))
return results
else:
# construct text
text = ''
for i, result in enumerate(search_results):
text += f'{i+1}: {result["name"]} - {result["snippet"]}\n'
text += '\n\nRelated Searches:\n'
for related in response['relatedSearches']['value']:
text += f'{related["displayText"]} - {related["webSearchUrl"]}\n'
return self.create_text_message(text=self.summary(user_id=user_id, content=text))

View File

@ -15,6 +15,7 @@ parameters:
- name: query - name: query
type: string type: string
required: true required: true
form: llm
label: label:
en_US: Query string en_US: Query string
zh_Hans: 查询语句 zh_Hans: 查询语句
@ -24,7 +25,45 @@ parameters:
zh_Hans: 用于搜索网页内容 zh_Hans: 用于搜索网页内容
pt_BR: used for searching pt_BR: used for searching
llm_description: key words for searching llm_description: key words for searching
form: llm - name: limit
type: number
required: false
form: form
label:
en_US: Limit for results length
zh_Hans: 返回长度限制
pt_BR: Limit for results length
human_description:
en_US: limit the number of results
zh_Hans: 限制返回结果的数量
pt_BR: limit the number of results
min: 1
max: 10
default: 5
- name: result_type
type: select
required: false
label:
en_US: result type
zh_Hans: 结果类型
pt_BR: result type
human_description:
en_US: return a list of links or texts
zh_Hans: 返回一个连接列表还是纯文本内容
pt_BR: return a list of links or texts
default: text
options:
- value: link
label:
en_US: Link
zh_Hans: 链接
pt_BR: Link
- value: text
label:
en_US: Text
zh_Hans: 文本
pt_BR: Text
form: form
- name: market - name: market
type: select type: select
label: label: