Add component qweather (#1873)

### What problem does this PR solve?

#1739 

### Type of change

- [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
H 2024-08-08 17:57:46 +08:00 committed by GitHub
parent ffb3fc4bf5
commit 193aa3ba88
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 117 additions and 35 deletions

View File

@ -20,6 +20,7 @@ from .googlescholar import GoogleScholar, GoogleScholarParam
from .deepl import DeepL, DeepLParam from .deepl import DeepL, DeepLParam
from .github import GitHub, GitHubParam from .github import GitHub, GitHubParam
from .baidufanyi import BaiduFanyi, BaiduFanyiParam from .baidufanyi import BaiduFanyi, BaiduFanyiParam
from .qweather import QWeather, QWeatherParam
def component_class(class_name): def component_class(class_name):
m = importlib.import_module("agent.component") m = importlib.import_module("agent.component")

View File

@ -16,7 +16,6 @@
import random import random
from abc import ABC from abc import ABC
import requests import requests
import re
from agent.component.base import ComponentBase, ComponentParamBase from agent.component.base import ComponentBase, ComponentParamBase
from hashlib import md5 from hashlib import md5
@ -28,7 +27,6 @@ class BaiduFanyiParam(ComponentParamBase):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.prompt = ""
self.appid = "xxx" self.appid = "xxx"
self.secret_key = "xxx" self.secret_key = "xxx"
self.trans_type = 'translate' self.trans_type = 'translate'
@ -62,25 +60,12 @@ class BaiduFanyi(ComponentBase, ABC):
component_name = "BaiduFanyi" component_name = "BaiduFanyi"
def _run(self, history, **kwargs): def _run(self, history, **kwargs):
prompt = self._param.prompt
ans = self.get_input() ans = self.get_input()
ans = " - ".join(ans["content"]) if "content" in ans else "" ans = " - ".join(ans["content"]) if "content" in ans else ""
if not ans: if not ans:
return BaiduFanyi.be_output("") 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: try:
source_lang = self._param.source_lang source_lang = self._param.source_lang
target_lang = self._param.target_lang target_lang = self._param.target_lang
@ -89,8 +74,8 @@ class BaiduFanyi(ComponentBase, ABC):
secret_key = self._param.secret_key secret_key = self._param.secret_key
if self._param.trans_type == 'translate': if self._param.trans_type == 'translate':
sign = md5((appid + prompt + salt + secret_key).encode('utf-8')).hexdigest() sign = md5((appid + ans + 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 url = 'http://api.fanyi.baidu.com/api/trans/vip/translate?' + 'q=' + ans + '&from=' + source_lang + '&to=' + target_lang + '&appid=' + appid + '&salt=' + salt + '&sign=' + sign
headers = {"Content-Type": "application/x-www-form-urlencoded"} headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(url=url, headers=headers).json() response = requests.post(url=url, headers=headers).json()
@ -100,8 +85,8 @@ class BaiduFanyi(ComponentBase, ABC):
return BaiduFanyi.be_output(response['trans_result'][0]['dst']) return BaiduFanyi.be_output(response['trans_result'][0]['dst'])
elif self._param.trans_type == 'fieldtranslate': elif self._param.trans_type == 'fieldtranslate':
domain = self._param.domain domain = self._param.domain
sign = md5((appid + prompt + salt + domain + secret_key).encode('utf-8')).hexdigest() sign = md5((appid + ans + 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 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
headers = {"Content-Type": "application/x-www-form-urlencoded"} headers = {"Content-Type": "application/x-www-form-urlencoded"}
response = requests.post(url=url, headers=headers).json() response = requests.post(url=url, headers=headers).json()

View File

@ -26,7 +26,6 @@ class DeepLParam(ComponentParamBase):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.prompt = ""
self.auth_key = "xxx" self.auth_key = "xxx"
self.parameters = [] self.parameters = []
self.source_lang = 'ZH' self.source_lang = 'ZH'
@ -48,28 +47,14 @@ class DeepL(ComponentBase, ABC):
component_name = "GitHub" component_name = "GitHub"
def _run(self, history, **kwargs): def _run(self, history, **kwargs):
prompt = self._param.prompt
ans = self.get_input() ans = self.get_input()
ans = " - ".join(ans["content"]) if "content" in ans else "" ans = " - ".join(ans["content"]) if "content" in ans else ""
if not ans: if not ans:
return DeepL.be_output("") 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: try:
translator = deepl.Translator(self._param.auth_key) translator = deepl.Translator(self._param.auth_key)
result = translator.translate_text(prompt, source_lang=self._param.source_lang, result = translator.translate_text(ans, source_lang=self._param.source_lang,
target_lang=self._param.target_lang) target_lang=self._param.target_lang)
return DeepL.be_output(result.text) return DeepL.be_output(result.text)

111
agent/component/qweather.py Normal file
View File

@ -0,0 +1,111 @@
#
# 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.component.base import ComponentBase, ComponentParamBase
class QWeatherParam(ComponentParamBase):
"""
Define the QWeather component parameters.
"""
def __init__(self):
super().__init__()
self.web_apikey = "xxx"
self.lang = "zh"
self.type = "weather"
self.user_type = 'free'
self.error_code = {
"204": "The request was successful, but the region you are querying does not have the data you need at this time.",
"400": "Request error, may contain incorrect request parameters or missing mandatory request parameters.",
"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).",
"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.",
"403": "No access, may be the binding PackageName, BundleID, domain IP address is inconsistent, or the data that requires additional payment.",
"404": "The queried data or region does not exist.",
"429": "Exceeded the limited QPM (number of accesses per minute), please refer to the QPM description",
"500": "No response or timeout, interface service abnormality please contact us"
}
# Weather
self.time_period = 'now'
def check(self):
self.check_empty(self.web_apikey, "BaiduFanyi APPID")
self.check_valid_value(self.type, "Type", ["weather", "indices", "airquality"])
self.check_valid_value(self.user_type, "Free subscription or paid subscription", ["free", "paid"])
self.check_valid_value(self.lang, "Use language",
['zh', 'zh-hant', 'en', 'de', 'es', 'fr', 'it', 'ja', 'ko', 'ru', 'hi', 'th', 'ar', 'pt',
'bn', 'ms', 'nl', 'el', 'la', 'sv', 'id', 'pl', 'tr', 'cs', 'et', 'vi', 'fil', 'fi',
'he', 'is', 'nb'])
self.check_vaild_value(self.time_period, "Time period", ['now', '3d', '7d', '10d', '15d', '30d'])
class QWeather(ComponentBase, ABC):
component_name = "QWeather"
def _run(self, history, **kwargs):
ans = self.get_input()
ans = "".join(ans["content"]) if "content" in ans else ""
if not ans:
return QWeather.be_output("")
try:
response = requests.get(
url="https://geoapi.qweather.com/v2/city/lookup?location=" + ans + "&key=" + self._param.web_apikey).json()
if response["code"] == "200":
location_id = response["location"][0]["id"]
else:
return QWeather.be_output("**Error**" + self._param.error_code[response["code"]])
base_url = "https://api.qweather.com/v7/" if self._param.user_type == 'paid' else "https://devapi.qweather.com/v7/"
if self._param.type == "weather":
url = base_url + "weather/" + self._param.time_period + "?location=" + location_id + "&key=" + self._param.web_apikey + "&lang=" + self._param.lang
response = requests.get(url=url).json()
if response["code"] == "200":
if self._param.time_period == "now":
return QWeather.be_output(str(response["now"]))
else:
qweather_res = [{"content": str(i) + "\n"} for i in response["daily"]]
if not qweather_res:
return QWeather.be_output("")
df = pd.DataFrame(qweather_res)
return df
else:
return QWeather.be_output("**Error**" + self._param.error_code[response["code"]])
elif self._param.type == "indices":
url = base_url + "indices/1d?type=0&location=" + location_id + "&key=" + self._param.web_apikey + "&lang=" + self._param.lang
response = requests.get(url=url).json()
if response["code"] == "200":
indices_res = response["daily"][0]["date"] + "\n" + "\n".join(
[i["name"] + ": " + i["category"] + ", " + i["text"] for i in response["daily"]])
return QWeather.be_output(indices_res)
else:
return QWeather.be_output("**Error**" + self._param.error_code[response["code"]])
elif self._param.type == "airquality":
url = base_url + "air/now?location=" + location_id + "&key=" + self._param.web_apikey + "&lang=" + self._param.lang
response = requests.get(url=url).json()
if response["code"] == "200":
return QWeather.be_output(str(response["now"]))
else:
return QWeather.be_output("**Error**" + self._param.error_code[response["code"]])
except Exception as e:
return QWeather.be_output("**Error**" + str(e))