Skip to content

Commit

Permalink
feat: add the tool Serper for Google search. (#6786) (#6790)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwzhuhao authored Aug 2, 2024
1 parent 048bc4c commit 541bf1d
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/core/tools/provider/_position.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- bing
- duckduckgo
- searchapi
- serper
- searxng
- dalle
- azuredalle
Expand Down
12 changes: 12 additions & 0 deletions api/core/tools/provider/builtin/serper/_assets/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions api/core/tools/provider/builtin/serper/serper.py
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))
31 changes: 31 additions & 0 deletions api/core/tools/provider/builtin/serper/serper.yaml
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 api/core/tools/provider/builtin/serper/tools/serper_search.py
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 api/core/tools/provider/builtin/serper/tools/serper_search.yaml
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

0 comments on commit 541bf1d

Please sign in to comment.