H
commited on
Commit
·
37b4567
1
Parent(s):
4c2d648
Add component qweather (#1873)
Browse files### What problem does this PR solve?
#1739
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- agent/component/__init__.py +1 -0
- agent/component/baidufanyi.py +4 -19
- agent/component/deepl.py +1 -16
- agent/component/qweather.py +111 -0
agent/component/__init__.py
CHANGED
@@ -20,6 +20,7 @@ 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")
|
|
|
20 |
from .deepl import DeepL, DeepLParam
|
21 |
from .github import GitHub, GitHubParam
|
22 |
from .baidufanyi import BaiduFanyi, BaiduFanyiParam
|
23 |
+
from .qweather import QWeather, QWeatherParam
|
24 |
|
25 |
def component_class(class_name):
|
26 |
m = importlib.import_module("agent.component")
|
agent/component/baidufanyi.py
CHANGED
@@ -16,7 +16,6 @@
|
|
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 |
|
@@ -28,7 +27,6 @@ class BaiduFanyiParam(ComponentParamBase):
|
|
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'
|
@@ -62,25 +60,12 @@ 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
|
@@ -89,8 +74,8 @@ class BaiduFanyi(ComponentBase, ABC):
|
|
89 |
secret_key = self._param.secret_key
|
90 |
|
91 |
if self._param.trans_type == 'translate':
|
92 |
-
sign = md5((appid +
|
93 |
-
url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?' + 'q=' +
|
94 |
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
95 |
response = requests.post(url=url, headers=headers).json()
|
96 |
|
@@ -100,8 +85,8 @@ class BaiduFanyi(ComponentBase, ABC):
|
|
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 +
|
104 |
-
url = 'http://api.fanyi.baidu.com/api/trans/vip/fieldtranslate?' + 'q=' +
|
105 |
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
106 |
response = requests.post(url=url, headers=headers).json()
|
107 |
|
|
|
16 |
import random
|
17 |
from abc import ABC
|
18 |
import requests
|
|
|
19 |
from agent.component.base import ComponentBase, ComponentParamBase
|
20 |
from hashlib import md5
|
21 |
|
|
|
27 |
|
28 |
def __init__(self):
|
29 |
super().__init__()
|
|
|
30 |
self.appid = "xxx"
|
31 |
self.secret_key = "xxx"
|
32 |
self.trans_type = 'translate'
|
|
|
60 |
component_name = "BaiduFanyi"
|
61 |
|
62 |
def _run(self, history, **kwargs):
|
|
|
63 |
|
64 |
ans = self.get_input()
|
65 |
ans = " - ".join(ans["content"]) if "content" in ans else ""
|
66 |
if not ans:
|
67 |
return BaiduFanyi.be_output("")
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
try:
|
70 |
source_lang = self._param.source_lang
|
71 |
target_lang = self._param.target_lang
|
|
|
74 |
secret_key = self._param.secret_key
|
75 |
|
76 |
if self._param.trans_type == 'translate':
|
77 |
+
sign = md5((appid + ans + salt + secret_key).encode('utf-8')).hexdigest()
|
78 |
+
url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?' + 'q=' + ans + '&from=' + source_lang + '&to=' + target_lang + '&appid=' + appid + '&salt=' + salt + '&sign=' + sign
|
79 |
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
80 |
response = requests.post(url=url, headers=headers).json()
|
81 |
|
|
|
85 |
return BaiduFanyi.be_output(response['trans_result'][0]['dst'])
|
86 |
elif self._param.trans_type == 'fieldtranslate':
|
87 |
domain = self._param.domain
|
88 |
+
sign = md5((appid + ans + salt + domain + secret_key).encode('utf-8')).hexdigest()
|
89 |
+
url = 'http://api.fanyi.baidu.com/api/trans/vip/fieldtranslate?' + 'q=' + ans + '&from=' + source_lang + '&to=' + target_lang + '&appid=' + appid + '&salt=' + salt + '&domain=' + domain + '&sign=' + sign
|
90 |
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
91 |
response = requests.post(url=url, headers=headers).json()
|
92 |
|
agent/component/deepl.py
CHANGED
@@ -26,7 +26,6 @@ class DeepLParam(ComponentParamBase):
|
|
26 |
|
27 |
def __init__(self):
|
28 |
super().__init__()
|
29 |
-
self.prompt = ""
|
30 |
self.auth_key = "xxx"
|
31 |
self.parameters = []
|
32 |
self.source_lang = 'ZH'
|
@@ -48,28 +47,14 @@ 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(
|
73 |
target_lang=self._param.target_lang)
|
74 |
|
75 |
return DeepL.be_output(result.text)
|
|
|
26 |
|
27 |
def __init__(self):
|
28 |
super().__init__()
|
|
|
29 |
self.auth_key = "xxx"
|
30 |
self.parameters = []
|
31 |
self.source_lang = 'ZH'
|
|
|
47 |
component_name = "GitHub"
|
48 |
|
49 |
def _run(self, history, **kwargs):
|
|
|
|
|
50 |
ans = self.get_input()
|
51 |
ans = " - ".join(ans["content"]) if "content" in ans else ""
|
52 |
if not ans:
|
53 |
return DeepL.be_output("")
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
try:
|
56 |
translator = deepl.Translator(self._param.auth_key)
|
57 |
+
result = translator.translate_text(ans, source_lang=self._param.source_lang,
|
58 |
target_lang=self._param.target_lang)
|
59 |
|
60 |
return DeepL.be_output(result.text)
|
agent/component/qweather.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.component.base import ComponentBase, ComponentParamBase
|
20 |
+
|
21 |
+
|
22 |
+
class QWeatherParam(ComponentParamBase):
|
23 |
+
"""
|
24 |
+
Define the QWeather component parameters.
|
25 |
+
"""
|
26 |
+
|
27 |
+
def __init__(self):
|
28 |
+
super().__init__()
|
29 |
+
self.web_apikey = "xxx"
|
30 |
+
self.lang = "zh"
|
31 |
+
self.type = "weather"
|
32 |
+
self.user_type = 'free'
|
33 |
+
self.error_code = {
|
34 |
+
"204": "The request was successful, but the region you are querying does not have the data you need at this time.",
|
35 |
+
"400": "Request error, may contain incorrect request parameters or missing mandatory request parameters.",
|
36 |
+
"401": "Authentication fails, possibly using the wrong KEY, wrong digital signature, wrong type of KEY (e.g. using the SDK's KEY to access the Web API).",
|
37 |
+
"402": "Exceeded the number of accesses or the balance is not enough to support continued access to the service, you can recharge, upgrade the accesses or wait for the accesses to be reset.",
|
38 |
+
"403": "No access, may be the binding PackageName, BundleID, domain IP address is inconsistent, or the data that requires additional payment.",
|
39 |
+
"404": "The queried data or region does not exist.",
|
40 |
+
"429": "Exceeded the limited QPM (number of accesses per minute), please refer to the QPM description",
|
41 |
+
"500": "No response or timeout, interface service abnormality please contact us"
|
42 |
+
}
|
43 |
+
# Weather
|
44 |
+
self.time_period = 'now'
|
45 |
+
|
46 |
+
def check(self):
|
47 |
+
self.check_empty(self.web_apikey, "BaiduFanyi APPID")
|
48 |
+
self.check_valid_value(self.type, "Type", ["weather", "indices", "airquality"])
|
49 |
+
self.check_valid_value(self.user_type, "Free subscription or paid subscription", ["free", "paid"])
|
50 |
+
self.check_valid_value(self.lang, "Use language",
|
51 |
+
['zh', 'zh-hant', 'en', 'de', 'es', 'fr', 'it', 'ja', 'ko', 'ru', 'hi', 'th', 'ar', 'pt',
|
52 |
+
'bn', 'ms', 'nl', 'el', 'la', 'sv', 'id', 'pl', 'tr', 'cs', 'et', 'vi', 'fil', 'fi',
|
53 |
+
'he', 'is', 'nb'])
|
54 |
+
self.check_vaild_value(self.time_period, "Time period", ['now', '3d', '7d', '10d', '15d', '30d'])
|
55 |
+
|
56 |
+
|
57 |
+
class QWeather(ComponentBase, ABC):
|
58 |
+
component_name = "QWeather"
|
59 |
+
|
60 |
+
def _run(self, history, **kwargs):
|
61 |
+
ans = self.get_input()
|
62 |
+
ans = "".join(ans["content"]) if "content" in ans else ""
|
63 |
+
if not ans:
|
64 |
+
return QWeather.be_output("")
|
65 |
+
|
66 |
+
try:
|
67 |
+
response = requests.get(
|
68 |
+
url="https://geoapi.qweather.com/v2/city/lookup?location=" + ans + "&key=" + self._param.web_apikey).json()
|
69 |
+
if response["code"] == "200":
|
70 |
+
location_id = response["location"][0]["id"]
|
71 |
+
else:
|
72 |
+
return QWeather.be_output("**Error**" + self._param.error_code[response["code"]])
|
73 |
+
|
74 |
+
base_url = "https://api.qweather.com/v7/" if self._param.user_type == 'paid' else "https://devapi.qweather.com/v7/"
|
75 |
+
|
76 |
+
if self._param.type == "weather":
|
77 |
+
url = base_url + "weather/" + self._param.time_period + "?location=" + location_id + "&key=" + self._param.web_apikey + "&lang=" + self._param.lang
|
78 |
+
response = requests.get(url=url).json()
|
79 |
+
if response["code"] == "200":
|
80 |
+
if self._param.time_period == "now":
|
81 |
+
return QWeather.be_output(str(response["now"]))
|
82 |
+
else:
|
83 |
+
qweather_res = [{"content": str(i) + "\n"} for i in response["daily"]]
|
84 |
+
if not qweather_res:
|
85 |
+
return QWeather.be_output("")
|
86 |
+
|
87 |
+
df = pd.DataFrame(qweather_res)
|
88 |
+
return df
|
89 |
+
else:
|
90 |
+
return QWeather.be_output("**Error**" + self._param.error_code[response["code"]])
|
91 |
+
|
92 |
+
elif self._param.type == "indices":
|
93 |
+
url = base_url + "indices/1d?type=0&location=" + location_id + "&key=" + self._param.web_apikey + "&lang=" + self._param.lang
|
94 |
+
response = requests.get(url=url).json()
|
95 |
+
if response["code"] == "200":
|
96 |
+
indices_res = response["daily"][0]["date"] + "\n" + "\n".join(
|
97 |
+
[i["name"] + ": " + i["category"] + ", " + i["text"] for i in response["daily"]])
|
98 |
+
return QWeather.be_output(indices_res)
|
99 |
+
|
100 |
+
else:
|
101 |
+
return QWeather.be_output("**Error**" + self._param.error_code[response["code"]])
|
102 |
+
|
103 |
+
elif self._param.type == "airquality":
|
104 |
+
url = base_url + "air/now?location=" + location_id + "&key=" + self._param.web_apikey + "&lang=" + self._param.lang
|
105 |
+
response = requests.get(url=url).json()
|
106 |
+
if response["code"] == "200":
|
107 |
+
return QWeather.be_output(str(response["now"]))
|
108 |
+
else:
|
109 |
+
return QWeather.be_output("**Error**" + self._param.error_code[response["code"]])
|
110 |
+
except Exception as e:
|
111 |
+
return QWeather.be_output("**Error**" + str(e))
|