部署上线
概述
将财报分析 Agent 部署到生产环境,让用户可以通过 Web 或 API 访问。
部署方案
| 方案 | 优点 | 缺点 | 适用场景 |
|---|---|---|---|
| Streamlit | 快速、简单 | 性能一般 | 原型演示 |
| FastAPI | 高性能、标准 | 需前端 | API 服务 |
| Gradio | 易用、美观 | 定制性差 | AI 应用 |
Streamlit 部署
基础应用
python
import streamlit as st
import yfinance as yf
st.title("财报分析 Agent")
# 输入
symbol = st.text_input("输入股票代码", "AAPL")
if st.button("开始分析"):
with st.spinner("正在分析..."):
ticker = yf.Ticker(symbol)
info = ticker.info
# 显示基本信息
st.subheader(f"{info.get('longName')}")
col1, col2, col3 = st.columns(3)
col1.metric("市盈率", f"{info.get('trailingPE', 'N/A'):.2f}")
col2.metric("市净率", f"{info.get('priceToBook', 'N/A'):.2f}")
col3.metric("ROE", f"{info.get('returnOnEquity', 0)*100:.1f}%")运行命令
bash
streamlit run app.py --server.port 8501FastAPI 部署
API 服务
python
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI(title="财报分析 API")
class AnalysisRequest(BaseModel):
symbol: str
analysis_type: str = "full"
@app.post("/analyze")
async def analyze(request: AnalysisRequest):
# 执行分析
result = run_analysis(request.symbol)
return {"status": "success", "data": result}
@app.get("/health")
async def health():
return {"status": "healthy"}启动服务
bash
uvicorn main:app --host 0.0.0.0 --port 8000Docker 部署
Dockerfile
dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]构建运行
bash
docker build -t financial-agent .
docker run -p 8000:8000 financial-agent云平台部署
Vercel (Streamlit)
bash
# 安装 Vercel CLI
npm i -g vercel
# 部署
vercel --prodRailway
bash
# 连接 GitHub 仓库后自动部署
# 设置环境变量
OPENAI_API_KEY=your-key监控运维
日志配置
python
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[
logging.FileHandler("app.log"),
logging.StreamHandler()
]
)
logger = logging.getLogger(__name__)健康检查
python
@app.get("/health")
async def health_check():
return {
"status": "healthy",
"version": "1.0.0",
"timestamp": datetime.now().isoformat()
}安全配置
python
# API 密钥验证
from fastapi import Header, HTTPException
async def verify_api_key(x_api_key: str = Header(...)):
if x_api_key != os.getenv("API_KEY"):
raise HTTPException(status_code=401, detail="Invalid API key")
return x_api_key项目完成
恭喜!你已完成财报分析 Agent 的全部开发。
回顾:
- 需求分析 - 明确功能和技术选型
- 数据获取 - 接入财报 API
- 数据分析 - 计算财务指标
- AI 分析 - LLM 生成洞察
- 可视化 - 图表和报告
- 部署上线 - 生产环境部署