forked from langgenius/dify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools): add Baidu translation tool (langgenius#9943)
- Loading branch information
Showing
10 changed files
with
748 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions
11
api/core/tools/provider/builtin/baidu_translate/_baidu_translate_tool_base.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from hashlib import md5 | ||
|
||
|
||
class BaiduTranslateToolBase: | ||
def _get_sign(self, appid, secret, salt, query): | ||
""" | ||
get baidu translate sign | ||
""" | ||
# concatenate the string in the order of appid+q+salt+secret | ||
str = appid + query + salt + secret | ||
return md5(str.encode("utf-8")).hexdigest() |
17 changes: 17 additions & 0 deletions
17
api/core/tools/provider/builtin/baidu_translate/baidu_translate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from typing import Any | ||
|
||
from core.tools.errors import ToolProviderCredentialValidationError | ||
from core.tools.provider.builtin.baidu_translate.tools.translate import BaiduTranslateTool | ||
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController | ||
|
||
|
||
class BaiduTranslateProvider(BuiltinToolProviderController): | ||
def _validate_credentials(self, credentials: dict[str, Any]) -> None: | ||
try: | ||
BaiduTranslateTool().fork_tool_runtime( | ||
runtime={ | ||
"credentials": credentials, | ||
} | ||
).invoke(user_id="", tool_parameters={"q": "这是一段测试文本", "from": "auto", "to": "en"}) | ||
except Exception as e: | ||
raise ToolProviderCredentialValidationError(str(e)) |
39 changes: 39 additions & 0 deletions
39
api/core/tools/provider/builtin/baidu_translate/baidu_translate.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
identity: | ||
author: Xiao Ley | ||
name: baidu_translate | ||
label: | ||
en_US: Baidu Translate | ||
zh_Hans: 百度翻译 | ||
description: | ||
en_US: Translate text using Baidu | ||
zh_Hans: 使用百度进行翻译 | ||
icon: icon.png | ||
tags: | ||
- utilities | ||
credentials_for_provider: | ||
appid: | ||
type: secret-input | ||
required: true | ||
label: | ||
en_US: Baidu translate appid | ||
zh_Hans: Baidu translate appid | ||
placeholder: | ||
en_US: Please input your Baidu translate appid | ||
zh_Hans: 请输入你的百度翻译 appid | ||
help: | ||
en_US: Get your Baidu translate appid from Baidu translate | ||
zh_Hans: 从百度翻译开放平台获取你的 appid | ||
url: https://api.fanyi.baidu.com | ||
secret: | ||
type: secret-input | ||
required: true | ||
label: | ||
en_US: Baidu translate secret | ||
zh_Hans: Baidu translate secret | ||
placeholder: | ||
en_US: Please input your Baidu translate secret | ||
zh_Hans: 请输入你的百度翻译 secret | ||
help: | ||
en_US: Get your Baidu translate secret from Baidu translate | ||
zh_Hans: 从百度翻译开放平台获取你的 secret | ||
url: https://api.fanyi.baidu.com |
78 changes: 78 additions & 0 deletions
78
api/core/tools/provider/builtin/baidu_translate/tools/fieldtranslate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import random | ||
from hashlib import md5 | ||
from typing import Any, Union | ||
|
||
import requests | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.provider.builtin.baidu_translate._baidu_translate_tool_base import BaiduTranslateToolBase | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
|
||
|
||
class BaiduFieldTranslateTool(BuiltinTool, BaiduTranslateToolBase): | ||
def _invoke( | ||
self, | ||
user_id: str, | ||
tool_parameters: dict[str, Any], | ||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: | ||
""" | ||
invoke tools | ||
""" | ||
BAIDU_FIELD_TRANSLATE_URL = "https://fanyi-api.baidu.com/api/trans/vip/fieldtranslate" | ||
|
||
appid = self.runtime.credentials.get("appid", "") | ||
if not appid: | ||
raise ValueError("invalid baidu translate appid") | ||
|
||
secret = self.runtime.credentials.get("secret", "") | ||
if not secret: | ||
raise ValueError("invalid baidu translate secret") | ||
|
||
q = tool_parameters.get("q", "") | ||
if not q: | ||
raise ValueError("Please input text to translate") | ||
|
||
from_ = tool_parameters.get("from", "") | ||
if not from_: | ||
raise ValueError("Please select source language") | ||
|
||
to = tool_parameters.get("to", "") | ||
if not to: | ||
raise ValueError("Please select destination language") | ||
|
||
domain = tool_parameters.get("domain", "") | ||
if not domain: | ||
raise ValueError("Please select domain") | ||
|
||
salt = str(random.randint(32768, 16777215)) | ||
sign = self._get_sign(appid, secret, salt, q, domain) | ||
|
||
headers = {"Content-Type": "application/x-www-form-urlencoded"} | ||
params = { | ||
"q": q, | ||
"from": from_, | ||
"to": to, | ||
"appid": appid, | ||
"salt": salt, | ||
"domain": domain, | ||
"sign": sign, | ||
"needIntervene": 1, | ||
} | ||
try: | ||
response = requests.post(BAIDU_FIELD_TRANSLATE_URL, headers=headers, data=params) | ||
result = response.json() | ||
|
||
if "trans_result" in result: | ||
result_text = result["trans_result"][0]["dst"] | ||
else: | ||
result_text = f'{result["error_code"]}: {result["error_msg"]}' | ||
|
||
return self.create_text_message(str(result_text)) | ||
except requests.RequestException as e: | ||
raise ValueError(f"Translation service error: {e}") | ||
except Exception: | ||
raise ValueError("Translation service error, please check the network") | ||
|
||
def _get_sign(self, appid, secret, salt, query, domain): | ||
str = appid + query + salt + domain + secret | ||
return md5(str.encode("utf-8")).hexdigest() |
123 changes: 123 additions & 0 deletions
123
api/core/tools/provider/builtin/baidu_translate/tools/fieldtranslate.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
identity: | ||
name: field_translate | ||
author: Xiao Ley | ||
label: | ||
en_US: Field translate | ||
zh_Hans: 百度领域翻译 | ||
description: | ||
human: | ||
en_US: A tool for Baidu Field translate (Currently, the fields of "novel" and "wiki" only support Chinese to English translation. If the language direction is set to English to Chinese, the default output will be a universal translation result). | ||
zh_Hans: 百度领域翻译,提供多种领域的文本翻译(目前“网络文学领域”和“人文社科领域”仅支持中到英,如设置语言方向为英到中,则默认输出通用翻译结果) | ||
llm: A tool for Baidu Field translate | ||
parameters: | ||
- name: q | ||
type: string | ||
required: true | ||
label: | ||
en_US: Text content | ||
zh_Hans: 文本内容 | ||
human_description: | ||
en_US: Text content to be translated | ||
zh_Hans: 需要翻译的文本内容 | ||
llm_description: Text content to be translated | ||
form: llm | ||
- name: from | ||
type: select | ||
required: true | ||
label: | ||
en_US: source language | ||
zh_Hans: 源语言 | ||
human_description: | ||
en_US: The source language of the input text | ||
zh_Hans: 输入的文本的源语言 | ||
default: auto | ||
form: form | ||
options: | ||
- value: auto | ||
label: | ||
en_US: auto | ||
zh_Hans: 自动检测 | ||
- value: zh | ||
label: | ||
en_US: Chinese | ||
zh_Hans: 中文 | ||
- value: en | ||
label: | ||
en_US: English | ||
zh_Hans: 英语 | ||
- name: to | ||
type: select | ||
required: true | ||
label: | ||
en_US: destination language | ||
zh_Hans: 目标语言 | ||
human_description: | ||
en_US: The destination language of the input text | ||
zh_Hans: 输入文本的目标语言 | ||
default: en | ||
form: form | ||
options: | ||
- value: zh | ||
label: | ||
en_US: Chinese | ||
zh_Hans: 中文 | ||
- value: en | ||
label: | ||
en_US: English | ||
zh_Hans: 英语 | ||
- name: domain | ||
type: select | ||
required: true | ||
label: | ||
en_US: domain | ||
zh_Hans: 领域 | ||
human_description: | ||
en_US: The domain of the input text | ||
zh_Hans: 输入文本的领域 | ||
default: novel | ||
form: form | ||
options: | ||
- value: it | ||
label: | ||
en_US: it | ||
zh_Hans: 信息技术领域 | ||
- value: finance | ||
label: | ||
en_US: finance | ||
zh_Hans: 金融财经领域 | ||
- value: machinery | ||
label: | ||
en_US: machinery | ||
zh_Hans: 机械制造领域 | ||
- value: senimed | ||
label: | ||
en_US: senimed | ||
zh_Hans: 生物医药领域 | ||
- value: novel | ||
label: | ||
en_US: novel (only support Chinese to English translation) | ||
zh_Hans: 网络文学领域(仅支持中到英) | ||
- value: academic | ||
label: | ||
en_US: academic | ||
zh_Hans: 学术论文领域 | ||
- value: aerospace | ||
label: | ||
en_US: aerospace | ||
zh_Hans: 航空航天领域 | ||
- value: wiki | ||
label: | ||
en_US: wiki (only support Chinese to English translation) | ||
zh_Hans: 人文社科领域(仅支持中到英) | ||
- value: news | ||
label: | ||
en_US: news | ||
zh_Hans: 新闻咨询领域 | ||
- value: law | ||
label: | ||
en_US: law | ||
zh_Hans: 法律法规领域 | ||
- value: contract | ||
label: | ||
en_US: contract | ||
zh_Hans: 合同领域 |
95 changes: 95 additions & 0 deletions
95
api/core/tools/provider/builtin/baidu_translate/tools/language.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import random | ||
from typing import Any, Union | ||
|
||
import requests | ||
|
||
from core.tools.entities.tool_entities import ToolInvokeMessage | ||
from core.tools.provider.builtin.baidu_translate._baidu_translate_tool_base import BaiduTranslateToolBase | ||
from core.tools.tool.builtin_tool import BuiltinTool | ||
|
||
|
||
class BaiduLanguageTool(BuiltinTool, BaiduTranslateToolBase): | ||
def _invoke( | ||
self, | ||
user_id: str, | ||
tool_parameters: dict[str, Any], | ||
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]: | ||
""" | ||
invoke tools | ||
""" | ||
BAIDU_LANGUAGE_URL = "https://fanyi-api.baidu.com/api/trans/vip/language" | ||
|
||
appid = self.runtime.credentials.get("appid", "") | ||
if not appid: | ||
raise ValueError("invalid baidu translate appid") | ||
|
||
secret = self.runtime.credentials.get("secret", "") | ||
if not secret: | ||
raise ValueError("invalid baidu translate secret") | ||
|
||
q = tool_parameters.get("q", "") | ||
if not q: | ||
raise ValueError("Please input text to translate") | ||
|
||
description_language = tool_parameters.get("description_language", "English") | ||
|
||
salt = str(random.randint(32768, 16777215)) | ||
sign = self._get_sign(appid, secret, salt, q) | ||
|
||
headers = {"Content-Type": "application/x-www-form-urlencoded"} | ||
params = { | ||
"q": q, | ||
"appid": appid, | ||
"salt": salt, | ||
"sign": sign, | ||
} | ||
|
||
try: | ||
response = requests.post(BAIDU_LANGUAGE_URL, params=params, headers=headers) | ||
result = response.json() | ||
if "error_code" not in result: | ||
raise ValueError("Translation service error, please check the network") | ||
|
||
result_text = "" | ||
if result["error_code"] != 0: | ||
result_text = f'{result["error_code"]}: {result["error_msg"]}' | ||
else: | ||
result_text = result["data"]["src"] | ||
result_text = self.mapping_result(description_language, result_text) | ||
|
||
return self.create_text_message(result_text) | ||
except requests.RequestException as e: | ||
raise ValueError(f"Translation service error: {e}") | ||
except Exception: | ||
raise ValueError("Translation service error, please check the network") | ||
|
||
def mapping_result(self, description_language: str, result: str) -> str: | ||
""" | ||
mapping result | ||
""" | ||
mapping = { | ||
"English": { | ||
"zh": "Chinese", | ||
"en": "English", | ||
"jp": "Japanese", | ||
"kor": "Korean", | ||
"th": "Thai", | ||
"vie": "Vietnamese", | ||
"ru": "Russian", | ||
}, | ||
"Chinese": { | ||
"zh": "中文", | ||
"en": "英文", | ||
"jp": "日文", | ||
"kor": "韩文", | ||
"th": "泰语", | ||
"vie": "越南语", | ||
"ru": "俄语", | ||
}, | ||
} | ||
|
||
language_mapping = mapping.get(description_language) | ||
if not language_mapping: | ||
return result | ||
|
||
return language_mapping.get(result, result) |
Oops, something went wrong.