diff --git a/api/core/tools/provider/_position.yaml b/api/core/tools/provider/_position.yaml index 3a3ff64426..25d9f403a0 100644 --- a/api/core/tools/provider/_position.yaml +++ b/api/core/tools/provider/_position.yaml @@ -2,6 +2,7 @@ - bing - duckduckgo - searchapi +- serper - searxng - dalle - azuredalle diff --git a/api/core/tools/provider/builtin/serper/_assets/icon.svg b/api/core/tools/provider/builtin/serper/_assets/icon.svg new file mode 100644 index 0000000000..3f973a552e --- /dev/null +++ b/api/core/tools/provider/builtin/serper/_assets/icon.svg @@ -0,0 +1,12 @@ + + + serper + + + + + + + + + \ No newline at end of file diff --git a/api/core/tools/provider/builtin/serper/serper.py b/api/core/tools/provider/builtin/serper/serper.py new file mode 100644 index 0000000000..2a42109373 --- /dev/null +++ b/api/core/tools/provider/builtin/serper/serper.py @@ -0,0 +1,23 @@ +from typing import Any + +from core.tools.errors import ToolProviderCredentialValidationError +from core.tools.provider.builtin.serper.tools.serper_search import SerperSearchTool +from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController + + +class SerperProvider(BuiltinToolProviderController): + def _validate_credentials(self, credentials: dict[str, Any]) -> None: + try: + SerperSearchTool().fork_tool_runtime( + runtime={ + "credentials": credentials, + } + ).invoke( + user_id='', + tool_parameters={ + "query": "test", + "result_type": "link" + }, + ) + except Exception as e: + raise ToolProviderCredentialValidationError(str(e)) diff --git a/api/core/tools/provider/builtin/serper/serper.yaml b/api/core/tools/provider/builtin/serper/serper.yaml new file mode 100644 index 0000000000..b3b2d76c4b --- /dev/null +++ b/api/core/tools/provider/builtin/serper/serper.yaml @@ -0,0 +1,31 @@ +identity: + author: zhuhao + name: serper + label: + en_US: Serper + zh_Hans: Serper + pt_BR: Serper + description: + en_US: Serper is a powerful real-time search engine tool API that provides structured data from Google Search. + zh_Hans: Serper 是一个强大的实时搜索引擎工具API,可提供来自 Google 搜索引擎搜索的结构化数据。 + pt_BR: Serper is a powerful real-time search engine tool API that provides structured data from Google Search. + icon: icon.svg + tags: + - search +credentials_for_provider: + serperapi_api_key: + type: secret-input + required: true + label: + en_US: Serper API key + zh_Hans: Serper API key + pt_BR: Serper API key + placeholder: + en_US: Please input your Serper API key + zh_Hans: 请输入你的 Serper API key + pt_BR: Please input your Serper API key + help: + en_US: Get your Serper API key from Serper + zh_Hans: 从 Serper 获取您的 Serper API key + pt_BR: Get your Serper API key from Serper + url: https://serper.dev/api-key diff --git a/api/core/tools/provider/builtin/serper/tools/serper_search.py b/api/core/tools/provider/builtin/serper/tools/serper_search.py new file mode 100644 index 0000000000..24facaf4ec --- /dev/null +++ b/api/core/tools/provider/builtin/serper/tools/serper_search.py @@ -0,0 +1,44 @@ +from typing import Any, Union + +import requests + +from core.tools.entities.tool_entities import ToolInvokeMessage +from core.tools.tool.builtin_tool import BuiltinTool + +SERPER_API_URL = "https://google.serper.dev/search" + + +class SerperSearchTool(BuiltinTool): + + def _parse_response(self, response: dict) -> dict: + result = {} + if "knowledgeGraph" in response: + result["title"] = response["knowledgeGraph"].get("title", "") + result["description"] = response["knowledgeGraph"].get("description", "") + if "organic" in response: + result["organic"] = [ + { + "title": item.get("title", ""), + "link": item.get("link", ""), + "snippet": item.get("snippet", "") + } + for item in response["organic"] + ] + return result + def _invoke(self, + user_id: str, + tool_parameters: dict[str, Any], + ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: + params = { + "q": tool_parameters['query'], + "gl": "us", + "hl": "en" + } + headers = { + 'X-API-KEY': self.runtime.credentials['serperapi_api_key'], + 'Content-Type': 'application/json' + } + response = requests.get(url=SERPER_API_URL, params=params,headers=headers) + response.raise_for_status() + valuable_res = self._parse_response(response.json()) + return self.create_json_message(valuable_res) diff --git a/api/core/tools/provider/builtin/serper/tools/serper_search.yaml b/api/core/tools/provider/builtin/serper/tools/serper_search.yaml new file mode 100644 index 0000000000..e1c0a056e6 --- /dev/null +++ b/api/core/tools/provider/builtin/serper/tools/serper_search.yaml @@ -0,0 +1,27 @@ +identity: + name: serper + author: zhuhao + label: + en_US: Serper + zh_Hans: Serper + pt_BR: Serper +description: + human: + en_US: A tool for performing a Google search and extracting snippets and webpages.Input should be a search query. + zh_Hans: 一个用于执行 Google 搜索并提取片段和网页的工具。输入应该是一个搜索查询。 + pt_BR: A tool for performing a Google search and extracting snippets and webpages.Input should be a search query. + llm: A tool for performing a Google search and extracting snippets and webpages.Input should be a search query. +parameters: + - name: query + type: string + required: true + label: + en_US: Query string + zh_Hans: 查询语句 + pt_BR: Query string + human_description: + en_US: used for searching + zh_Hans: 用于搜索网页内容 + pt_BR: used for searching + llm_description: key words for searching + form: llm