Browse Source

Add googlenews tools from rapidapi (#10877)

Co-authored-by: steven <sunzwj@digitalchina.com>
Steven sun 5 months ago
parent
commit
2ae6460f46

BIN
api/core/tools/provider/builtin/rapidapi/_assets/rapidapi.png


+ 22 - 0
api/core/tools/provider/builtin/rapidapi/rapidapi.py

@@ -0,0 +1,22 @@
+from typing import Any
+
+from core.tools.errors import ToolProviderCredentialValidationError
+from core.tools.provider.builtin.rapidapi.tools.google_news import GooglenewsTool
+from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
+
+
+class RapidapiProvider(BuiltinToolProviderController):
+    def _validate_credentials(self, credentials: dict[str, Any]) -> None:
+        try:
+            GooglenewsTool().fork_tool_runtime(
+                meta={
+                    "credentials": credentials,
+                }
+            ).invoke(
+                user_id="",
+                tool_parameters={
+                    "language_region": "en-US",
+                },
+            )
+        except Exception as e:
+            raise ToolProviderCredentialValidationError(str(e))

+ 39 - 0
api/core/tools/provider/builtin/rapidapi/rapidapi.yaml

@@ -0,0 +1,39 @@
+identity:
+  name: rapidapi
+  author: Steven Sun
+  label:
+    en_US: RapidAPI
+    zh_Hans: RapidAPI
+  description:
+    en_US: RapidAPI is the world's largest API marketplace with over 1,000,000 developers and 10,000 APIs.
+    zh_Hans: RapidAPI是全球最大的API市场,拥有超过100万开发人员和10000个API。
+  icon: rapidapi.png
+  tags:
+    - news
+credentials_for_provider:
+  x-rapidapi-host:
+    type: text-input
+    required: true
+    label:
+      en_US: x-rapidapi-host
+      zh_Hans: x-rapidapi-host
+    placeholder:
+      en_US: Please input your x-rapidapi-host
+      zh_Hans: 请输入你的 x-rapidapi-host
+    help:
+      en_US: Get your x-rapidapi-host from RapidAPI.
+      zh_Hans: 从 RapidAPI 获取您的 x-rapidapi-host。
+    url: https://rapidapi.com/
+  x-rapidapi-key:
+    type: secret-input
+    required: true
+    label:
+      en_US: x-rapidapi-key
+      zh_Hans: x-rapidapi-key
+    placeholder:
+      en_US: Please input your x-rapidapi-key
+      zh_Hans: 请输入你的 x-rapidapi-key
+    help:
+      en_US: Get your x-rapidapi-key from RapidAPI.
+      zh_Hans: 从 RapidAPI 获取您的 x-rapidapi-key。
+    url: https://rapidapi.com/

+ 33 - 0
api/core/tools/provider/builtin/rapidapi/tools/google_news.py

@@ -0,0 +1,33 @@
+from typing import Any, Union
+
+import requests
+
+from core.tools.entities.tool_entities import ToolInvokeMessage
+from core.tools.errors import ToolInvokeError, ToolProviderCredentialValidationError
+from core.tools.tool.builtin_tool import BuiltinTool
+
+
+class GooglenewsTool(BuiltinTool):
+    def _invoke(
+        self,
+        user_id: str,
+        tool_parameters: dict[str, Any],
+    ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
+        """
+        invoke tools
+        """
+        key = self.runtime.credentials.get("x-rapidapi-key", "")
+        host = self.runtime.credentials.get("x-rapidapi-host", "")
+        if not all([key, host]):
+            raise ToolProviderCredentialValidationError("Please input correct x-rapidapi-key and x-rapidapi-host")
+        headers = {"x-rapidapi-key": key, "x-rapidapi-host": host}
+        lr = tool_parameters.get("language_region", "")
+        url = f"https://{host}/latest?lr={lr}"
+        response = requests.get(url, headers=headers)
+        if response.status_code != 200:
+            raise ToolInvokeError(f"Error {response.status_code}: {response.text}")
+        return self.create_text_message(response.text)
+
+    def validate_credentials(self, parameters: dict[str, Any]) -> None:
+        parameters["validate"] = True
+        self._invoke(parameters)

+ 24 - 0
api/core/tools/provider/builtin/rapidapi/tools/google_news.yaml

@@ -0,0 +1,24 @@
+identity:
+  name: google_news
+  author: Steven Sun
+  label:
+    en_US: GoogleNews
+    zh_Hans: 谷歌新闻
+description:
+  human:
+    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.
+    zh_Hans: 谷歌新闻是由谷歌开发的新闻聚合服务。它提供了一个持续的、可定制的文章流,这些文章是从成千上万的出版商和杂志中整理出来的。
+  llm: A tool to get the latest news from Google News.
+parameters:
+  - name: language_region
+    type: string
+    required: true
+    label:
+      en_US: Language and Region
+      zh_Hans: 语言和地区
+    human_description:
+      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).
+      zh_Hans: 语言和地区决定了搜索结果的语言和地区,其赋值按照《国家语言代码对照表》,形如en-US,代表英语(美国);zh-CN,代表中文(简体)。
+    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).
+    default: en-US
+    form: llm