Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub, deepl, baidu-fanyi #1857

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
69a1b56
Update wikipedia.py
guoyuhao2330 Jul 15, 2024
6bbb1df
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 15, 2024
4e3067a
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 17, 2024
bc8d5ad
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 17, 2024
678994d
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 17, 2024
268c0c7
Update requirements.txt
guoyuhao2330 Jul 17, 2024
757ca5b
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 18, 2024
dfe71cb
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 18, 2024
7aba129
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 18, 2024
e52e08f
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 19, 2024
c140c70
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 19, 2024
92cf451
Update llm_service.py
guoyuhao2330 Jul 19, 2024
ad5a5b8
Update index.tsx
guoyuhao2330 Jul 19, 2024
4b49bb9
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 22, 2024
12baffc
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 22, 2024
585a0f6
Update init_data.py
guoyuhao2330 Jul 22, 2024
f3b3451
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 25, 2024
31af72c
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 25, 2024
e23f5b5
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 26, 2024
941c29d
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 26, 2024
bace5b5
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 26, 2024
8936941
Merge branch 'infiniflow:main' into main
guoyuhao2330 Jul 29, 2024
4fe66d8
Merge branch 'infiniflow:main' into main
guoyuhao2330 Aug 2, 2024
3cad1dd
Merge branch 'infiniflow:main' into main
guoyuhao2330 Aug 5, 2024
4ce659e
Merge branch 'infiniflow:main' into main
guoyuhao2330 Aug 7, 2024
08108cb
Add files via upload
guoyuhao2330 Aug 7, 2024
97c0b76
Update __init__.py
guoyuhao2330 Aug 7, 2024
bf26011
Update requirements.txt
guoyuhao2330 Aug 7, 2024
6e5e3da
Update requirements_arm.txt
guoyuhao2330 Aug 7, 2024
f0d5a89
Update requirements_dev.txt
guoyuhao2330 Aug 7, 2024
ced5b20
Update deepl.py
guoyuhao2330 Aug 8, 2024
c02a953
Update agent/component/deepl.py
guoyuhao2330 Aug 8, 2024
a88eace
Update agent/component/github.py
guoyuhao2330 Aug 8, 2024
79caa10
Update baidufanyi.py
guoyuhao2330 Aug 8, 2024
48fd395
Update deepl.py
guoyuhao2330 Aug 8, 2024
538191a
Update github.py
guoyuhao2330 Aug 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion agent/component/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from .google import Google, GoogleParam
from .bing import Bing, BingParam
from .googlescholar import GoogleScholar, GoogleScholarParam

from .deepl import DeepL, DeepLParam
from .github import GitHub, GitHubParam
from .baidufanyi import BaiduFanyi, BaiduFanyiParam

def component_class(class_name):
m = importlib.import_module("agent.component")
Expand Down
114 changes: 114 additions & 0 deletions agent/component/baidufanyi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import random
from abc import ABC
import requests
import re
from agent.component.base import ComponentBase, ComponentParamBase
from hashlib import md5


class BaiduFanyiParam(ComponentParamBase):
"""
Define the BaiduFanyi component parameters.
"""

def __init__(self):
super().__init__()
self.prompt = ""
self.appid = "xxx"
self.secret_key = "xxx"
self.trans_type = 'translate'
self.parameters = []
self.source_lang = 'auto'
self.target_lang = 'auto'
self.domain = 'finance'

def check(self):
self.check_positive_integer(self.top_n, "Top N")
self.check_empty(self.appid, "BaiduFanyi APPID")
self.check_empty(self.secret_key, "BaiduFanyi Secret Key")
self.check_valid_value(self.trans_type, "Translate type", ['translate', 'fieldtranslate'])
self.check_valid_value(self.trans_type, "Translate domain",
['it', 'finance', 'machinery', 'senimed', 'novel', 'academic', 'aerospace', 'wiki',
'news', 'law', 'contract'])
self.check_valid_value(self.source_lang, "Source language",
['auto', 'zh', 'en', 'yue', 'wyw', 'jp', 'kor', 'fra', 'spa', 'th', 'ara', 'ru', 'pt',
'de', 'it', 'el', 'nl', 'pl', 'bul', 'est', 'dan', 'fin', 'cs', 'rom', 'slo', 'swe',
'hu', 'cht', 'vie'])
self.check_valid_value(self.target_lang, "Target language",
['auto', 'zh', 'en', 'yue', 'wyw', 'jp', 'kor', 'fra', 'spa', 'th', 'ara', 'ru', 'pt',
'de', 'it', 'el', 'nl', 'pl', 'bul', 'est', 'dan', 'fin', 'cs', 'rom', 'slo', 'swe',
'hu', 'cht', 'vie'])
self.check_valid_value(self.domain, "Translate field",
['it', 'finance', 'machinery', 'senimed', 'novel', 'academic', 'aerospace', 'wiki',
'news', 'law', 'contract'])


class BaiduFanyi(ComponentBase, ABC):
component_name = "BaiduFanyi"

def _run(self, history, **kwargs):
prompt = self._param.prompt

ans = self.get_input()
ans = " - ".join(ans["content"]) if "content" in ans else ""
if not ans:
return BaiduFanyi.be_output("")

for para in self._param.parameters:
cpn = self._canvas.get_component(para["component_id"])["obj"]
_, out = cpn.output(allow_partial=False)
if "content" not in out.columns:
kwargs[para["key"]] = "Nothing"
else:
kwargs[para["key"]] = " - " + "\n - ".join(out["content"])

kwargs["input"] = ans
for n, v in kwargs.items():
prompt = re.sub(r"\{%s\}" % n, str(v), prompt)

