Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Environment:24mlight A Share investment Agent Python 3 11

From Leeroopedia
Knowledge Sources
Domains Infrastructure, Finance, LLM_Infrastructure
Last Updated 2026-10-10 14:00 GMT

Overview

Python 3.9+ runtime with LangGraph 0.2.56, AkShare/BaoStock financial data sources, Gemini/OpenAI-compatible LLM APIs, Tavily news search, and SQLite caching for A-Share investment analysis.

Description

This environment provides a standard CPU-based Python runtime for the A_Share_investment_Agent multi-agent investment analysis system. It requires Python >= 3.9 (3.11 recommended) and includes the full LangChain/LangGraph stack for agent orchestration, AkShare and BaoStock for Chinese A-Share market data, Google Gemini or any OpenAI-compatible API for LLM inference, and Tavily API for news search. A SQLite database provides persistent caching of market data, financial indicators, and news.

The system uses python-dotenv to load credentials from a .env file at the project root. The LLMClientFactory auto-detects the LLM provider: if OPENAI_COMPATIBLE_API_KEY, OPENAI_COMPATIBLE_BASE_URL, and OPENAI_COMPATIBLE_MODEL are all set, it uses the OpenAI-compatible endpoint; otherwise it defaults to Google Gemini.

Usage

Use this environment for all workflows in the A_Share_investment_Agent system: Stock Analysis, Backtesting, News Sentiment Pipeline, Debate Room, and Backend API. Every agent, tool, and service in the repository requires this environment.

System Requirements

Category Requirement Notes
OS Linux / macOS / Windows Windows requires UTF-8 stdout reconfiguration (handled in code)
Hardware CPU (no GPU required) Network access to Gemini/OpenAI/Tavily/AkShare APIs
Disk 500MB+ SQLite cache grows with usage; stored in data/market_data_cache.db
Python >= 3.9 (3.11 recommended) requires-python = ">=3.9" in pyproject.toml
Network Internet access Required for AkShare, BaoStock, Gemini, Tavily APIs

Dependencies

System Packages

  • python3 >= 3.9
  • sqlite3 (bundled with Python)
  • playwright browser binaries (playwright install)

Python Packages

  • langchain == 0.3.0
  • langchain-openai == 0.2.11
  • langchain-core >= 0.3.29, < 0.4.0
  • langgraph == 0.2.56
  • pandas >= 2.1.0, < 3.0.0
  • numpy >= 1.24.0, < 2.0.0
  • python-dotenv == 1.0.0
  • matplotlib >= 3.9.2, < 4.0.0
  • akshare >= 1.11.22, < 2.0.0
  • baostock >= 0.8.9, < 0.9.0
  • requests >= 2.31.0, < 3.0.0
  • beautifulsoup4 >= 4.12.3, < 5.0.0
  • openai >= 1.12.0, < 2.0.0
  • google-generativeai >= 0.3.0, < 0.4.0
  • google-genai >= 0.6.0, < 0.7.0
  • backoff >= 2.2.1, < 3.0.0
  • uvicorn >= 0.34.0, < 0.35.0
  • fastapi >= 0.115.12, < 0.116.0
  • playwright >= 1.52.0, < 2.0.0
  • yfinance >= 0.2.51, < 0.3.0

Credentials

The following environment variables must be set in .env:

Required:

  • GEMINI_API_KEY: Google Gemini API token (required if OpenAI-compatible not configured). Obtain from Google AI Studio.
  • GEMINI_MODEL: Gemini model name (optional, defaults to gemini-1.5-flash).

Optional (OpenAI-compatible alternative):

  • OPENAI_COMPATIBLE_API_KEY: API key for OpenAI-compatible endpoint.
  • OPENAI_COMPATIBLE_BASE_URL: Base URL for OpenAI-compatible endpoint.
  • OPENAI_COMPATIBLE_MODEL: Model name for OpenAI-compatible endpoint.

Optional (News search):

  • TAVILY_API_KEY: Single Tavily API key for news search.
  • TAVILY_API_KEYS: Multiple Tavily API keys (comma/semicolon separated) for key rotation.

Optional (Proxy configuration):

  • AKSHARE_PROXY_LIST: Comma/semicolon separated proxy URLs (default: direct).
  • AKSHARE_PROXY_FORCE_DIRECT: Force direct connection, bypass all proxies (default: false).
  • AKSHARE_PROXY_ALLOW_DIRECT: Allow direct connection as fallback (default: true).
  • AKSHARE_PROXY_MAX_ATTEMPTS: Max retry attempts (default: 3).
  • AKSHARE_PROXY_BASE_DELAY: Base backoff delay in seconds (default: 1).
  • AKSHARE_PROXY_MAX_DELAY: Max backoff delay in seconds (default: 15).
  • AKSHARE_PROXY_JITTER: Jitter in seconds added to backoff (default: 0.5).

