wencai.py

Overview

The wencai.py file implements an interface to interact with WenCai (iwencai), a Chinese financial data and stock selection platform powered by AI technologies. The WenCai platform provides comprehensive and timely financial information, including news, announcements, research reports, and quantitative stock/fund selection tools. This file encapsulates the parameters, invocation logic, and response processing required to query WenCai's services using the third-party pywencai Python package.

Key functionalities include:


Classes and Functions

WenCaiParam

class WenCaiParam(ToolParamBase):

Description

WenCaiParam encapsulates the configuration parameters for querying the WenCai platform. It extends the generic ToolParamBase class, adding WenCai-specific parameters and validation.

Attributes

Methods

Usage Example

param = WenCaiParam()
param.top_n = 5
param.query_type = "fund"
param.check()  # Validates parameters, raises if invalid
input_form = param.get_input_form()
print(input_form)

WenCai

class WenCai(ToolBase, ABC):

Description

WenCai is the main tool class that implements the logic to query the WenCai platform and process its results. It inherits from ToolBase and Python's ABC (abstract base class), integrating with the agent framework.

Class Attributes

Methods

Usage Example

wencai_tool = WenCai()
wencai_tool._param = WenCaiParam()
wencai_tool._param.top_n = 5
wencai_tool._param.query_type = "stock"
result = wencai_tool._invoke(query="AAPL stock price")
print(result)  # Markdown formatted financial data report

Important Implementation Details


Integration with the Larger System


Visual Diagram

classDiagram
    class WenCaiParam {
        +meta: ToolMeta
        +top_n: int
        +query_type: str
        +__init__()
        +check()
        +get_input_form() dict
    }

    class WenCai {
        +component_name: str
        +_invoke(kwargs) str
        +thoughts() str
    }

    WenCaiParam <|-- WenCai : uses parameter object
    WenCai ..|> ToolBase
    WenCai ..|> ABC

Summary

The wencai.py file provides a robust and well-structured integration point with the WenCai financial platform. It cleanly separates parameter configuration from execution logic, handles diverse response formats gracefully, and ensures resilience through retries and timeouts. Its design supports seamless embedding into larger AI agents or financial analytics systems, enabling users to query rich live financial data and receive neatly formatted analysis reports.