使用说明
支出跟踪AI
使用FastAPI、LangGraph、MCP(模型上下文协议)和Streamlit构建的现代费用跟踪应用程序。
🏗️ 建筑
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Streamlit UI │ │ LangGraph AI │ │ FastAPI MCP │
│ │◄──►│ Agent │◄──►│ Server │
│ User Interface │ │ │ │ │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │ │
│ │ │
▼ ▼ ▼
┌─────────────────────────────────────────────────────────────────┐
│ AWS RDS MySQL Database │
└─────────────────────────────────────────────────────────────────┘
🚀 快速开始
1.安装依赖项
pip install -e .
2.设置环境变量
创建一个 .env 文件:
OPENAI_API_KEY=your_openai_api_key_here
MYSQL_HOST=expense-tracker-db.cx0aeuk0edcm.eu-north-1.rds.amazonaws.com
MYSQL_PORT=3306
MYSQL_USER=expense_tracker
MYSQL_PASSWORD=expense-tracker-db
MYSQL_DATABASE=expense_tracker
3.启动服务
# Start both FastAPI and Streamlit
python main.py
# Start only FastAPI
python main.py --mode api
# Start only Streamlit
python main.py --mode frontend
🔧 服务
FastAPI MCP服务器(端口8000)
- 主要API: http://localhost:8000
- MCP端点: http://localhost:8000/mcp
- API 文档: http://localhost:8000/docs
Streamlit应用程序(端口8501)
- 统一资源定位符: http://localhost:8501
- 特性:与AI代理的聊天界面
🤖 AI代理功能
LangGraph代理可以帮助您:
- 增加费用:“今天增加25美元的午餐费用”
- 查看数据:“显示上个月的所有费用”
- 摘要:“按类别给我一个细分”
- 搜索:“查找所有超过50美元的食品费用”
📁 项目结构
mcp-demo/
├── app/ # Main application
│ ├── api/ # API layer
│ │ ├── v1/endpoints/ # API endpoints
│ │ └── dependencies.py # FastAPI dependencies
│ ├── core/ # Core configuration
│ │ ├── config.py # Settings
│ │ └── database.py # Database connection
│ ├── models/ # Database models
│ ├── schemas/ # Pydantic schemas
│ ├── services/ # Business logic
│ ├── agents/ # AI agents
│ └── app.py # FastAPI app
├── frontend/
│ └── streamlit_app.py # Streamlit UI
├── tests/ # Test suite
├── docker/ # Docker configurations
├── main.py # Main entry point
└── pyproject.toml # Dependencies
🧪 测试
pytest tests/
🐳 码头工人
# Build image
docker build -f docker/Dockerfile -t expense-tracker .
# Run with docker-compose
docker-compose -f docker/docker-compose.yml up
📊 数据库
该应用程序使用具有以下架构的AWS RDS MySQL:
CREATE TABLE expenses (
id INT PRIMARY KEY AUTO_INCREMENT,
date DATE NOT NULL,
amount DECIMAL(10,2) NOT NULL,
category VARCHAR(100) NOT NULL,
subcategory VARCHAR(100),
note TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
🔒 安全
- 设置AWS RDS安全组以允许端口3306
- 对敏感数据使用环境变量
- 验证所有用户输入
📄 许可证
MIT许可证
docker exec -it expense_tracker_mysql mysql -u root -p
先进的后端技术
1=>Priority Implementation Order
2=>Observability (Logging, Metrics, Tracing) - Critical for production
3=>Security (Authentication, Input validation) - Security first
4=>Background Tasks - For scalability
5=>Event-Driven Architecture - For loose coupling
6=>Advanced Caching - For performance
7=>Database Optimization - For efficiency
8=>Circuit breaker for External sources.
9=>Background Task Processing
高级缓存技术
# Multi-level caching
class CacheStrategy:
# L1: In-memory cache
# L2: Redis cache
# L3: Database
async def get_with_fallback(self, key: str):
# Try L1 -> L2 -> L3
pass
# Cache warming
@celery_app.task
async def warm_cache():
# Pre-populate frequently accessed data
pass
# Cache invalidation patterns
class CacheInvalidator:
async def invalidate_pattern(self, pattern: str):
# Smart invalidation based on patterns
pass