Skip to content

10.11 FinRobot - AI4Finance 多智能体金融分析平台

本节概述

在学习了 TradingAgent 和 PrimoAgent 之后,我们来看另一个重要的开源金融智能体项目——FinRobot。这是由 AI4Finance Foundation 开发的企业级多智能体平台,采用四层架构设计,支持多种 LLM 和丰富的金融数据源。

项目地址: https://github.com/AI4Finance-Foundation/FinRobot

论文地址: arXiv:2405.14767


项目定位

FinRobot 是一个超越 FinGPT 的 AI 智能体平台,专为金融应用精心设计。它整合了多种 AI 技术,不仅限于语言模型,旨在民主化金融 AI 技术,让专业分析师和普通用户都能使用强大的 AI 金融分析工具。

核心愿景

  • 降低门槛: 让非技术人员也能使用高级金融 AI
  • 开放生态: 开源平台,促进社区协作
  • 企业级: 支持生产环境部署
  • 可扩展: 插件式架构,易于添加新功能

四层架构设计

FinRobot 采用分层架构,每层专注特定职责:

FinRobot 生态系统

架构层次详解

text
┌─────────────────────────────────────────────────────────────────┐
│          Layer 1: Financial AI Agents Layer                      │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  Market Forecaster | Financial Analyst | Trade Strategist│    │
│  │  ─────────────────────────────────────────────────────── │    │
│  │  Financial Chain-of-Thought (CoT) Prompting              │    │
│  │  Complex Analysis Decomposition                          │    │
│  └─────────────────────────────────────────────────────────┘    │
├─────────────────────────────────────────────────────────────────┤
│          Layer 2: Financial LLMs Algorithms Layer                │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  Domain-Specific Model Configuration                     │    │
│  │  Task-Tailored Strategy Selection                        │    │
│  │  Global Market Analysis Adaptation                       │    │
│  └─────────────────────────────────────────────────────────┘    │
├─────────────────────────────────────────────────────────────────┤
│          Layer 3: LLMOps and DataOps Layer                       │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  Multi-source LLM Integration                            │    │
│  │  Model Training & Fine-tuning                            │    │
│  │  Task-Relevant Data Pipeline                             │    │
│  └─────────────────────────────────────────────────────────┘    │
├─────────────────────────────────────────────────────────────────┤
│          Layer 4: Multi-source LLM Foundation Models             │
│  ┌─────────────────────────────────────────────────────────┐    │
│  │  OpenAI GPT-4 | Claude | Llama | FinGPT | Ollama         │    │
│  │  Plug-and-Play Model Integration                         │    │
│  └─────────────────────────────────────────────────────────┘    │
└─────────────────────────────────────────────────────────────────┘
层级职责关键组件
Layer 1金融 AI 智能体CoT 推理、复杂分析分解
Layer 2金融 LLM 算法领域特定配置、策略选择
Layer 3LLMOps/DataOps多源集成、训练微调
Layer 4基础模型多 LLM 支持、插件式架构

智能体工作流: Perception-Brain-Action

智能体工作流

FinRobot 的智能体采用经典的感知-思考-行动三阶段工作流:

1. Perception (感知阶段)

职责: 捕获多模态金融数据

数据类型:

  • 市场数据流 (实时价格、成交量)
  • 新闻和公告 (SEC 文件、财报)
  • 经济指标 (GDP、利率、通胀)
  • 另类数据 (社交媒体、Reddit)

2. Brain (思考阶段)

职责: 使用 LLM 和 Financial CoT 处理数据

处理流程:

text
原始数据 → 数据预处理 → LLM 推理 → Financial CoT → 结构化指令

Financial Chain-of-Thought (CoT):

将复杂金融问题分解为逻辑步骤:

text
问题: "分析 Apple 的投资价值"

Step 1: 收集财务数据 (营收、利润、现金流)

Step 2: 计算估值指标 (P/E、P/B、DCF)

Step 3: 对比行业基准

Step 4: 评估风险因素

Step 5: 综合投资建议

3. Action (行动阶段)

职责: 执行指令,产生输出

输出类型:

  • 交易信号和执行
  • 投资组合调整
  • 研究报告生成
  • 风险预警

Smart Scheduler 智能调度器

智能调度器

Smart Scheduler 是 FinRobot 的任务编排核心,包含四个关键组件:

组件详解

