You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. class WenCaiParam(ComponentParamBase):
  21. """
  22. Define the WenCai component parameters.
  23. """
  24. def __init__(self):
  25. super().__init__()
  26. self.top_n = 10
  27. self.query_type = "stock"
  28. def check(self):
  29. self.check_positive_integer(self.top_n, "Top N")
  30. self.check_valid_value(self.query_type, "Query type",
  31. ['stock', 'zhishu', 'fund', 'hkstock', 'usstock', 'threeboard', 'conbond', 'insurance',
  32. 'futures', 'lccp',
  33. 'foreign_exchange'])
  34. class WenCai(ComponentBase, ABC):
  35. component_name = "WenCai"
  36. def _run(self, history, **kwargs):
  37. ans = self.get_input()
  38. ans = ",".join(ans["content"]) if "content" in ans else ""
  39. if not ans:
  40. return WenCai.be_output("")
  41. try:
  42. wencai_res = []
  43. res = pywencai.get(query=ans, query_type=self._param.query_type, perpage=self._param.top_n)
  44. if isinstance(res, pd.DataFrame):
  45. wencai_res.append({"content": res.to_markdown()})
  46. if isinstance(res, dict):
  47. for item in res.items():
  48. if isinstance(item[1], list):
  49. wencai_res.append({"content": item[0] + "\n" + pd.DataFrame(item[1]).to_markdown()})
  50. continue
  51. if isinstance(item[1], str):
  52. wencai_res.append({"content": item[0] + "\n" + item[1]})
  53. continue
  54. if isinstance(item[1], dict):
  55. if "meta" in item[1].keys():
  56. continue
  57. wencai_res.append({"content": pd.DataFrame.from_dict(item[1], orient='index').to_markdown()})
  58. continue
  59. if isinstance(item[1], pd.DataFrame):
  60. if "image_url" in item[1].columns:
  61. continue
  62. wencai_res.append({"content": item[1].to_markdown()})
  63. continue
  64. wencai_res.append({"content": item[0] + "\n" + str(item[1])})
  65. except Exception as e:
  66. return WenCai.be_output("**ERROR**: " + str(e))
  67. if not wencai_res:
  68. return WenCai.be_output("")
  69. return pd.DataFrame(wencai_res)