feat(tools): add Baidu translation tool (#9943)

This commit is contained in:
Xiao Ley 2024-10-28 17:18:28 +08:00 committed by GitHub
parent ddb960ddfb
commit 7056009b6a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 748 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -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()

View File

@ -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))

View File

@ -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

View File

@ -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()

View File

@ -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: 合同领域

View File

@ -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)

View File

@ -0,0 +1,43 @@
identity:
name: language
author: Xiao Ley
label:
en_US: Baidu Language
zh_Hans: 百度语种识别
description:
human:
en_US: A tool for Baidu Language, support Chinese, English, Japanese, Korean, Thai, Vietnamese and Russian
zh_Hans: 使用百度进行语种识别,支持的语种:中文、英语、日语、韩语、泰语、越南语和俄语
llm: A tool for Baidu Language
parameters:
- name: q
type: string
required: true
label:
en_US: Text content
zh_Hans: 文本内容
human_description:
en_US: Text content to be recognized
zh_Hans: 需要识别语言的文本内容
llm_description: Text content to be recognized
form: llm
- name: description_language
type: select
required: true
label:
en_US: Description language
zh_Hans: 描述语言
human_description:
en_US: Describe the language used to identify the results
zh_Hans: 描述识别结果所用的语言
default: Chinese
form: form
options:
- value: Chinese
label:
en_US: Chinese
zh_Hans: 中文
- value: English
label:
en_US: English
zh_Hans: 英语

View File

@ -0,0 +1,67 @@
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 BaiduTranslateTool(BuiltinTool, BaiduTranslateToolBase):
def _invoke(
self,
user_id: str,
tool_parameters: dict[str, Any],
) -> Union[ToolInvokeMessage, list[ToolInvokeMessage]]:
"""
invoke tools
"""
BAIDU_TRANSLATE_URL = "https://fanyi-api.baidu.com/api/trans/vip/translate"
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")
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,
"from": from_,
"to": to,
"appid": appid,
"salt": salt,
"sign": sign,
}
try:
response = requests.post(BAIDU_TRANSLATE_URL, params=params, headers=headers)
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")

View File

@ -0,0 +1,275 @@
identity:
name: translate
author: Xiao Ley
label:
en_US: Translate
zh_Hans: 百度翻译
description:
human:
en_US: A tool for Baidu Translate
zh_Hans: 百度翻译
llm: A tool for Baidu 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: 英语
- value: cht
label:
en_US: Traditional Chinese
zh_Hans: 繁体中文
- value: yue
label:
en_US: Yue
zh_Hans: 粤语
- value: wyw
label:
en_US: Wyw
zh_Hans: 文言文
- value: jp
label:
en_US: Japanese
zh_Hans: 日语
- value: kor
label:
en_US: Korean
zh_Hans: 韩语
- value: fra
label:
en_US: French
zh_Hans: 法语
- value: spa
label:
en_US: Spanish
zh_Hans: 西班牙语
- value: th
label:
en_US: Thai
zh_Hans: 泰语
- value: ara
label:
en_US: Arabic
zh_Hans: 阿拉伯语
- value: ru
label:
en_US: Russian
zh_Hans: 俄语
- value: pt
label:
en_US: Portuguese
zh_Hans: 葡萄牙语
- value: de
label:
en_US: German
zh_Hans: 德语
- value: it
label:
en_US: Italian
zh_Hans: 意大利语
- value: el
label:
en_US: Greek
zh_Hans: 希腊语
- value: nl
label:
en_US: Dutch
zh_Hans: 荷兰语
- value: pl
label:
en_US: Polish
zh_Hans: 波兰语
- value: bul
label:
en_US: Bulgarian
zh_Hans: 保加利亚语
- value: est
label:
en_US: Estonian
zh_Hans: 爱沙尼亚语
- value: dan
label:
en_US: Danish
zh_Hans: 丹麦语
- value: fin
label:
en_US: Finnish
zh_Hans: 芬兰语
- value: cs
label:
en_US: Czech
zh_Hans: 捷克语
- value: rom
label:
en_US: Romanian
zh_Hans: 罗马尼亚语
- value: slo
label:
en_US: Slovak
zh_Hans: 斯洛文尼亚语
- value: swe
label:
en_US: Swedish
zh_Hans: 瑞典语
- value: hu
label:
en_US: Hungarian
zh_Hans: 匈牙利语
- value: vie
label:
en_US: Vietnamese
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: 英语
- value: cht
label:
en_US: Traditional Chinese
zh_Hans: 繁体中文
- value: yue
label:
en_US: Yue
zh_Hans: 粤语
- value: wyw
label:
en_US: Wyw
zh_Hans: 文言文
- value: jp
label:
en_US: Japanese
zh_Hans: 日语
- value: kor
label:
en_US: Korean
zh_Hans: 韩语
- value: fra
label:
en_US: French
zh_Hans: 法语
- value: spa
label:
en_US: Spanish
zh_Hans: 西班牙语
- value: th
label:
en_US: Thai
zh_Hans: 泰语
- value: ara
label:
en_US: Arabic
zh_Hans: 阿拉伯语
- value: ru
label:
en_US: Russian
zh_Hans: 俄语
- value: pt
label:
en_US: Portuguese
zh_Hans: 葡萄牙语
- value: de
label:
en_US: German
zh_Hans: 德语
- value: it
label:
en_US: Italian
zh_Hans: 意大利语
- value: el
label:
en_US: Greek
zh_Hans: 希腊语
- value: nl
label:
en_US: Dutch
zh_Hans: 荷兰语
- value: pl
label:
en_US: Polish
zh_Hans: 波兰语
- value: bul
label:
en_US: Bulgarian
zh_Hans: 保加利亚语
- value: est
label:
en_US: Estonian
zh_Hans: 爱沙尼亚语
- value: dan
label:
en_US: Danish
zh_Hans: 丹麦语
- value: fin
label:
en_US: Finnish
zh_Hans: 芬兰语
- value: cs
label:
en_US: Czech
zh_Hans: 捷克语
- value: rom
label:
en_US: Romanian
zh_Hans: 罗马尼亚语
- value: slo
label:
en_US: Slovak
zh_Hans: 斯洛文尼亚语
- value: swe
label:
en_US: Swedish
zh_Hans: 瑞典语
- value: hu
label:
en_US: Hungarian
zh_Hans: 匈牙利语
- value: vie
label:
en_US: Vietnamese
zh_Hans: 越南语