组件职责实现方式
Director Agent任务分配主管分析任务需求,分派给合适智能体
Agent Registration智能体注册中心追踪可用智能体及其能力
Agent Adaptor智能体适配器定制智能体功能,匹配任务需求
Task Manager任务管理器维护微调后的智能体,管理执行队列

调度流程

text
用户请求

Director Agent 分析任务

查询 Agent Registration 获取可用智能体

Agent Adaptor 定制功能

Task Manager 分配执行

智能体执行任务

返回结果

项目结构

text
FinRobot/
├── finrobot/                    # 核心包
│   ├── agents/                  # 智能体实现
│   │   ├── agent_library.py     # 10种角色定义 (6KB)
│   │   ├── workflow.py          # 工作流编排 (15KB)
│   │   ├── prompts.py           # 提示词模板
│   │   └── utils.py             # 工具函数
│   │
│   ├── data_source/             # 数据源集成
│   │   ├── finnhub_utils.py     # Finnhub API (10.6KB)
│   │   ├── finnlp_utils.py      # FinNLP 工具 (14.3KB)
│   │   ├── fmp_utils.py         # Financial Modeling Prep (11.3KB)
│   │   ├── sec_utils.py         # SEC 文件 (7.6KB)
│   │   ├── yfinance_utils.py    # Yahoo Finance (4.3KB)
│   │   └── reddit_utils.py      # Reddit 数据 (4.1KB)
│   │
│   ├── functional/              # 功能模块
│   │   ├── analyzer.py          # 财务分析 (23KB) ⭐
│   │   ├── quantitative.py      # 量化分析 (7.3KB)
│   │   ├── charting.py          # 图表可视化 (8.7KB)
│   │   ├── reportlab.py         # PDF报告生成 (15.6KB)
│   │   ├── rag.py               # RAG 检索 (3.1KB)
│   │   └── ragquery.py          # RAG 查询 (9.6KB)
│   │
│   ├── toolkits.py              # 工具包
│   └── utils.py                 # 通用工具

├── tutorials_beginner/          # 入门教程
│   ├── agent_annual_report.ipynb
│   ├── agent_fingpt_forecaster.ipynb
│   ├── agent_rag_earnings_call_sec_filings.ipynb
│   └── ollama function call.ipynb

├── tutorials_advanced/          # 高级教程
├── configs/                     # 配置文件
├── experiments/                 # 实验代码
└── report/                      # 报告输出

十大智能体角色

FinRobot 预定义了10种专业角色:

技术角色 (4种)

角色职责
Software_Developer软件开发和系统集成
Programmer编程和代码实现
IT_Specialist技术支持和基础设施
AI_EngineerAI/ML 模型开发

分析角色 (2种)

角色职责
Data_Analyst数据分析和可视化
Statistician统计建模和假设检验

金融角色 (4种)

角色职责配备工具
Accountant财务报表分析-
Financial_Analyst财务建模和估值-
Market_Analyst市场数据分析Finnhub, yFinance
Expert_Investor投资策略和报告SEC, PDF生成, 图表

Expert_Investor 详解

这是功能最全面的角色:

python
{
    "name": "Expert_Investor",
    "profile": "具有精湛分析能力的投资专家...",
    "toolkits": [
        "get_sec_report",      # SEC报告获取
        "get_pdf",             # PDF生成
        "plot_chart",          # 图表绘制
        "analyze_report"       # 报告分析
    ]
}

数据源集成

FinRobot 支持6大数据源:

数据源对比

数据源数据类型代码文件
Finnhub实时行情、新闻、基本面finnhub_utils.py
Yahoo Finance历史价格、财务报表yfinance_utils.py
FMP综合财务指标fmp_utils.py
SEC Edgar监管文件 (10-K, 10-Q)sec_utils.py
FinNLP金融 NLP 处理finnlp_utils.py
Reddit另类数据、情绪reddit_utils.py

数据流程

text
┌─────────────────────────────────────────────────────────────────┐
│                         数据采集层                                │
│                                                                 │
│   ┌─────────┐  ┌─────────┐  ┌─────────┐  ┌─────────┐           │
│   │ Finnhub │  │ yFinance│  │   FMP   │  │   SEC   │           │
│   └────┬────┘  └────┬────┘  └────┬────┘  └────┬────┘           │
│        │            │            │            │                 │
│        └────────────┼────────────┼────────────┘                 │
│                     ↓                                           │
│              ┌─────────────┐                                    │
│              │  数据标准化   │                                    │
│              └──────┬──────┘                                    │
│                     ↓                                           │
│              ┌─────────────┐                                    │
│              │  智能体消费   │                                    │
│              └─────────────┘                                    │
└─────────────────────────────────────────────────────────────────┘

