Skip to content

Commit 01b6be0

Browse files
Add SearchApi description, _get_tool_labels, return results based on result_type
1 parent a03d2c6 commit 01b6be0

File tree

8 files changed

+39
-28
lines changed

8 files changed

+39
-28
lines changed

api/core/tools/provider/builtin/searchapi/searchapi.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Any
22

3+
from core.tools.entities.values import ToolLabelEnum
34
from core.tools.errors import ToolProviderCredentialValidationError
45
from core.tools.provider.builtin.searchapi.tools.google import GoogleTool
56
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
@@ -9,7 +10,7 @@ class SearchAPIProvider(BuiltinToolProviderController):
910
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
1011
try:
1112
GoogleTool().fork_tool_runtime(
12-
meta={
13+
runtime={
1314
"credentials": credentials,
1415
}
1516
).invoke(
@@ -20,4 +21,9 @@ def _validate_credentials(self, credentials: dict[str, Any]) -> None:
2021
},
2122
)
2223
except Exception as e:
23-
raise ToolProviderCredentialValidationError(str(e))
24+
raise ToolProviderCredentialValidationError(str(e))
25+
26+
def _get_tool_labels(self) -> list[ToolLabelEnum]:
27+
return [
28+
ToolLabelEnum.SEARCH
29+
]

api/core/tools/provider/builtin/searchapi/searchapi.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ identity:
66
zh_Hans: SearchApi
77
pt_BR: SearchApi
88
description:
9-
en_US: SearchApi
10-
zh_Hans: SearchApi
11-
pt_BR: SearchApi
9+
en_US: SearchApi is a robust real-time SERP API delivering structured data from a collection of search engines including Google Search, Google Jobs, YouTube, Google News, and many more.
10+
zh_Hans: SearchApi 是一个强大的实时 SERP API,可提供来自 Google 搜索、Google 招聘、YouTube、Google 新闻等搜索引擎集合的结构化数据。
11+
pt_BR: SearchApi is a robust real-time SERP API delivering structured data from a collection of search engines including Google Search, Google Jobs, YouTube, Google News, and many more.
1212
icon: icon.svg
1313
credentials_for_provider:
1414
searchapi_api_key:

api/core/tools/provider/builtin/searchapi/tools/google.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _process_response(res: dict, type: str) -> str:
4545
"""Process response from SearchAPI."""
4646
if "error" in res.keys():
4747
raise ValueError(f"Got error from SearchApi: {res['error']}")
48-
48+
4949
toret = ""
5050
if type == "text":
5151
if "answer_box" in res.keys() and "answer" in res["answer_box"].keys():
@@ -63,7 +63,7 @@ def _process_response(res: dict, type: str) -> str:
6363
elif type == "link":
6464
if "answer_box" in res.keys() and "organic_result" in res["answer_box"].keys():
6565
if "title" in res["answer_box"]["organic_result"].keys():
66-
toret = f"[{res['answer_box']['organic_result']['title']}]({res['answer_box']['organic_result']['link']})\n"
66+
toret = f"[{res['answer_box']['organic_result']['title']}]({res['answer_box']['organic_result']['link']})\n"
6767
elif "organic_results" in res.keys() and "link" in res["organic_results"][0].keys():
6868
toret = ""
6969
for item in res["organic_results"]:
@@ -81,9 +81,9 @@ def _process_response(res: dict, type: str) -> str:
8181
return toret
8282