try:
source_lang = self._param.source_lang
target_lang = self._param.target_lang
appid = self._param.appid
salt = random.randint(32768, 65536)
secret_key = self._param.secret_key

if self._param.trans_type == 'translate':
sign = md5((appid + prompt + salt + secret_key).encode('utf-8')).hexdigest()
url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?' + 'q=' + prompt + '&from=' + source_lang + '&to=' + target_lang + '&appid=' + appid + '&salt=' + salt + '&sign=' + sign
headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(url=url, headers=headers).json()

if response.get('error_code'):
BaiduFanyi.be_output("**Error**:" + response['error_msg'])

return BaiduFanyi.be_output(response['trans_result'][0]['dst'])
elif self._param.trans_type == 'fieldtranslate':
domain = self._param.domain
sign = md5((appid + prompt + salt + domain + secret_key).encode('utf-8')).hexdigest()
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
headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(url=url, headers=headers).json()

if response.get('error_code'):
BaiduFanyi.be_output("**Error**:" + response['error_msg'])

return BaiduFanyi.be_output(response['trans_result'][0]['dst'])

except Exception as e:
BaiduFanyi.be_output("**Error**:" + str(e))
77 changes: 77 additions & 0 deletions agent/component/deepl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from abc import ABC
import re
from agent.component.base import ComponentBase, ComponentParamBase
import deepl


class DeepLParam(ComponentParamBase):
"""
Define the DeepL component parameters.
"""

def __init__(self):
super().__init__()
self.prompt = ""
self.auth_key = "xxx"
self.parameters = []
self.source_lang = 'ZH'
self.target_lang = 'EN-GB'

def check(self):
self.check_positive_integer(self.top_n, "Top N")
self.check_valid_value(self.source_lang, "Source language",
['AR', 'BG', 'CS', 'DA', 'DE', 'EL', 'EN', 'ES', 'ET', 'FI', 'FR', 'HU', 'ID', 'IT',
'JA', 'KO', 'LT', 'LV', 'NB', 'NL', 'PL', 'PT', 'RO', 'RU', 'SK', 'SL', 'SV', 'TR',
'UK', 'ZH'])
self.check_valid_value(self.target_lang, "Target language",
['AR', 'BG', 'CS', 'DA', 'DE', 'EL', 'EN-GB', 'EN-US', 'ES', 'ET', 'FI', 'FR', 'HU',
'ID', 'IT', 'JA', 'KO', 'LT', 'LV', 'NB', 'NL', 'PL', 'PT-BR', 'PT-PT', 'RO', 'RU',
'SK', 'SL', 'SV', 'TR', 'UK', 'ZH'])


class DeepL(ComponentBase, ABC):
component_name = "GitHub"

def _run(self, history, **kwargs):
prompt = self._param.prompt

ans = self.get_input()
ans = " - ".join(ans["content"]) if "content" in ans else ""
if not ans:
return DeepL.be_output("")

for para in self._param.parameters:
cpn = self._canvas.get_component(para["component_id"])["obj"]
_, out = cpn.output(allow_partial=False)
if "content" not in out.columns:
kwargs[para["key"]] = "Nothing"
else:
kwargs[para["key"]] = " - " + "\n - ".join(out["content"])

kwargs["input"] = ans
for n, v in kwargs.items():
prompt = re.sub(r"\{%s\}" % n, str(v), prompt)

try:
translator = deepl.Translator(self._param.auth_key)
result = translator.translate_text(prompt, source_lang=self._param.source_lang,
target_lang=self._param.target_lang)

return DeepL.be_output(result.text)
except Exception as e:
DeepL.be_output("**Error**:" + str(e))
61 changes: 61 additions & 0 deletions agent/component/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#
# Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from abc import ABC
import pandas as pd
import requests
from agent.settings import DEBUG
from agent.component.base import ComponentBase, ComponentParamBase


class GitHubParam(ComponentParamBase):
"""
Define the GitHub component parameters.
"""

def __init__(self):
super().__init__()
self.top_n = 10

def check(self):
self.check_positive_integer(self.top_n, "Top N")


class GitHub(ComponentBase, ABC):
component_name = "GitHub"

def _run(self, history, **kwargs):
ans = self.get_input()
ans = " - ".join(ans["content"]) if "content" in ans else ""
if not ans:
return GitHub.be_output("")

try:
url = 'https://api.github.com/search/repositories?q=' + ans + '&sort=stars&order=desc&per_page=' + str(
self._param.top_n)
headers = {"Content-Type": "application/vnd.github+json", "X-GitHub-Api-Version": '2022-11-28'}
response = requests.get(url=url, headers=headers).json()

github_res = [{"content": '<a href="' + i["html_url"] + '">' + i["name"] + '</a>' + str(
i["description"]) + '\n stars:' + str(i['watchers'])} for i in response['items']]
except Exception as e:
return GitHub.be_output("**ERROR**: " + str(e))

if not github_res:
return GitHub.be_output("")

df = pd.DataFrame(github_res)
if DEBUG: print(df, ":::::::::::::::::::::::::::::::::")
return df
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ cn2an==0.5.22
cohere==5.6.2
dashscope==1.14.1
datrie==0.8.2
deepl==1.18.0
demjson3==3.0.6
discord.py==2.3.2
duckduckgo_search==6.1.9
Expand Down
1 change: 1 addition & 0 deletions requirements_arm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,4 @@ jina==3.27.2
editdistance==0.8.1
markdown_to_json==2.1.1
scholarly==1.7.11
deepl==1.18.0
1 change: 1 addition & 0 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,4 @@ jina==3.27.2
editdistance==0.8.1
markdown_to_json==2.1.1
scholarly==1.7.11
deepl==1.18.0