H commited on
Commit
5d39832
·
1 Parent(s): 3927930

Add component Jin10 (#2271)

Browse files

### What problem does this PR solve?

_Briefly describe what this PR aims to solve. Include background context
that will help reviewers understand the purpose of the PR._

### Type of change

- [x] New Feature (non-breaking change which adds functionality)

agent/component/__init__.py CHANGED
@@ -24,6 +24,7 @@ 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):
 
24
  from .exesql import ExeSQL, ExeSQLParam
25
  from .yahoofinance import YahooFinance, YahooFinanceParam
26
  from .wencai import WenCai, WenCaiParam
27
+ from .jin10 import Jin10, Jin10Param
28
 
29
 
30
  def component_class(class_name):
agent/component/jin10.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 json
17
+ from abc import ABC
18
+ import pandas as pd
19
+ import requests
20
+ from agent.component.base import ComponentBase, ComponentParamBase
21
+
22
+
23
+ class Jin10Param(ComponentParamBase):
24
+ """
25
+ Define the Jin10 component parameters.
26
+ """
27
+
28
+ def __init__(self):
29
+ super().__init__()
30
+ self.type = "flash"
31
+ self.secret_key = "xxx"
32
+ self.flash_type = '1'
33
+ self.calendar_type = 'cj'
34
+ self.calendar_datatype = 'data'
35
+ self.symbols_type = 'GOODS'
36
+ self.symbols_datatype = 'symbols'
37
+ self.contain = ""
38
+ self.filter = ""
39
+
40
+ def check(self):
41
+ self.check_valid_value(self.type, "Type", ['flash', 'calendar', 'symbols', 'news'])
42
+ self.check_valid_value(self.flash_type, "Flash Type", ['1', '2', '3', '4', '5'])
43
+ self.check_valid_value(self.calendar_type, "Calendar Type", ['cj', 'qh', 'hk', 'us'])
44
+ self.check_valid_value(self.calendar_datatype, "Calendar DataType", ['data', 'event', 'holiday'])
45
+ self.check_valid_value(self.symbols_type, "Symbols Type", ['GOODS', 'FOREX', 'FUTURE', 'CRYPTO'])
46
+ self.check_valid_value(self.symbols_datatype, 'Symbols DataType', ['symbols', 'quotes'])
47
+
48
+
49
+ class Jin10(ComponentBase, ABC):
50
+ component_name = "Jin10"
51
+
52
+ def _run(self, history, **kwargs):
53
+ ans = self.get_input()
54
+ ans = " - ".join(ans["content"]) if "content" in ans else ""
55
+ if not ans:
56
+ return Jin10.be_output("")
57
+
58
+ jin10_res = []
59
+ headers = {'secret-key': self._param.secret_key}
60
+ try:
61
+ if self._param.type == "flash":
62
+ params = {
63
+ 'category': self._param.flash_type,
64
+ 'contain': self._param.contain,
65
+ 'filter': self._param.filter
66
+ }
67
+ response = requests.get(
68
+ url='https://open-data-api.jin10.com/data-api/flash?category=' + self._param.flash_type,
69
+ headers=headers, data=json.dumps(params))
70
+ response = response.json()
71
+ for i in response['data']:
72
+ jin10_res.append({"content": i['data']['content']})
73
+ if self._param.type == "calendar":
74
+ params = {
75
+ 'category': self._param.calendar_type
76
+ }
77
+ response = requests.get(
78
+ url='https://open-data-api.jin10.com/data-api/calendar/' + self._param.calendar_datatype + '?category=' + self._param.calendar_type,
79
+ headers=headers, data=json.dumps(params))
80
+
81
+ response = response.json()
82
+ jin10_res.append({"content": pd.DataFrame(response['data']).to_markdown()})
83
+ if self._param.type == "symbols":
84
+ params = {
85
+ 'type': self._param.symbols_type
86
+ }
87
+ if self._param.symbols_datatype == "quotes":
88
+ params['codes'] = 'BTCUSD'
89
+ response = requests.get(
90
+ url='https://open-data-api.jin10.com/data-api/' + self._param.symbols_datatype + '?type=' + self._param.symbols_type,
91
+ headers=headers, data=json.dumps(params))
92
+ response = response.json()
93
+ if self._param.symbols_datatype == "symbols":
94
+ for i in response['data']:
95
+ i['Commodity Code'] = i['c']
96
+ i['Stock Exchange'] = i['e']
97
+ i['Commodity Name'] = i['n']
98
+ i['Commodity Type'] = i['t']
99
+ del i['c'], i['e'], i['n'], i['t']
100
+ if self._param.symbols_datatype == "quotes":
101
+ for i in response['data']:
102
+ i['Selling Price'] = i['a']
103
+ i['buying price'] = i['b']
104
+ i['commodity code'] = i['c']
105
+ i['Stock Exchange'] = i['e']
106
+ i['Highest Price'] = i['h']
107
+ i['Yesterday’s Closing Price'] = i['hc']
108
+ i['Lowest Price'] = i['l']
109
+ i['Opening Price'] = i['o']
110
+ i['Latest Price'] = i['p']
111
+ i['Market Quote Time'] = i['t']
112
+ del i['a'], i['b'], i['c'], i['e'], i['h'], i['hc'], i['l'], i['o'], i['p'], i['t']
113
+ jin10_res.append({"content": pd.DataFrame(response['data']).to_markdown()})
114
+ if self._param.type == "news":
115
+ params = {
116
+ 'contain': self._param.contain,
117
+ 'filter': self._param.filter
118
+ }
119
+ response = requests.get(
120
+ url='https://open-data-api.jin10.com/data-api/news',
121
+ headers=headers, data=json.dumps(params))
122
+ response = response.json()
123
+ jin10_res.append({"content": pd.DataFrame(response['data']).to_markdown()})
124
+ except Exception as e:
125
+ return Jin10.be_output("**ERROR**: " + str(e))
126
+
127
+ if not jin10_res:
128
+ return Jin10.be_output("")
129
+
130
+ return pd.DataFrame(jin10_res)