H
commited on
Commit
·
4eaa0b4
1
Parent(s):
ee833f3
Add component WenCai (#2269)
Browse files### What problem does this PR solve?
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
- agent/component/__init__.py +1 -0
- agent/component/wencai.py +74 -0
- requirements.txt +1 -0
- requirements_arm.txt +1 -0
agent/component/__init__.py
CHANGED
@@ -23,6 +23,7 @@ from .baidufanyi import BaiduFanyi, BaiduFanyiParam
|
|
23 |
from .qweather import QWeather, QWeatherParam
|
24 |
from .exesql import ExeSQL, ExeSQLParam
|
25 |
from .yahoofinance import YahooFinance, YahooFinanceParam
|
|
|
26 |
|
27 |
|
28 |
def component_class(class_name):
|
|
|
23 |
from .qweather import QWeather, QWeatherParam
|
24 |
from .exesql import ExeSQL, ExeSQLParam
|
25 |
from .yahoofinance import YahooFinance, YahooFinanceParam
|
26 |
+
from .wencai import WenCai, WenCaiParam
|
27 |
|
28 |
|
29 |
def component_class(class_name):
|
agent/component/wencai.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 pywencai
|
19 |
+
from agent.component.base import ComponentBase, ComponentParamBase
|
20 |
+
|
21 |
+
|
22 |
+
class WenCaiParam(ComponentParamBase):
|
23 |
+
"""
|
24 |
+
Define the WenCai component parameters.
|
25 |
+
"""
|
26 |
+
|
27 |
+
def __init__(self):
|
28 |
+
super().__init__()
|
29 |
+
self.top_n = 10
|
30 |
+
self.query_type = "stock"
|
31 |
+
|
32 |
+
def check(self):
|
33 |
+
self.check_positive_integer(self.top_n, "Top N")
|
34 |
+
self.check_valid_value(self.query_type, "Query type",
|
35 |
+
['stock', 'zhishu', 'fund', 'hkstock', 'usstock', 'threeboard', 'conbond', 'insurance',
|
36 |
+
'futures', 'lccp',
|
37 |
+
'foreign_exchange'])
|
38 |
+
|
39 |
+
|
40 |
+
class WenCai(ComponentBase, ABC):
|
41 |
+
component_name = "WenCai"
|
42 |
+
|
43 |
+
def _run(self, history, **kwargs):
|
44 |
+
ans = self.get_input()
|
45 |
+
ans = ",".join(ans["content"]) if "content" in ans else ""
|
46 |
+
if not ans:
|
47 |
+
return WenCai.be_output("")
|
48 |
+
|
49 |
+
try:
|
50 |
+
wencai_res = []
|
51 |
+
res = pywencai.get(query=ans, query_type=self._param.query_type, perpage=self._param.top_n)
|
52 |
+
if isinstance(res, pd.DataFrame):
|
53 |
+
wencai_res.append({"content": res.to_markdown()})
|
54 |
+
if isinstance(res, dict):
|
55 |
+
for item in res.items():
|
56 |
+
if isinstance(item[1], list):
|
57 |
+
wencai_res.append({"content": item[0] + "\n" + pd.DataFrame(item[1]).to_markdown()})
|
58 |
+
continue
|
59 |
+
if isinstance(item[1], str):
|
60 |
+
wencai_res.append({"content": item[0] + "\n" + item[1]})
|
61 |
+
continue
|
62 |
+
if isinstance(item[1], dict):
|
63 |
+
if "meta" in item[1].keys():
|
64 |
+
continue
|
65 |
+
wencai_res.append({"content": pd.DataFrame.from_dict(item[1], orient='index').to_markdown()})
|
66 |
+
continue
|
67 |
+
wencai_res.append({"content": item[0] + "\n" + str(item[1])})
|
68 |
+
except Exception as e:
|
69 |
+
return WenCai.be_output("**ERROR**: " + str(e))
|
70 |
+
|
71 |
+
if not wencai_res:
|
72 |
+
return WenCai.be_output("")
|
73 |
+
|
74 |
+
return pd.DataFrame(wencai_res)
|
requirements.txt
CHANGED
@@ -64,6 +64,7 @@ pytest==8.2.2
|
|
64 |
python-dotenv==1.0.1
|
65 |
python_dateutil==2.8.2
|
66 |
python_pptx==0.6.23
|
|
|
67 |
qianfan==0.4.6
|
68 |
readability_lxml==0.8.1
|
69 |
redis==5.0.3
|
|
|
64 |
python-dotenv==1.0.1
|
65 |
python_dateutil==2.8.2
|
66 |
python_pptx==0.6.23
|
67 |
+
pywencai==0.12.2
|
68 |
qianfan==0.4.6
|
69 |
readability_lxml==0.8.1
|
70 |
redis==5.0.3
|
requirements_arm.txt
CHANGED
@@ -169,3 +169,4 @@ psycopg2-binary==2.9.9
|
|
169 |
tabulate==0.9.0
|
170 |
vertexai==1.64.0
|
171 |
yfinance==0.2.43
|
|
|
|
169 |
tabulate==0.9.0
|
170 |
vertexai==1.64.0
|
171 |
yfinance==0.2.43
|
172 |
+
pywencai==0.12.2
|