From cc0c826f36ba1c9a30b87ec4d9220dd2e830c8d1 Mon Sep 17 00:00:00 2001 From: svcvit Date: Tue, 16 Jul 2024 15:28:33 +0800 Subject: [PATCH] Add tool: Google Translate (#6156) --- .../builtin/google_translate/_assets/icon.svg | 18 ++ .../google_translate/google_translate.py | 17 ++ .../google_translate/google_translate.yaml | 12 + .../google_translate/tools/translate.py | 52 +++++ .../google_translate/tools/translate.yaml | 215 ++++++++++++++++++ 5 files changed, 314 insertions(+) create mode 100644 api/core/tools/provider/builtin/google_translate/_assets/icon.svg create mode 100644 api/core/tools/provider/builtin/google_translate/google_translate.py create mode 100644 api/core/tools/provider/builtin/google_translate/google_translate.yaml create mode 100644 api/core/tools/provider/builtin/google_translate/tools/translate.py create mode 100644 api/core/tools/provider/builtin/google_translate/tools/translate.yaml diff --git a/api/core/tools/provider/builtin/google_translate/_assets/icon.svg b/api/core/tools/provider/builtin/google_translate/_assets/icon.svg new file mode 100644 index 0000000000..de69a9c5e5 --- /dev/null +++ b/api/core/tools/provider/builtin/google_translate/_assets/icon.svg @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/api/core/tools/provider/builtin/google_translate/google_translate.py b/api/core/tools/provider/builtin/google_translate/google_translate.py new file mode 100644 index 0000000000..f6e1d65834 --- /dev/null +++ b/api/core/tools/provider/builtin/google_translate/google_translate.py @@ -0,0 +1,17 @@ +from typing import Any + +from core.tools.errors import ToolProviderCredentialValidationError +from core.tools.provider.builtin.google_translate.tools.translate import GoogleTranslate +from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController + + +class JsonExtractProvider(BuiltinToolProviderController): + def _validate_credentials(self, credentials: dict[str, Any]) -> None: + try: + GoogleTranslate().invoke(user_id='', + tool_parameters={ + "content": "这是一段测试文本", + "dest": "en" + }) + except Exception as e: + raise ToolProviderCredentialValidationError(str(e)) diff --git a/api/core/tools/provider/builtin/google_translate/google_translate.yaml b/api/core/tools/provider/builtin/google_translate/google_translate.yaml new file mode 100644 index 0000000000..8bc821a3d5 --- /dev/null +++ b/api/core/tools/provider/builtin/google_translate/google_translate.yaml @@ -0,0 +1,12 @@ +identity: + author: Ron Liu + name: google_translate + label: + en_US: Google Translate + zh_Hans: 谷歌翻译 + description: + en_US: Translate text using Google + zh_Hans: 使用 Google 进行翻译 + icon: icon.svg + tags: + - utilities diff --git a/api/core/tools/provider/builtin/google_translate/tools/translate.py b/api/core/tools/provider/builtin/google_translate/tools/translate.py new file mode 100644 index 0000000000..4314182b06 --- /dev/null +++ b/api/core/tools/provider/builtin/google_translate/tools/translate.py @@ -0,0 +1,52 @@ +from typing import Any, Union + +import requests + +from core.tools.entities.tool_entities import ToolInvokeMessage +from core.tools.tool.builtin_tool import BuiltinTool + + +class GoogleTranslate(BuiltinTool): + def _invoke(self, + user_id: str, + tool_parameters: dict[str, Any], + ) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: + """ + invoke tools + """ + content = tool_parameters.get('content', '') + if not content: + return self.create_text_message('Invalid parameter content') + + dest = tool_parameters.get('dest', '') + if not dest: + return self.create_text_message('Invalid parameter destination language') + + try: + result = self._translate(content, dest) + return self.create_text_message(str(result)) + except Exception: + return self.create_text_message('Translation service error, please check the network') + + def _translate(self, content: str, dest: str) -> str: + try: + url = "https://translate.googleapis.com/translate_a/single" + params = { + "client": "gtx", + "sl": "auto", + "tl": dest, + "dt": "t", + "q": content + } + + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" + } + + response_json = requests.get( + url, params=params, headers=headers).json() + result = response_json[0] + translated_text = ''.join([item[0] for item in result if item[0]]) + return str(translated_text) + except Exception as e: + return str(e) diff --git a/api/core/tools/provider/builtin/google_translate/tools/translate.yaml b/api/core/tools/provider/builtin/google_translate/tools/translate.yaml new file mode 100644 index 0000000000..a4189cd743 --- /dev/null +++ b/api/core/tools/provider/builtin/google_translate/tools/translate.yaml @@ -0,0 +1,215 @@ +identity: + name: translate + author: Ron Liu + label: + en_US: Translate + zh_Hans: 翻译 +description: + human: + en_US: A tool for Google Translate + zh_Hans: Google 翻译 + llm: A tool for Google Translate +parameters: + - name: content + type: string + required: true + label: + en_US: Text content + zh_Hans: 文本内容 + human_description: + en_US: Text content + zh_Hans: 需要翻译的文本内容 + llm_description: Text content + form: llm + - name: dest + type: select + required: true + label: + en_US: destination language + zh_Hans: 目标语言 + human_description: + en_US: The destination language you want to translate. + zh_Hans: 你想翻译的目标语言 + default: en + form: form + options: + - value: ar + label: + en_US: Arabic + zh_Hans: 阿拉伯语 + - value: bg + label: + en_US: Bulgarian + zh_Hans: 保加利亚语 + - value: ca + label: + en_US: Catalan + zh_Hans: 加泰罗尼亚语 + - value: zh-cn + label: + en_US: Chinese (Simplified) + zh_Hans: 中文(简体) + - value: zh-tw + label: + en_US: Chinese (Traditional) + zh_Hans: 中文(繁体) + - value: cs + label: + en_US: Czech + zh_Hans: 捷克语 + - value: da + label: + en_US: Danish + zh_Hans: 丹麦语 + - value: nl + label: + en_US: Dutch + zh_Hans: 荷兰语 + - value: en + label: + en_US: English + zh_Hans: 英语 + - value: et + label: + en_US: Estonian + zh_Hans: 爱沙尼亚语 + - value: fi + label: + en_US: Finnish + zh_Hans: 芬兰语 + - value: fr + label: + en_US: French + zh_Hans: 法语 + - value: de + label: + en_US: German + zh_Hans: 德语 + - value: el + label: + en_US: Greek + zh_Hans: 希腊语 + - value: iw + label: + en_US: Hebrew + zh_Hans: 希伯来语 + - value: hi + label: + en_US: Hindi + zh_Hans: 印地语 + - value: hu + label: + en_US: Hungarian + zh_Hans: 匈牙利语 + - value: id + label: + en_US: Indonesian + zh_Hans: 印尼语 + - value: it + label: + en_US: Italian + zh_Hans: 意大利语 + - value: ja + label: + en_US: Japanese + zh_Hans: 日语 + - value: kn + label: + en_US: Kannada + zh_Hans: 卡纳达语 + - value: ko + label: + en_US: Korean + zh_Hans: 韩语 + - value: lv + label: + en_US: Latvian + zh_Hans: 拉脱维亚语 + - value: lt + label: + en_US: Lithuanian + zh_Hans: 立陶宛语 + - value: my + label: + en_US: Malay + zh_Hans: 马来语 + - value: ml + label: + en_US: Malayalam + zh_Hans: 马拉雅拉姆语 + - value: mr + label: + en_US: Marathi + zh_Hans: 马拉地语 + - value: "no" + label: + en_US: Norwegian + zh_Hans: 挪威语 + - value: pl + label: + en_US: Polish + zh_Hans: 波兰语 + - value: pt-br + label: + en_US: Portuguese (Brazil) + zh_Hans: 葡萄牙语(巴西) + - value: pt-pt + label: + en_US: Portuguese (Portugal) + zh_Hans: 葡萄牙语(葡萄牙) + - value: pa + label: + en_US: Punjabi + zh_Hans: 旁遮普语 + - value: ro + label: + en_US: Romanian + zh_Hans: 罗马尼亚语 + - value: ru + label: + en_US: Russian + zh_Hans: 俄语 + - value: sr + label: + en_US: Serbian + zh_Hans: 塞尔维亚语 + - value: sk + label: + en_US: Slovak + zh_Hans: 斯洛伐克语 + - value: sl + label: + en_US: Slovenian + zh_Hans: 斯洛文尼亚语 + - value: es + label: + en_US: Spanish + zh_Hans: 西班牙语 + - value: sv + label: + en_US: Swedish + zh_Hans: 瑞典语 + - value: ta + label: + en_US: Tamil + zh_Hans: 泰米尔语 + - value: te + label: + en_US: Telugu + zh_Hans: 泰卢固语 + - value: th + label: + en_US: Thai + zh_Hans: 泰语 + - value: tr + label: + en_US: Turkish + zh_Hans: 土耳其语 + - value: uk + label: + en_US: Ukrainian + zh_Hans: 乌克兰语 + - value: vi + label: + en_US: Vietnamese + zh_Hans: 越南语