H
Kevin Hu
commited on
Commit
·
570986a
1
Parent(s):
6a3255a
Add GitHub, deepl, baidu-fanyi (#1857)
Browse files### What problem does this PR solve?
#1739
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
---------
Co-authored-by: Kevin Hu <[email protected]>
- agent/component/__init__.py +3 -1
- agent/component/baidufanyi.py +114 -0
- agent/component/deepl.py +77 -0
- agent/component/github.py +61 -0
- requirements.txt +1 -0
- requirements_arm.txt +1 -0
- requirements_dev.txt +1 -0
agent/component/__init__.py
CHANGED
|
@@ -17,7 +17,9 @@ from .arxiv import ArXiv, ArXivParam
|
|
| 17 |
from .google import Google, GoogleParam
|
| 18 |
from .bing import Bing, BingParam
|
| 19 |
from .googlescholar import GoogleScholar, GoogleScholarParam
|
| 20 |
-
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def component_class(class_name):
|
| 23 |
m = importlib.import_module("agent.component")
|
|
|
|
| 17 |
from .google import Google, GoogleParam
|
| 18 |
from .bing import Bing, BingParam
|
| 19 |
from .googlescholar import GoogleScholar, GoogleScholarParam
|
| 20 |
+
from .deepl import DeepL, DeepLParam
|
| 21 |
+
from .github import GitHub, GitHubParam
|
| 22 |
+
from .baidufanyi import BaiduFanyi, BaiduFanyiParam
|
| 23 |
|
| 24 |
def component_class(class_name):
|
| 25 |
m = importlib.import_module("agent.component")
|
agent/component/baidufanyi.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
#
|
| 16 |
+
import random
|
| 17 |
+
from abc import ABC
|
| 18 |
+
import requests
|
| 19 |
+
import re
|
| 20 |
+
from agent.component.base import ComponentBase, ComponentParamBase
|
| 21 |
+
from hashlib import md5
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class BaiduFanyiParam(ComponentParamBase):
|
| 25 |
+
"""
|
| 26 |
+
Define the BaiduFanyi component parameters.
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
def __init__(self):
|
| 30 |
+
super().__init__()
|
| 31 |
+
self.prompt = ""
|
| 32 |
+
self.appid = "xxx"
|
| 33 |
+
self.secret_key = "xxx"
|
| 34 |
+
self.trans_type = 'translate'
|
| 35 |
+
self.parameters = []
|
| 36 |
+
self.source_lang = 'auto'
|
| 37 |
+
self.target_lang = 'auto'
|
| 38 |
+
self.domain = 'finance'
|
| 39 |
+
|
| 40 |
+
def check(self):
|
| 41 |
+
self.check_positive_integer(self.top_n, "Top N")
|
| 42 |
+
self.check_empty(self.appid, "BaiduFanyi APPID")
|
| 43 |
+
self.check_empty(self.secret_key, "BaiduFanyi Secret Key")
|
| 44 |
+
self.check_valid_value(self.trans_type, "Translate type", ['translate', 'fieldtranslate'])
|
| 45 |
+
self.check_valid_value(self.trans_type, "Translate domain",
|
| 46 |
+
['it', 'finance', 'machinery', 'senimed', 'novel', 'academic', 'aerospace', 'wiki',
|
| 47 |
+
'news', 'law', 'contract'])
|
| 48 |
+
self.check_valid_value(self.source_lang, "Source language",
|
| 49 |
+
['auto', 'zh', 'en', 'yue', 'wyw', 'jp', 'kor', 'fra', 'spa', 'th', 'ara', 'ru', 'pt',
|
| 50 |
+
'de', 'it', 'el', 'nl', 'pl', 'bul', 'est', 'dan', 'fin', 'cs', 'rom', 'slo', 'swe',
|
| 51 |
+
'hu', 'cht', 'vie'])
|
| 52 |
+
self.check_valid_value(self.target_lang, "Target language",
|
| 53 |
+
['auto', 'zh', 'en', 'yue', 'wyw', 'jp', 'kor', 'fra', 'spa', 'th', 'ara', 'ru', 'pt',
|
| 54 |
+
'de', 'it', 'el', 'nl', 'pl', 'bul', 'est', 'dan', 'fin', 'cs', 'rom', 'slo', 'swe',
|
| 55 |
+
'hu', 'cht', 'vie'])
|
| 56 |
+
self.check_valid_value(self.domain, "Translate field",
|
| 57 |
+
['it', 'finance', 'machinery', 'senimed', 'novel', 'academic', 'aerospace', 'wiki',
|
| 58 |
+
'news', 'law', 'contract'])
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
class BaiduFanyi(ComponentBase, ABC):
|
| 62 |
+
component_name = "BaiduFanyi"
|
| 63 |
+
|
| 64 |
+
def _run(self, history, **kwargs):
|
| 65 |
+
prompt = self._param.prompt
|
| 66 |
+
|
| 67 |
+
ans = self.get_input()
|
| 68 |
+
ans = " - ".join(ans["content"]) if "content" in ans else ""
|
| 69 |
+
if not ans:
|
| 70 |
+
return BaiduFanyi.be_output("")
|
| 71 |
+
|
| 72 |
+
for para in self._param.parameters:
|
| 73 |
+
cpn = self._canvas.get_component(para["component_id"])["obj"]
|
| 74 |
+
_, out = cpn.output(allow_partial=False)
|
| 75 |
+
if "content" not in out.columns:
|
| 76 |
+
kwargs[para["key"]] = "Nothing"
|
| 77 |
+
else:
|
| 78 |
+
kwargs[para["key"]] = " - " + "\n - ".join(out["content"])
|
| 79 |
+
|
| 80 |
+
kwargs["input"] = ans
|
| 81 |
+
for n, v in kwargs.items():
|
| 82 |
+
prompt = re.sub(r"\{%s\}" % n, str(v), prompt)
|
| 83 |
+
|
| 84 |
+
try:
|
| 85 |
+
source_lang = self._param.source_lang
|
| 86 |
+
target_lang = self._param.target_lang
|
| 87 |
+
appid = self._param.appid
|
| 88 |
+
salt = random.randint(32768, 65536)
|
| 89 |
+
secret_key = self._param.secret_key
|
| 90 |
+
|
| 91 |
+
if self._param.trans_type == 'translate':
|
| 92 |
+
sign = md5((appid + prompt + salt + secret_key).encode('utf-8')).hexdigest()
|
| 93 |
+
url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?' + 'q=' + prompt + '&from=' + source_lang + '&to=' + target_lang + '&appid=' + appid + '&salt=' + salt + '&sign=' + sign
|
| 94 |
+
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
| 95 |
+
response = requests.post(url=url, headers=headers).json()
|
| 96 |
+
|
| 97 |
+
if response.get('error_code'):
|
| 98 |
+
BaiduFanyi.be_output("**Error**:" + response['error_msg'])
|
| 99 |
+
|
| 100 |
+
return BaiduFanyi.be_output(response['trans_result'][0]['dst'])
|
| 101 |
+
elif self._param.trans_type == 'fieldtranslate':
|
| 102 |
+
domain = self._param.domain
|
| 103 |
+
sign = md5((appid + prompt + salt + domain + secret_key).encode('utf-8')).hexdigest()
|
| 104 |
+
url = 'http://api.fanyi.baidu.com/api/trans/vip/fieldtranslate?' + 'q=' + prompt + '&from=' + source_lang + '&to=' + target_lang + '&appid=' + appid + '&salt=' + salt + '&domain=' + domain + '&sign=' + sign
|
| 105 |
+
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
| 106 |
+
response = requests.post(url=url, headers=headers).json()
|
| 107 |
+
|
| 108 |
+
if response.get('error_code'):
|
| 109 |
+
BaiduFanyi.be_output("**Error**:" + response['error_msg'])
|
| 110 |
+
|
| 111 |
+
return BaiduFanyi.be_output(response['trans_result'][0]['dst'])
|
| 112 |
+
|
| 113 |
+
except Exception as e:
|
| 114 |
+
BaiduFanyi.be_output("**Error**:" + str(e))
|
agent/component/deepl.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
#
|
| 16 |
+
from abc import ABC
|
| 17 |
+
import re
|
| 18 |
+
from agent.component.base import ComponentBase, ComponentParamBase
|
| 19 |
+
import deepl
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class DeepLParam(ComponentParamBase):
|
| 23 |
+
"""
|
| 24 |
+
Define the DeepL component parameters.
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
def __init__(self):
|
| 28 |
+
super().__init__()
|
| 29 |
+
self.prompt = ""
|
| 30 |
+
self.auth_key = "xxx"
|
| 31 |
+
self.parameters = []
|
| 32 |
+
self.source_lang = 'ZH'
|
| 33 |
+
self.target_lang = 'EN-GB'
|
| 34 |
+
|
| 35 |
+
def check(self):
|
| 36 |
+
self.check_positive_integer(self.top_n, "Top N")
|
| 37 |
+
self.check_valid_value(self.source_lang, "Source language",
|
| 38 |
+
['AR', 'BG', 'CS', 'DA', 'DE', 'EL', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'ID', 'IT',
|
| 39 |
+
'JA', 'KO', 'LT', 'LV', 'NB', 'NL', 'PL', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'TR',
|
| 40 |
+
'UK', 'ZH'])
|
| 41 |
+
self.check_valid_value(self.target_lang, "Target language",
|
| 42 |
+
['AR', 'BG', 'CS', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'ES', 'ET', 'FI', 'FR', 'HU',
|
| 43 |
+
'ID', 'IT', 'JA', 'KO', 'LT', 'LV', 'NB', 'NL', 'PL', 'PT-BR', 'PT-PT', 'RO', 'RU',
|
| 44 |
+
'SK', 'SL', 'SV', 'TR', 'UK', 'ZH'])
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
class DeepL(ComponentBase, ABC):
|
| 48 |
+
component_name = "GitHub"
|
| 49 |
+
|
| 50 |
+
def _run(self, history, **kwargs):
|
| 51 |
+
prompt = self._param.prompt
|
| 52 |
+
|
| 53 |
+
ans = self.get_input()
|
| 54 |
+
ans = " - ".join(ans["content"]) if "content" in ans else ""
|
| 55 |
+
if not ans:
|
| 56 |
+
return DeepL.be_output("")
|
| 57 |
+
|
| 58 |
+
for para in self._param.parameters:
|
| 59 |
+
cpn = self._canvas.get_component(para["component_id"])["obj"]
|
| 60 |
+
_, out = cpn.output(allow_partial=False)
|
| 61 |
+
if "content" not in out.columns:
|
| 62 |
+
kwargs[para["key"]] = "Nothing"
|
| 63 |
+
else:
|
| 64 |
+
kwargs[para["key"]] = " - " + "\n - ".join(out["content"])
|
| 65 |
+
|
| 66 |
+
kwargs["input"] = ans
|
| 67 |
+
for n, v in kwargs.items():
|
| 68 |
+
prompt = re.sub(r"\{%s\}" % n, str(v), prompt)
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
translator = deepl.Translator(self._param.auth_key)
|
| 72 |
+
result = translator.translate_text(prompt, source_lang=self._param.source_lang,
|
| 73 |
+
target_lang=self._param.target_lang)
|
| 74 |
+
|
| 75 |
+
return DeepL.be_output(result.text)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
DeepL.be_output("**Error**:" + str(e))
|
agent/component/github.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
#
|
| 16 |
+
from abc import ABC
|
| 17 |
+
import pandas as pd
|
| 18 |
+
import requests
|
| 19 |
+
from agent.settings import DEBUG
|
| 20 |
+
from agent.component.base import ComponentBase, ComponentParamBase
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class GitHubParam(ComponentParamBase):
|
| 24 |
+
"""
|
| 25 |
+
Define the GitHub component parameters.
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
def __init__(self):
|
| 29 |
+
super().__init__()
|
| 30 |
+
self.top_n = 10
|
| 31 |
+
|
| 32 |
+
def check(self):
|
| 33 |
+
self.check_positive_integer(self.top_n, "Top N")
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class GitHub(ComponentBase, ABC):
|
| 37 |
+
component_name = "GitHub"
|
| 38 |
+
|
| 39 |
+
def _run(self, history, **kwargs):
|
| 40 |
+
ans = self.get_input()
|
| 41 |
+
ans = " - ".join(ans["content"]) if "content" in ans else ""
|
| 42 |
+
if not ans:
|
| 43 |
+
return GitHub.be_output("")
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
url = 'https://api.github.com/search/repositories?q=' + ans + '&sort=stars&order=desc&per_page=' + str(
|
| 47 |
+
self._param.top_n)
|
| 48 |
+
headers = {"Content-Type": "application/vnd.github+json", "X-GitHub-Api-Version": '2022-11-28'}
|
| 49 |
+
response = requests.get(url=url, headers=headers).json()
|
| 50 |
+
|
| 51 |
+
github_res = [{"content": '<a href="' + i["html_url"] + '">' + i["name"] + '</a>' + str(
|
| 52 |
+
i["description"]) + '\n stars:' + str(i['watchers'])} for i in response['items']]
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return GitHub.be_output("**ERROR**: " + str(e))
|
| 55 |
+
|
| 56 |
+
if not github_res:
|
| 57 |
+
return GitHub.be_output("")
|
| 58 |
+
|
| 59 |
+
df = pd.DataFrame(github_res)
|
| 60 |
+
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
|
| 61 |
+
return df
|
requirements.txt
CHANGED
|
@@ -10,6 +10,7 @@ cn2an==0.5.22
|
|
| 10 |
cohere==5.6.2
|
| 11 |
dashscope==1.14.1
|
| 12 |
datrie==0.8.2
|
|
|
|
| 13 |
demjson3==3.0.6
|
| 14 |
discord.py==2.3.2
|
| 15 |
duckduckgo_search==6.1.9
|
|
|
|
| 10 |
cohere==5.6.2
|
| 11 |
dashscope==1.14.1
|
| 12 |
datrie==0.8.2
|
| 13 |
+
deepl==1.18.0
|
| 14 |
demjson3==3.0.6
|
| 15 |
discord.py==2.3.2
|
| 16 |
duckduckgo_search==6.1.9
|
requirements_arm.txt
CHANGED
|
@@ -159,3 +159,4 @@ google_search_results==2.4.2
|
|
| 159 |
editdistance==0.8.1
|
| 160 |
markdown_to_json==2.1.1
|
| 161 |
scholarly==1.7.11
|
|
|
|
|
|
| 159 |
editdistance==0.8.1
|
| 160 |
markdown_to_json==2.1.1
|
| 161 |
scholarly==1.7.11
|
| 162 |
+
deepl==1.18.0
|
requirements_dev.txt
CHANGED
|
@@ -144,3 +144,4 @@ google_search_results==2.4.2
|
|
| 144 |
editdistance==0.8.1
|
| 145 |
markdown_to_json==2.1.1
|
| 146 |
scholarly==1.7.11
|
|
|
|
|
|
| 144 |
editdistance==0.8.1
|
| 145 |
markdown_to_json==2.1.1
|
| 146 |
scholarly==1.7.11
|
| 147 |
+
deepl==1.18.0
|