ai-due-diligence-copilot
AI Due Diligence Copilot
AI Due Diligence Copilot 是一个端到端的财务文档分析系统,用于摄取公司申报文件(10-K / 10-Q)、提取结构化财务信息、检索支撑性证据,并使用最可靠的可用来源回答分析师的问题。
与许多完全依赖 LLM 的 AI 应用不同,本系统会将每个问题智能路由到以下任一来源:
PostgreSQL 中存储的结构化财务数据
通过检索增强生成(RAG)流水线检索到的申报文件证据
两者的组合
为了提高可信度,系统还支持使用基于 PyTorch 的 groundedness(依据性)分类器对生成的回答进行评估,以检查其中的主张是否由检索证据所支持。
为什么选择这个项目?
财务分析师经常需要满足以下条件的答案:
事实准确
可解释
可追溯到源文档
传统 AI LLM 应用一贯会出现幻觉备份,编造数字或提供没有依据的断言。
针对这一问题,本项目通过以下方式:
使用 SQL 进行定量推理
使用 RAG 进行定性文档理解
使用依据性评估来验证证据支持
支持可选的基于 Claude 的答案综合,同时不依赖任何付费 API 也能完整运行
实现细节与设计选择的深入说明,请参阅:
ARCHITECTURE.mdRelated MCP server: SEC-MCP
功能特性
文档摄取
上传财务申报文件(
.txt、.pdf、.md)自动进行文档清洗与分块
申报文件元数据跟踪
持久化存储于 PostgreSQL
结构化财务指标提取
目前提取并存储:
营业收入
营业利润率
净利润
研发费用(R&D Expense)
现金及现金等价物
这些年来同时存入 PostgreSQL,可直接用于定量分析。
智能查询路由
系统会自动判断一个问题应通过哪种方式回答:
结构化 SQL 检索
文档检索(RAG)
SQL + RAG
示例:
问题 | 路由 |
营业利润率发生了什么变化? | SQL |
公司面临哪些供应商风险? | RAG |
利润率如何变化以及为什么变化? | SQL + RAG |
MCP 工具集成
实现了模型上下文协议(MCP)工具:
公司查询(Company Lookup)
财务比率计算器(Financial Ratio Calculator)
文档搜索(Document Search)
依据性评估
一种轻量级的 PyTorch 分类器,用于判断生成的主张是否由检索到的证据支持。
输出包括:
依据性得分
主张级别的支持分类
置信度指标
评估框架
内置评估框架,指标包括:
路由准确率
检索相关性
依据性
延迟
交互式仪表盘
Web 界面,用于:
上传申报文件
问题解答
查看路由决策
查看工具调用情况
查看依据性得分
系统架构
Financial Filing
│
▼
Document Ingestion
│
▼
Chunking
│
▼
Metric Extraction + Vectorization
(TF-IDF + SVD)
│ │
▼ ▼
PostgreSQL Vector Store
▲ ▲
│ │
User Query ───► Query Router ───┘
│
┌──────────┴──────────┐
▼ ▼
SQL Financial Route RAG Retrieval
▼ ▼
Financial Metrics Evidence Search
└──────────┬──────────┘
▼
Answer Generation
▼
Groundedness Evaluation
▼
Final Response回答生成模式
1. 离线 / 本地模式(默认)
不需要任何 API 密钥。
系统使用以下方式回答问题:
PostgreSQL 财务数据
RAG 检索
抽取式回答生成
PyTorch 依据性评估
开发和测试均在此期间完成。
2. Claude 辅助模式(可选)
如果提供了 Anthropic API 密钥:
ANTHROPIC_API_KEY=your_api_key_here即可将检索到的证据视为 Claude,从而生成自然语言回答。
流程:
User Query
↓
Retrieval / SQL
↓
Claude
↓
Groundedness Evaluation
↓
Final ResponseClaude 仅用于答案。
系统不依赖 Claude 实现以下环节:
检索
查询路由
财务计算
依据性评分
若未配置 API 密钥,系统将自动回退到本地抽取式流水线。
技术栈
后端
技术 | 用途 |
FastAPI | API 框架 |
Uvicorn | ASGI 服务器 |
Pydantic | 数据验证 |
数据库
技术 | 用途 |
PostgreSQL | 主数据库 |
SQLAlchemy | ORM |
机器学习
技术 | 用途 |
PyTorch | Groundedness 分类器 |
Scikit-Learn | 检索流水线 |
NumPy | 数值操作 |
检索
组件 | 用途 |
TF-IDF | 文档向量化 |
Truncated SVD | 密集语义表示 |
Cosine Similarity | 检索效排名 |
AI 工具
组件 | 用途 |
MCP Tools | 结构化工具接入 |
Query Router | 路由选择 |
Groundedness Evaluator | 证据验证 |
项目结构
diligence-copilot/
│
├── app/
│ ├── db/
│ ├── ingestion/
│ ├── ml/
│ ├── rag/
│ ├── mcp_tools/
│ └── main.py
│
├── data/
│
├── scripts/
│ └── setup_postgres.sql
│
├── tests/
│
├── Dockerfile
├── docker-compose.yml
├── requirements.txt
├── ARCHITECTURE.md
└── README.md安装
前提
Python 3.11+
PostgreSQL 16+(已在 PostgreSQL 17.11 上开展测试)
Git
1. 克隆仓库
git clone <repository-url>
cd diligence-copilot2. 创建虚拟环境
环境:
Windows
python -m venv venv
.\venv\Scripts\Activate.ps1Linux / macOS
python -m venv venv
source venv/bin/activate3. 安装依赖
安装 CPU 版 PyTorch:
pip install torch --index-url https://download.pytorch.org/whl/cpu安装项目依赖:
pip install -r requirements.txt4. 配置 PostgreSQL
执行:
psql -U postgres -f scripts/setup_postgres.sql这将创建:
diligence_copilot数据库diligence_app用户所需的权限
5. 构建依据性数据集
python -m app.ml.build_dataset6. 训练依据性分类器
python -m app.ml.groundedness7. 启动应用
uvicorn app.main:app --reload应用
http://localhost:8000API 文档:
http://localhost:8000/docsDocker
docker-compose up --build验证演练
创建测试申报文件
Total revenue for fiscal year 2024 was $500 million.
Operating margin for fiscal year 2024 was 12.5%, compared to 10.1% in fiscal year 2023.
The company faces significant risk from a single supplier located in Vietnam.上传申报文件
设置:
Ticker: TEST
Company: Test Company
Fiscal Period: FY2024
输出示例:
Done: 1 chunks, 3 financial metrics extracted.测试财务推理
问题:
What was the change in operating margin?示例输出:
operating_margin moved from 10.1 (FY2023)
to 12.5 (FY2024), a change of 23.76%.路由:
SQL工具:
financial_ratio_calculator测试检索
问题:
What supplier risks does the company face?示例输出:
The company faces significant risk from a single supplier located in Vietnam.注意:对于很少、可以划分到单个分块的申报文件,返回结果可能是整个分块,而不是单个句子。
路由:
RAG运行评估
python -m app.eval.run_eval结果
已验证以下端到端流程:
文档摄取
财务指标提取
PostgreSQL 持久化
查询路由
基于 SQL 的财务推理
基于检索的推理
依据性评估
FastAPI 部署
采样评估结果
指标 | 值 |
路由准确率 | 1.00 |
平均检索相关性 | 0.67 |
平均延迟 | ~30ms |
Groundedness 分类器
指标 | 值 |
准确率 | 0.70 – 0.90 |
召回率 | 0.75 – 1.00 |
结果略有差别,是因为评估数据集特意做得很小。
局限性
Groundedness 分类器仅用 38 条标注数据训练
财务指标提取目前使用基于规则的模式
检索使用 TF-IDF + SVD,而非 Transformer 嵌入
向量索引在内存中,并针对演示规模进行优化
Claude 集成是可选功能,核心功能不需要依赖它
未来改进方向
基于 Transformer 的嵌入
混合搜索(关键词 + 向量)
更大规模的 Groundedness 数据集
基于 LLM 的提取回退
多步 Agent 工作流
pgvector 集成
多文档对比
手动生成分析师报告
This server cannot be deployed
Maintenance
Related MCP Connectors
Financial data and research MCP for US/CN/JP equities: filings, statements, ownership, signals.
Paid bilingual MCP tools for evidence-linked SEC filing risk and finance-author track records.
MCP server for VC pitch-deck scoring, thesis-fit matching, and deal-flow management.
Financial data MCP for market, company, news, macro, and US Congress research.
Related MCP Servers
- AlicenseNot gradedqualityDmaintenanceMCP server for SEC EDGAR that provides real-time access to filings, financial statements, and full-text search across all EDGAR documents.Apache 2.0
- AlicenseNot gradedqualityBmaintenanceMCP server for analyzing SEC filings (10-K, 10-Q, 8-K) with industry-aware financial extraction and BERT-based NLP.1MIT
- AlicenseNot gradedqualityDmaintenanceAn MCP server that provides deterministic finance tools for SEC filing analysis, enabling LLMs to compute financial ratios, fetch filings, and perform equity research without hallucinated numbers.MIT
- AlicenseAqualityBmaintenanceAn MCP server that wraps SEC EDGAR APIs to provide company financial data, screening metrics, and disclosure signals for investment diligence, with every figure traced to its source filing.8MIT