财务分析功能 (analyzer.py)

这是 FinRobot 的核心分析模块,包含丰富的财务分析函数:

报表分析函数

函数用途
analyze_income_stmt()收入趋势、成本控制、利润率
analyze_balance_sheet()资产结构、流动性、偿债能力
analyze_cash_flow()经营/投资/融资现金流
analyze_segment_stmt()业务分部表现

综合分析函数

函数用途
income_summarization()收入报表总结
get_risk_assessment()风险评估 (Top 3)
get_competitors_analysis()竞争对手对比
get_key_data()关键财务指标

使用示例

python
from finrobot.functional.analyzer import (
    analyze_income_stmt,
    get_risk_assessment,
    get_competitors_analysis
)

# 分析收入报表
income_analysis = analyze_income_stmt(
    ticker="AAPL",
    fiscal_year=2024
)

# 获取风险评估
risks = get_risk_assessment(
    ticker="AAPL",
    top_n=3,
    include_industry=True
)

# 竞争对手分析
competitors = get_competitors_analysis(
    ticker="AAPL",
    metrics=["ebitda_margin", "revenue_growth", "roic"]
)

三大核心智能体应用

1. Market Forecaster Agent (市场预测智能体)

市场预测演示

功能: 预测股票价格走势

输入:

  • 公司财务数据
  • 新闻和公告
  • 技术指标

输出:

  • 价格预测 (涨/跌/持平)
  • 置信度评分
  • 关键影响因素

2. Financial Analyst Agent (财务分析智能体)

财务分析演示

功能: 生成股权研究报告

输入:

  • 10-K 年度报告
  • 财务报表
  • 市场数据

输出:

  • 完整的 PDF 研究报告
  • 估值分析
  • 投资建议

3. Trade Strategist Agent (交易策略智能体)

功能: 开发多模态交易策略

输入:

  • 技术图表
  • 基本面数据
  • 情绪指标

输出:

  • 交易策略
  • 入场/出场点位
  • 风险管理建议

工作流实现

FinRobot 使用 AutoGen 框架实现智能体编排:

单智能体模式

python
class SingleAssistant:
    """单智能体对话模式"""

    def __init__(self, agent_config):
        self.user_proxy = UserProxyAgent(...)
        self.assistant = FinRobotAssistant(agent_config)

    def chat(self, message):
        # 用户代理发起对话
        self.user_proxy.initiate_chat(
            self.assistant,
            message=message
        )
        # 持续对话直到终止信号
        while not self.is_terminated():
            response = self.assistant.generate_reply()
            self.process_response(response)

多智能体模式

python
class MultiAssistant:
    """多智能体协作模式"""

    def __init__(self, agents_config):
        self.agents = [FinRobotAgent(c) for c in agents_config]
        self.group_chat = GroupChat(
            agents=self.agents,
            speaker_selection="custom"  # 自定义发言选择
        )

    def select_speaker(self, messages):
        """基于消息历史选择下一个发言者"""
        last_message = messages[-1]
        if last_message.has_tool_calls:
            return self.get_tool_executor()
        return self.get_next_agent(messages)

Leader-Based 模式

python
class MultiAssistantWithLeader:
    """带领导者的层级协作"""

    def __init__(self, leader_config, team_configs):
        self.leader = LeaderAgent(leader_config)
        self.team = [TeamAgent(c) for c in team_configs]

        # 建立嵌套对话连接
        for agent in self.team:
            agent.register_nested_chat(self.leader)

    def execute_task(self, task):
        # 领导者分配任务
        assignments = self.leader.assign_task(task)
        # 团队成员执行
        results = []
        for agent, sub_task in assignments:
            result = agent.execute(sub_task)
            results.append(result)
        # 领导者汇总
        return self.leader.summarize(results)

快速开始

环境配置

bash
# 创建 conda 环境
conda create --name finrobot python=3.10
conda activate finrobot

# 克隆仓库
git clone https://github.com/AI4Finance-Foundation/FinRobot.git
cd FinRobot

# 安装依赖
pip install -U finrobot
# 或开发模式
pip install -e .

API 密钥配置

OAI_CONFIG_LIST (OpenAI 配置):

