带MCP的人工智能助理API
基于FastAPI的AI助手,使用Pydantic AI与GPT-4o和MCP(模型上下文协议)服务器。
特性
- 🤖 AI助手 通过MCP访问安全检查系统数据
- 📚 Swagger 用户界面 用于交互式API文档(可用于生产)
- 🔌 MCP集成 用于实时数据访问
- 🐳 Docker支持 便于部署
- 🚀 快速API 用于高性能异步API
先决条件
- Python>=3.11
- 紫外线 包管理器
安装
- 安装所有依赖项:
uv sync环境变量
创建一个 .env 项目根目录中的文件包含以下内容:
# OpenAI API Key (required)
OPENAI_API_KEY=your_openai_api_key_here
# MCP Server URL
MCP_SERVER_URL=https://backend-pid-production.up.railway.app/mcp/sse运行项目
选项1:运行API服务器(生产版)
要运行FastAPI服务器:
uv run uvicorn api:app --reload --port 8000API将在以下网址提供:
- API根: http://localhost:8000
- Swagger 用户界面: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
- 健康检查: http://localhost:8000/health
选项2:运行Streamlit应用程序(开发/测试)
要运行Streamlit应用程序,请执行以下操作:
uv run streamlit run streamlit.py该应用程序将在 http://localhost:8501 默认情况下。
Docker部署
快速入门脚本(推荐!)
使用提供的启动脚本:
# Make executable (first time only)
chmod +x docker-start.sh
# Run
./docker-start.sh此脚本将:
- ✅ 检查Docker是否已安装并正在运行
- ✅ 验证
.env文件存在 - ✅ 构建Docker镜像
- ✅ 启动容器
- ✅ 等待API准备就绪
- ✅ 在浏览器中打开Swagger UI
手动Docker命令
- 塑造形象:
docker build -t pid-mcp-api .- 运行容器:
docker run -d \
-p 8000:8000 \
--env-file .env \
--name pid-mcp-api \
--restart unless-stopped \
pid-mcp-api- 访问API:
- Swagger 用户界面: http://localhost:8000/docs - API根: http://localhost:8000 - 健康检查: http://localhost:8000/health
- 查看日志:
docker logs -f pid-mcp-api- 停止并删除:
docker stop pid-mcp-api && docker rm pid-mcp-apiAPI使用示例
使用curl
curl -X POST "http://localhost:8000/ask" \
-H "Content-Type: application/json" \
-d '{
"question": "Какие есть департаменты?",
"model": "openai:gpt-4o"
}'使用Python
import requests
response = requests.post(
"http://localhost:8000/ask",
json={
"question": "Покажи статистику системы",
"model": "openai:gpt-4o"
}
)
result = response.json()
print(result["answer"])文档
- 看 医生.md 了解详细的Docker部署指南
- 看 MCP_INTEGRATION.md 有关MCP集成的详细信息
- 看 ENV_TEMPLATE.md 用于环境变量设置
