-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
- bing | ||
- duckduckgo | ||
- searchapi | ||
- serper | ||
- searxng | ||
- dalle | ||
- azuredalle | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
44 changes: 44 additions & 0 deletions
44
api/core/tools/provider/builtin/serper/tools/serper_search.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
27 changes: 27 additions & 0 deletions
27
api/core/tools/provider/builtin/serper/tools/serper_search.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |