哥伦比亚投资组合聚合器
基于人工智能的投资组合管理系统,使用LangChain和OpenRouter从多个哥伦比亚平台(Lulo、DollarApp、Trii等)的谷歌表格中读取和分析投资数据。
概述
该项目为管理和分析多个哥伦比亚平台上的个人投资组合提供了一个智能助手。它结合了谷歌表格,用于数据存储、实时汇率和通过LangChain代理进行的人工智能分析。
特性
核心功能
- 从谷歌表格中读取投资组合头寸
- 支持多种平台(Lulo、DollarApp、Trii等)
- 多币种支持(COP,美元)
- 实时USD/COP汇率整合
- 自动哥伦比亚数字格式处理(逗号以千计)
- 使用Google进行安全的OAuth身份验证
AI代理功能
- 自然语言作品集查询
- 使用ReAct模式进行智能刀具选择
- 投资组合总结和分析
- 按平台、货币和资产类型划分的分配明细
- 位置过滤和搜索
- 使用yfinance实时跟踪ETF价格
- 日常性能监控
- 预算跟踪和成本管理
使用Firecrawl进行网页抓取
- Portafolio.co的哥伦比亚市场分析
- 来自可靠来源的金融文章研究
- 支持的来源:
- 哥伦比亚:larepublica.co,portfolio.co,eltiempo.com - 全球:彭博社、路透社、finance.yahoo.com - ETF研究:晨星网,seekingalpha.com
预算管理
- 可配置的支出限额(总额和每日)
- JSON格式的自动使用跟踪
- 实时成本监控
- 使用率为50%、80%和95%时发出警告警报
- 详细的使用历史和分析
建筑
技术栈
- Python 3.11+
- 紫外线 -快速Python包管理器
- LangChain -代理编排
create_react_agent和AgentExecutor - 开放路由 -LLM API访问(支持多个模型)
- 派丹蒂克 -数据验证和建模
- 谷歌网页API -数据来源
- 谷歌OAuth 2.0 -安全身份验证
- 雅虎财经 -实时市场数据(免费,无API密钥)
- 火爬 -金融新闻和市场数据的网络抓取
项目结构
langchain-mcp-test/
├── models/
│ └── portfolio.py # Pydantic models for Position and Portfolio
├── mcp_servers/
│ └── portfolio_sheets/
│ ├── sheets_client.py # Google Sheets API client
│ └── __init__.py
├── agent/
│ ├── llm_client.py # LangChain + OpenRouter setup
│ ├── tools.py # Portfolio analysis tools
│ ├── agent.py # Main agent orchestrator
│ └── usage_tracker.py # Budget tracking system
├── utils/
│ ├── exchange_rates.py # Real-time exchange rate utilities
│ ├── market_data.py # ETF price tracking with yfinance
│ ├── firecrawl_client.py # Web scraping for financial sources
│ ├── mcp_integration.py # External MCP integration (placeholder)
│ └── __init__.py
├── test/
│ ├── test_models.py # Model tests
│ ├── test_sheets_client.py # Google Sheets integration tests
│ ├── test_agent.py # Agent functionality tests
│ └── test_market_data.py # ETF price integration tests
├── cli.py # Command-line interface
├── config.py # Configuration management
├── .env # Environment variables (not in git)
├── usage.json # API usage tracking (not in git)
└── requirements.txt # Python dependencies代理架构
User Question (CLI)
↓
PortfolioAgent (ReAct Pattern with create_react_agent + AgentExecutor)
↓
LLM Decision (OpenRouter - gpt-3.5-turbo)
↓
Tool Selection:
- get_portfolio_summary → Google Sheets data
- get_positions → Google Sheets data
- get_allocation_by_platform → Google Sheets data
- get_allocation_by_currency → Google Sheets data
- get_allocation_by_asset_type → Google Sheets data
- get_etf_prices → yfinance API
- get_market_analysis → Firecrawl (portafolio.co)
- research_article → Firecrawl (trusted sources)
- research_market → Web search (placeholder)
↓
Tool Execution → Data Sources
↓
LLM Analysis & Response
↓
Usage Tracking (JSON)数据模型
位置 -代表单一投资:
symbol资产名称(例如“动态股票”)platform:平台名称(例如“Lulo”、“Trii”、“Dolar App”)currency:货币代码(“COP”或“USD”)value:原始货币的总头寸价值asset_type:资产类型(可选,默认为“基金”)
投资组合 -用分析方法收集职位:
positions:位置对象列表- 总值、分配、过滤方法
设置
先决条件
- Python 3.11或更高版本
- 启用了Sheets API的Google Cloud帐户
- OpenRouter API帐户
- Firecrawl API密钥(可选,用于市场分析)
1.克隆和安装依赖项
# Navigate to project directory
cd "/path/to/langchain-mcp-test"
# Install uv (if not already installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Install dependencies with uv
uv sync
# Or with pip (alternative)
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt2.谷歌云设置
- 首选 谷歌云控制台
- 创建新项目或选择现有项目
- 启用 谷歌网页API
- 创建OAuth 2.0凭据:
- 应用程序类型: 桌面应用程序 - 下载JSON凭据
- 配置OAuth同意屏幕:
- 将您的电子邮件添加为测试用户 - 设置范围: ../auth/spreadsheets.readonly
3.OpenRouter设置
4.防火墙设置(可选)
- 首选 火爬
- 创建账户
- 生成API密钥
- 添加到您的
.env文件
5.环境配置
复制 .env.example 到 .env 并填写您的值:
# Google OAuth Credentials
GOOGLE_CLIENT_ID=your_client_id_here
GOOGLE_CLIENT_SECRET=your_client_secret_here
# Google Sheet ID (from sheet URL)
PORTFOLIO_SHEET_ID=your_sheet_id_here
# OpenRouter Configuration
OPENROUTER_API_KEY=sk-or-v1-your-api-key-here
LLM_MODEL=openai/gpt-3.5-turbo
# Budget Limits
BUDGET_LIMIT=5.0
DAILY_LIMIT=0.25
# Firecrawl (for web scraping - optional)
FIRECRAWL_API_KEY=your_firecrawl_api_key_here6.谷歌表格格式
你的谷歌表格应该按顺序(A-D)列出这些列:
| A:资产 | B:平台 | C:货币 | D:原值 |
|---|---|---|---|
| 资产名称1 | 平台1 | COP | 100000 |
| 资产名称2 | 平台2 | COP | 500000 |
| 资产名称3 | 平台3 | 美元 | 150.50 |
重要提示:
- A列:资产名称/符号
- B栏:平台名称
- C栏:货币(COP或美元)
- D列:总值(可包含逗号千位分隔符)
- 其他列(E、F、G等)将被忽略
用法
交互模式(推荐)
与代理开始对话会话:
uv run cli.py
# Or with activated venv:
python cli.py示例问题
投资组合查询:
- “显示我的投资组合”
- “我的平台分配是什么?”
- “我在鲁洛有多少钱?”
- “显示我的美元头寸”
- “我的货币敞口是多少?”
市场数据:
- “我的ETF今天表现如何?”
- “我的ETF目前的价格是多少?”
市场分析(需要Firecrawl):
- “给我一份市场分析”
- “目前哥伦比亚市场形势如何?”
文章研究(需要Firecrawl):
- “分析这篇文章:https://www.portafolio.co/economia/..."
CLI选项
# Interactive mode
uv run cli.py
# Single question
uv run cli.py -q "Show my portfolio"
# Check budget status
uv run cli.py --status
# View usage history
uv run cli.py --history
uv run cli.py --history --days 30可用工具
| 工具 | 说明 | 数据源 |
|---|---|---|
get_portfolio_summary | 总体投资组合摘要 | 谷歌表格 |
get_positions | 列出职位(可选按平台筛选) | 谷歌表格 |
get_allocation_by_platform | 按平台分配百分比 | 谷歌表格 |
get_allocation_by_currency | 按货币分配百分比 | 谷歌表格 |
get_allocation_by_asset_type | 按资产类型划分的分配百分比 | 谷歌表格 |
get_etf_prices | 实时ETF价格和损益融资 | |
get_market_analysis | 哥伦比亚市场指标 | Firecrawl |
research_article | 抓取金融文章 | Firecrawl |
research_market | 市场研究(占位符) | - |
许可证
MIT许可证