Optional (Cache and news limits):

  • MARKET_CACHE_DB_PATH: SQLite cache database path (default: data/market_data_cache.db).
  • NEWS_QUERY_MODE: Query building mode, rule or llm (default: rule).
  • NEWS_MAX_NEWS: Global news fetch limit (default: 100, max: 100).
  • TAVILY_MAX_NEWS: Tavily per-request limit (default: 20, max: 20).

Quick Install

# Install all required packages
pip install langchain==0.3.0 langchain-openai==0.2.11 langgraph==0.2.56 \
    langchain-core>=0.3.29 pandas>=2.1.0 numpy>=1.24.0 python-dotenv==1.0.0 \
    matplotlib>=3.9.2 akshare>=1.11.22 baostock>=0.8.9 requests>=2.31.0 \
    beautifulsoup4>=4.12.3 openai>=1.12.0 google-generativeai>=0.3.0 \
    google-genai>=0.6.0 backoff>=2.2.1 uvicorn>=0.34.0 fastapi>=0.115.12 \
    playwright>=1.52.0 yfinance>=0.2.51

# Install Playwright browser binaries (if needed)
playwright install

# Copy environment template
cp .env.example .env
# Edit .env and fill in your API keys

Code Evidence

Python version requirement from pyproject.toml:6:

requires-python = ">=3.9"

Gemini API key validation from src/tools/openrouter_config.py:42-47:

api_key = os.getenv("GEMINI_API_KEY")
model = os.getenv("GEMINI_MODEL")

if not api_key:
    logger.error(f"{ERROR_ICON} 未找到 GEMINI_API_KEY 环境变量")
    raise ValueError("GEMINI_API_KEY not found in environment variables")
if not model:
    model = "gemini-1.5-flash"

LLM provider auto-detection from src/utils/llm_clients.py:260-268:

if client_type == "auto":
    if (kwargs.get("api_key") and kwargs.get("base_url") and kwargs.get("model")) or \
       (os.getenv("OPENAI_COMPATIBLE_API_KEY") and os.getenv("OPENAI_COMPATIBLE_BASE_URL") and os.getenv("OPENAI_COMPATIBLE_MODEL")):
        client_type = "openai_compatible"
    else:
        client_type = "gemini"

OpenAI-compatible credential validation from src/utils/llm_clients.py:146-163:

self.api_key = api_key or os.getenv("OPENAI_COMPATIBLE_API_KEY")
self.base_url = base_url or os.getenv("OPENAI_COMPATIBLE_BASE_URL")
self.model = model or os.getenv("OPENAI_COMPATIBLE_MODEL")

if not self.api_key:
    raise ValueError("OPENAI_COMPATIBLE_API_KEY not found in environment variables")
if not self.base_url:
    raise ValueError("OPENAI_COMPATIBLE_BASE_URL not found in environment variables")
if not self.model:
    raise ValueError("OPENAI_COMPATIBLE_MODEL not found in environment variables")

Tavily API key loading from src/crawler/news_search/tavily_impl.py:56-63:

env_key = os.getenv("TAVILY_API_KEY", "").strip()
if env_key:
    self._keys.append(env_key)

env_keys = os.getenv("TAVILY_API_KEYS", "").strip()
if env_keys:
    keys_from_env = [k for k in re.split(r"[;,\s]+", env_keys) if k]
    self._keys.extend(keys_from_env)

Common Errors

Error Message Cause Solution
GEMINI_API_KEY not found in environment variables Gemini API key not set in .env Add GEMINI_API_KEY=your-key to .env file
OPENAI_COMPATIBLE_API_KEY not found OpenAI-compatible config incomplete Set all three: OPENAI_COMPATIBLE_API_KEY, OPENAI_COMPATIBLE_BASE_URL, OPENAI_COMPATIBLE_MODEL
Gemini API 地理位置限制错误 Gemini API geo-restricted (non-US IP) Use a US-node VPN, or switch to OpenAI-compatible provider
AFC is enabled Gemini API rate limit triggered Retry logic handles this automatically; reduce request frequency
未找到环境变量文件 .env file missing Run cp .env.example .env and fill in keys
AkShare RemoteDisconnected / ProxyError Network instability or proxy issues Configure AKSHARE_PROXY_LIST or use SQLite cache to reduce API calls
[Tavily] 未配置可用 API Key Tavily API key not set Add TAVILY_API_KEY=your-key to .env (optional, news search degrades gracefully)

Compatibility Notes

  • Windows: Code includes sys.stdout.reconfigure(encoding="utf-8") to handle emoji/Chinese output; matplotlib uses Microsoft YaHei font on Windows.
  • macOS/Linux: Fully supported; default development environment.
  • Gemini geo-restriction: Gemini API may be unavailable outside US; use VPN or OpenAI-compatible provider as alternative.
  • AkShare rate limits: AkShare/BaoStock may throttle or block frequent requests; proxy rotation and SQLite caching mitigate this.
  • Tavily optional: If no Tavily API key is configured, news search returns cached results only; the system degrades gracefully.
  • PyPI mirror: pyproject.toml configures Huawei Cloud mirror (repo.huaweicloud.com) as supplemental source for users in China.

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment