Skip to content

Commit 2ae6460

Browse files
Tuyohaisteven
and
steven
authored
Add googlenews tools from rapidapi (#10877)
Co-authored-by: steven <[email protected]>
1 parent 0067b16 commit 2ae6460

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import Any
2+
3+
from core.tools.errors import ToolProviderCredentialValidationError
4+
from core.tools.provider.builtin.rapidapi.tools.google_news import GooglenewsTool
5+
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
6+
7+
8+
class RapidapiProvider(BuiltinToolProviderController):
9+
def _validate_credentials(self, credentials: dict[str, Any]) -> None:
10+
try:
11+
GooglenewsTool().fork_tool_runtime(
12+
meta={
13+
"credentials": credentials,
14+
}
15+
).invoke(
16+
user_id="",
17+
tool_parameters={
18+
"language_region": "en-US",
19+
},
20+
)
21+
except Exception as e:
22+
raise ToolProviderCredentialValidationError(str(e))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
identity:
2+
name: rapidapi
3+
author: Steven Sun
4+
label:
5+
en_US: RapidAPI
6+
zh_Hans: RapidAPI
7+
description:
8+
en_US: RapidAPI is the world's largest API marketplace with over 1,000,000 developers and 10,000 APIs.
9+
zh_Hans: RapidAPI是全球最大的API市场,拥有超过100万开发人员和10000个API。
10+
icon: rapidapi.png
11+
tags:
12+
- news
13+
credentials_for_provider:
14+
x-rapidapi-host:
15+
type: text-input
16+
required: true
17+
label:
18+
en_US: x-rapidapi-host
19+
zh_Hans: x-rapidapi-host
20+
placeholder:
21+
en_US: Please input your x-rapidapi-host
22+
zh_Hans: 请输入你的 x-rapidapi-host
23+
help:
24+
en_US: Get your x-rapidapi-host from RapidAPI.
25+
zh_Hans: 从 RapidAPI 获取您的 x-rapidapi-host。
26+
url: https://rapidapi.com/
27+
x-rapidapi-key:
28+
type: secret-input
29+
required: true
30+
label:
31+
en_US: x-rapidapi-key
32+
zh_Hans: x-rapidapi-key
33+
placeholder:
34+
en_US: Please input your x-rapidapi-key
35+
zh_Hans: 请输入你的 x-rapidapi-key
36+
help:
37+
en_US: Get your x-rapidapi-key from RapidAPI.
38+
zh_Hans: 从 RapidAPI 获取您的 x-rapidapi-key。
39+
url: https://rapidapi.com/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from typing import Any, Union
2+
3+
import requests
4+
5+
from core.tools.entities.tool_entities import ToolInvokeMessage
6+
from core.tools.errors import ToolInvokeError, ToolProviderCredentialValidationError
7+
from core.tools.tool.builtin_tool import BuiltinTool
8+
9+
10+
class GooglenewsTool(BuiltinTool):
11+
def _invoke(
12+
self,
13+
user_id: str,
14+
tool_parameters: dict[str, Any],
15+
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
16+
"""
17+
invoke tools
18+
"""
19+
key = self.runtime.credentials.get("x-rapidapi-key", "")
20+
host = self.runtime.credentials.get("x-rapidapi-host", "")
21+
if not all([key, host]):
22+
raise ToolProviderCredentialValidationError("Please input correct x-rapidapi-key and x-rapidapi-host")
23+
headers = {"x-rapidapi-key": key, "x-rapidapi-host": host}
24+
lr = tool_parameters.get("language_region", "")
25+
url = f"https://{host}/latest?lr={lr}"
26+
response = requests.get(url, headers=headers)
27+
if response.status_code != 200:
28+
raise ToolInvokeError(f"Error {response.status_code}: {response.text}")
29+
return self.create_text_message(response.text)
30+
31+
def validate_credentials(self, parameters: dict[str, Any]) -> None:
32+
parameters["validate"] = True
33+
self._invoke(parameters)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
identity:
2+
name: google_news
3+
author: Steven Sun
4+
label:
5+
en_US: GoogleNews
6+
zh_Hans: 谷歌新闻
7+
description:
8+
human:
9+
en_US: google news is a news aggregator service developed by Google. It presents a continuous, customizable flow of articles organized from thousands of publishers and magazines.
10+
zh_Hans: 谷歌新闻是由谷歌开发的新闻聚合服务。它提供了一个持续的、可定制的文章流,这些文章是从成千上万的出版商和杂志中整理出来的。
11+
llm: A tool to get the latest news from Google News.
12+
parameters:
13+
- name: language_region
14+
type: string
15+
required: true
16+
label:
17+
en_US: Language and Region
18+
zh_Hans: 语言和地区
19+
human_description:
20+
en_US: The language and region determine the language and region of the search results, and its value is assigned according to the "National Language Code Comparison Table", such as en-US, which stands for English (United States); zh-CN, stands for Chinese (Simplified).
21+
zh_Hans: 语言和地区决定了搜索结果的语言和地区,其赋值按照《国家语言代码对照表》,形如en-US,代表英语(美国);zh-CN,代表中文(简体)。
22+
llm_description: The language and region determine the language and region of the search results, and its value is assigned according to the "National Language Code Comparison Table", such as en-US, which stands for English (United States); zh-CN, stands for Chinese (Simplified).
23+
default: en-US
24+
form: llm

0 commit comments

Comments
 (0)