json
[
    {
        "model": "gpt-4",
        "api_key": "sk-xxx"
    }
]

config_api_keys (数据源配置):

text
FINNHUB_API_KEY=xxx
FMP_API_KEY=xxx
SEC_API_KEY=xxx

运行教程

bash
# Jupyter 教程
cd tutorials_beginner
jupyter notebook agent_annual_report.ipynb

# 使用 Ollama 本地模型
jupyter notebook "ollama function call.ipynb"

三方对比: FinRobot vs PrimoAgent vs TradingAgent

架构对比

维度FinRobotPrimoAgentTradingAgent
架构类型四层分层架构顺序流水线多阶段循环
智能体数量10种角色4个15个节点
编排框架AutoGenLangGraphLangGraph
循环机制多智能体协作无循环6个循环
层级结构Leader-Based扁平阶段式

功能对比

text
┌──────────────────────────────────────────────────────────────────────┐
│                           功能矩阵                                     │
├──────────────────────────────────────────────────────────────────────┤
│                                                                      │
│   特性              FinRobot    PrimoAgent   TradingAgent            │
│   ───────────────────────────────────────────────────────────        │
│   交易信号生成        ✓           ✓            ✓                       │
│   研究报告生成        ✓ (PDF)     ✗            ✗                       │
│   10-K分析           ✓           ✗            ✗                       │
│   RAG问答            ✓           ✗            ✗                       │
│   多模态分析         ✓           ✗            ✗                       │
│   牛熊辩论           ✗           ✗            ✓                       │
│   风险三方讨论       ✗           ✗            ✓                       │
│   NLP情感分析        ✓           ✓ (7维)      ✓                       │
│   回测框架           ✗           ✓            ✗                       │
│   本地模型支持       ✓ (Ollama)  ✗            ✗                       │
│                                                                      │
└──────────────────────────────────────────────────────────────────────┘

数据源对比

数据源FinRobotPrimoAgentTradingAgent
Finnhub
yFinance
FMP
SEC Edgar
Reddit
FinNLP
社交媒体

技术栈对比

技术FinRobotPrimoAgentTradingAgent
核心框架AutoGenLangGraphLangGraph
LLM支持多模型OpenAI多模型
本地模型Ollama
报告生成ReportLabCSVMarkdown
可视化Matplotlib内置
RAG支持ChromaDB

适用场景对比

text
FinRobot 最适合:
├── 企业级金融分析系统
├── 需要生成专业研究报告
├── 10-K/10-Q 文件分析
├── 多数据源整合需求
└── 需要本地模型部署

PrimoAgent 最适合:
├── 快速原型开发
├── 学习LangGraph基础
├── 低延迟交易信号
├── 资源受限环境
└── 需要回测验证

TradingAgent 最适合:
├── 模拟真实交易公司流程
├── 需要多方验证的决策
├── 高价值交易决策
├── 需要可解释性
└── 学术研究和论文复现

学习曲线对比

方面FinRobotPrimoAgentTradingAgent
入门难度中等
文档质量优秀 (含教程)良好优秀 (含论文)
社区支持DiscordGitHubGitHub
代码复杂度中等
配置复杂度高 (多API)中等中等

性能对比

指标FinRobotPrimoAgentTradingAgent
单次分析时间60-120秒约30秒约160秒
API调用次数10-20次4-8次20+次
输出丰富度高 (PDF报告)中等 (CSV)高 (结构化)
可扩展性中等中等

本节小结

通过本节学习,你应该掌握:

  • FinRobot 的四层架构设计理念
  • Perception-Brain-Action 智能体工作流
  • Smart Scheduler 智能调度机制
  • 10种预定义智能体角色
  • 6大数据源集成方式
  • AutoGen 多智能体编排模式
  • 三大核心应用场景

核心收获:

  1. 分层架构的价值: 四层设计使系统高度模块化和可扩展
  2. 多模型支持: 从 GPT-4 到 Ollama 本地模型,灵活适应不同场景
  3. 企业级功能: PDF 报告生成、RAG 问答、10-K 分析等专业特性
  4. 与其他框架的差异: AutoGen vs LangGraph 的不同编排思路

上一节: 10.10 PrimoAgent

返回目录: 10.0 本章介绍

项目地址: FinRobot GitHub

论文地址: arXiv:2405.14767


最后更新: 2025年12月作者: Bryce Wang

基于 MIT 许可证发布。内容版权归作者所有。