8383
class GoogleTool(BuiltinTool):
84-
def _invoke(self,
84+
def _invoke(self,
8585
user_id: str,
86-
tool_parameters: dict[str, Any],
86+
tool_parameters: dict[str, Any],
8787
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
8888
"""
8989
Invoke the SearchApi tool.
@@ -95,8 +95,10 @@ def _invoke(self,
9595
gl = tool_parameters.get("gl", "us")
9696
hl = tool_parameters.get("hl", "en")
9797
location = tool_parameters.get("location", None)
98-
98+
9999
api_key = self.runtime.credentials['searchapi_api_key']
100100
result = SearchAPI(api_key).run(query, result_type=result_type, num=num, google_domain=google_domain, gl=gl, hl=hl, location=location)
101-
102-
return self.create_text_message(text=result)
101+
102+
if result_type == 'text':
103+
return self.create_text_message(text=result)
104+
return self.create_link_message(link=result)

api/core/tools/provider/builtin/searchapi/tools/google.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ parameters:
9696
zh_Hans: 指定每页显示的结果数。默认值为 10。最大数量 - 100,最小数量 - 1。
9797
pt_BR: Specifies the number of results to display per page. Default is 10. Max number - 100, min - 1.
9898
llm_description: Specifies the num of results to display per page.
99-
form: llm
99+
form: llm

api/core/tools/provider/builtin/searchapi/tools/google_jobs.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def _process_response(res: dict, type: str) -> str:
5050
if type == "text":
5151
if "jobs" in res.keys() and "title" in res["jobs"][0].keys():
5252
for item in res["jobs"]:
53-
toret += "title: " + item["title"] + "\n" + "company_name: " + item["company_name"] + "content: " + item["description"] + "\n"
53+
toret += "title: " + item["title"] + "\n" + "company_name: " + item["company_name"] + "content: " + item["description"] + "\n"
5454
if toret == "":
5555
toret = "No good search result found"
5656

@@ -63,9 +63,9 @@ def _process_response(res: dict, type: str) -> str:
6363
return toret
6464

6565
class GoogleJobsTool(BuiltinTool):
66-
def _invoke(self,
66+
def _invoke(self,
6767
user_id: str,
68-
tool_parameters: dict[str, Any],
68+
tool_parameters: dict[str, Any],
6969
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
7070
"""
7171
Invoke the SearchApi tool.
@@ -82,5 +82,7 @@ def _invoke(self,
8282

8383
api_key = self.runtime.credentials['searchapi_api_key']
8484
result = SearchAPI(api_key).run(query, result_type=result_type, google_domain=google_domain, gl=gl, hl=hl, location=location, ltype=ltype)
85-
86-
return self.create_text_message(text=result)
85+
86+
if result_type == 'text':
87+
return self.create_text_message(text=result)
88+
return self.create_link_message(link=result)

api/core/tools/provider/builtin/searchapi/tools/google_news.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def _process_response(res: dict, type: str) -> str:
4545
"""Process response from SearchAPI."""
4646
if "error" in res.keys():
4747
raise ValueError(f"Got error from SearchApi: {res['error']}")
48-
48+
4949
toret = ""
5050
if type == "text":
5151
if "organic_results" in res.keys() and "snippet" in res["organic_results"][0].keys():
@@ -69,9 +69,9 @@ def _process_response(res: dict, type: str) -> str:
6969
return toret
7070

7171
class GoogleNewsTool(BuiltinTool):
72-
def _invoke(self,
72+
def _invoke(self,
7373
user_id: str,
74-
tool_parameters: dict[str, Any],
74+
tool_parameters: dict[str, Any],
7575
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
7676
"""
7777
Invoke the SearchApi tool.
@@ -83,8 +83,10 @@ def _invoke(self,
8383
gl = tool_parameters.get("gl", "us")
8484
hl = tool_parameters.get("hl", "en")
8585
location = tool_parameters.get("location", None)
86-
86+
8787
api_key = self.runtime.credentials['searchapi_api_key']
8888
result = SearchAPI(api_key).run(query, result_type=result_type, num=num, google_domain=google_domain, gl=gl, hl=hl, location=location)
89-
90-
return self.create_text_message(text=result)
89+
90+
if result_type == 'text':
91+
return self.create_text_message(text=result)
92+
return self.create_link_message(link=result)

api/core/tools/provider/builtin/searchapi/tools/google_news.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,3 @@ parameters:
9797
pt_BR: Specifies the number of results to display per page. Default is 10. Max number - 100, min - 1.
9898
llm_description: Specifies the num of results to display per page.
9999
form: llm
100-

api/core/tools/provider/builtin/searchapi/tools/youtube_transcripts.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ def _process_response(res: dict) -> str:
5656
return toret
5757

5858
class YoutubeTranscriptsTool(BuiltinTool):
59-
def _invoke(self,
59+
def _invoke(self,
6060
user_id: str,
61-
tool_parameters: dict[str, Any],
61+
tool_parameters: dict[str, Any],
6262
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
6363
"""
6464
Invoke the SearchApi tool.
@@ -68,5 +68,5 @@ def _invoke(self,
6868

6969
api_key = self.runtime.credentials['searchapi_api_key']
7070
result = SearchAPI(api_key).run(video_id, language=language)
71-
71+
7272
return self.create_text_message(text=result)

0 commit comments

Comments
 (0)