Skip to content

Commit

Permalink
Added different search type
Browse files Browse the repository at this point in the history
  • Loading branch information
junytang committed Apr 11, 2024
1 parent d51714d commit d6c0b0e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 18 deletions.
3 changes: 2 additions & 1 deletion api/core/tools/provider/builtin/searxng/searxng.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def _validate_credentials(self, credentials: dict[str, Any]) -> None:
tool_parameters={
"query": "SearXNG",
"limit": 1,
"result_type": 'link'
"search_type": "page",
"result_type": "link"
},
)
except Exception as e:
Expand Down
66 changes: 57 additions & 9 deletions api/core/tools/provider/builtin/searxng/tools/searxng_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,40 @@ class SearXNGSearchTool(BuiltinTool):
Tool for performing a search using SearXNG engine.
"""

def _invoke_query(self, user_id: str, host: str, query: str, result_type: str, topK: int = 5) -> List[Dict]:
SEARCH_TYPE = {
"page": "general",
"news": "news",
"image": "images",
# "video": "videos",
# "file": "files"
}
LINK_FILED = {
"page": "url",
"news": "url",
"image": "img_src",
# "video": "iframe_src",
# "file": "magnetlink"
}
TEXT_FILED = {
"page": "content",
"news": "content",
"image": "img_src",
# "video": "iframe_src",
# "file": "magnetlink"
}

def _invoke_query(self, user_id: str, host: str, query: str, search_type: str, result_type: str, topK: int = 5) -> List[Dict]:
"""Run query and return the results."""

response = requests.get(host, params={"format": "json", "q": query})
search_type = search_type.lower()
if search_type not in self.SEARCH_TYPE.keys():
search_type= "page"

response = requests.get(host, params={
"q": query,
"format": "json",
"categories": self.SEARCH_TYPE[search_type]
})

if response.status_code != 200:
raise Exception(f'Error {response.status_code}: {response.text}')
Expand All @@ -36,16 +66,27 @@ def _invoke_query(self, user_id: str, host: str, query: str, result_type: str, t

if result_type == 'link':
results = []
for r in search_results:
results.append(self.create_text_message(
text=f'{r["title"]}: {r["url"]}'
))
if search_type == "page" or search_type == "news":
for r in search_results:
results.append(self.create_text_message(
text=f'{r["title"]}: {r.get(self.LINK_FILED[search_type], "")}'
))
elif search_type == "image":
for r in search_results:
results.append(self.create_image_message(
image=r.get(self.LINK_FILED[search_type], "")
))
else:
for r in search_results:
results.append(self.create_link_message(
link=r.get(self.LINK_FILED[search_type], "")
))

return results
else:
text = ''
for i, r in enumerate(search_results):
text += f'{i+1}: {r["title"]} - {r.get("content", "")}\n'
text += f'{i+1}: {r["title"]} - {r.get(self.TEXT_FILED[search_type], "")}\n'

return self.create_text_message(text=self.summary(user_id=user_id, content=text))

Expand All @@ -70,7 +111,14 @@ def _invoke(self, user_id: str, tool_parameters: dict[str, Any]) -> ToolInvokeMe
if not query:
return self.create_text_message('Please input query')

limit = min(tool_parameters.get('limit', 5), 20)
num_results = min(tool_parameters.get('num_results', 5), 20)
search_type = tool_parameters.get('search_type', 'page') or 'page'
result_type = tool_parameters.get('result_type', 'text') or 'text'

return self._invoke_query(user_id=user_id, host=host, query=query, result_type=result_type, topK=limit)
return self._invoke_query(
user_id=user_id,
host=host,
query=query,
search_type=search_type,
result_type=result_type,
topK=num_results)
45 changes: 37 additions & 8 deletions api/core/tools/provider/builtin/searxng/tools/searxng_search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,44 @@ parameters:
zh_Hans: 搜索查询语句。
llm_description: Key words for searching
form: llm
- name: limit
- name: search_type
type: select
required: true
label:
en_US: search type
zh_Hans: 搜索类型
pt_BR: search type
human_description:
en_US: search type for page, news or image.
zh_Hans: 选择搜索的类型:网页,新闻,图片。
pt_BR: search type for page, news or image.
default: Page
options:
- value: Page
label:
en_US: Page
zh_Hans: 网页
pt_BR: Page
- value: News
label:
en_US: News
zh_Hans: 新闻
pt_BR: News
- value: Image
label:
en_US: Image
zh_Hans: 图片
pt_BR: Image
form: form
- name: num_results
type: number
required: true
label:
en_US: Number of query result.
zh_Hans: 返回查询结果的数量。
en_US: Number of query results
zh_Hans: 返回查询数量
human_description:
en_US: The number of query result.
zh_Hans: 返回结果数量
en_US: The number of query results.
zh_Hans: 返回查询结果的数量
form: form
default: 5
min: 1
Expand All @@ -42,9 +71,9 @@ parameters:
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
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
Expand Down

0 comments on commit d6c0b0e

Please sign in to comment.