mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-11 18:49:02 +08:00
Add tool: Google Translate (#6156)
This commit is contained in:
parent
0099ef6896
commit
cc0c826f36
@ -0,0 +1,18 @@
|
|||||||
|
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" x="0" y="0" viewBox="0 0 998.1 998.3" xml:space="preserve">
|
||||||
|
<path fill="#DBDBDB" d="M931.7 998.3c36.5 0 66.4-29.4 66.4-65.4V265.8c0-36-29.9-65.4-66.4-65.4H283.6l260.1 797.9h388z"/>
|
||||||
|
<path fill="#DCDCDC" d="M931.7 230.4c9.7 0 18.9 3.8 25.8 10.6 6.8 6.7 10.6 15.5 10.6 24.8v667.1c0 9.3-3.7 18.1-10.6 24.8-6.9 6.8-16.1 10.6-25.8 10.6H565.5L324.9 230.4h606.8m0-30H283.6l260.1 797.9h388c36.5 0 66.4-29.4 66.4-65.4V265.8c0-36-29.9-65.4-66.4-65.4z"/>
|
||||||
|
<polygon fill="#4352B8" points="482.3,809.8 543.7,998.3 714.4,809.8"/>
|
||||||
|
<path fill="#607988" d="M936.1 476.1V437H747.6v-63.2h-61.2V437H566.1v39.1h239.4c-12.8 45.1-41.1 87.7-68.7 120.8-48.9-57.9-49.1-76.7-49.1-76.7h-50.8s2.1 28.2 70.7 108.6c-22.3 22.8-39.2 36.3-39.2 36.3l15.6 48.8s23.6-20.3 53.1-51.6c29.6 32.1 67.8 70.7 117.2 116.7l32.1-32.1c-52.9-48-91.7-86.1-120.2-116.7 38.2-45.2 77-102.1 85.2-154.2H936v.1z"/>
|
||||||
|
<path fill="#4285F4" d="M66.4 0C29.9 0 0 29.9 0 66.5v677c0 36.5 29.9 66.4 66.4 66.4h648.1L454.4 0h-388z"/>
|
||||||
|
<linearGradient id="a" gradientUnits="userSpaceOnUse" x1="534.3" y1="433.2" x2="998.1" y2="433.2">
|
||||||
|
<stop offset="0" stop-color="#fff" stop-opacity=".2"/>
|
||||||
|
<stop offset="1" stop-color="#fff" stop-opacity=".02"/>
|
||||||
|
</linearGradient>
|
||||||
|
<path fill="url(#a)" d="M534.3 200.4h397.4c36.5 0 66.4 29.4 66.4 65.4V666L534.3 200.4z"/>
|
||||||
|
<path fill="#EEEEEE" d="M371.4 430.6c-2.5 30.3-28.4 75.2-91.1 75.2-54.3 0-98.3-44.9-98.3-100.2s44-100.2 98.3-100.2c30.9 0 51.5 13.4 63.3 24.3l41.2-39.6c-27.1-25-62.4-40.6-104.5-40.6-86.1 0-156 69.9-156 156s69.9 156 156 156c90.2 0 149.8-63.3 149.8-152.6 0-12.8-1.6-22.2-3.7-31.8h-146v53.4l91 .1z"/>
|
||||||
|
<radialGradient id="b" cx="65.208" cy="19.366" r="1398.271" gradientUnits="userSpaceOnUse">
|
||||||
|
<stop offset="0" stop-color="#fff" stop-opacity=".1"/>
|
||||||
|
<stop offset="1" stop-color="#fff" stop-opacity="0"/>
|
||||||
|
</radialGradient>
|
||||||
|
<path fill="url(#b)" d="M931.7 200.4H518.8L454.4 0h-388C29.9 0 0 29.9 0 66.5v677c0 36.5 29.9 66.4 66.4 66.4h415.9l61.4 188.4h388c36.5 0 66.4-29.4 66.4-65.4V265.8c0-36-29.9-65.4-66.4-65.4z"/>
|
||||||
|
</svg>
|
After Width: | Height: | Size: 2.0 KiB |
@ -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))
|
@ -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
|
@ -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)
|
@ -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: 越南语
|
Loading…
x
Reference in New Issue
Block a user