# Knowledge Agent API 基于 FastAPI 的智能内容识别和结构化处理服务。 ## 🚀 快速开始 ### 1. 安装依赖 ```bash pip install -r requirements.txt ``` ### 2. 启动服务 #### 方式一:使用启动脚本(推荐) ```bash ./start_service.sh ``` #### 方式二:直接运行 ```bash python3 agent.py ``` #### 方式三:使用 uvicorn ```bash uvicorn agent:app --host 0.0.0.0 --port 8080 --reload ``` ### 3. 访问服务 - **服务地址**: http://localhost:8080 - **API文档**: http://localhost:8080/docs - **健康检查**: http://localhost:8080/health ## 📚 API 接口 ### POST /parse 同步解析内容处理 **请求体**: ```json { "requestId": "your_request_id" } ``` **响应**: ```json { "requestId": "your_request_id", "processed": 5, "success": 4, "details": [ { "index": 1, "dbInserted": true, "identifyError": null, "status": "success" } ] } ``` ### POST /parse/async 异步解析内容处理(后台任务) **请求体**: ```json { "requestId": "your_request_id" } ``` **响应**: ```json { "requestId": "your_request_id", "status": "processing", "message": "任务已提交到后台处理" } ``` ### GET /health 健康检查接口 **响应**: ```json { "status": "healthy", "timestamp": 1703123456.789 } ``` ## 🔧 配置 ### 环境变量 - `GEMINI_API_KEY`: Gemini API 密钥(必需) ### 数据库配置 在 `utils/mysql_db.py` 中配置数据库连接信息。 ## 📁 项目结构 ``` knowledge-agent/ ├── agent.py # FastAPI 主服务文件 ├── agent_tools.py # 核心工具类 ├── gemini.py # Gemini API 处理器 ├── indentify/ # 内容识别模块 │ ├── image_identifier.py # 图像识别 │ ├── video_identifier.py # 视频识别 │ └── indentify.py # 识别主逻辑 ├── structure/ # 结构化处理模块 │ └── structure_processor.py ├── utils/ # 工具模块 │ ├── mysql_db.py # 数据库操作 │ ├── logging_config.py # 日志配置 │ └── file.py # 文件操作 ├── prompt/ # 提示词模板 ├── requirements.txt # 依赖包列表 └── start_service.sh # 启动脚本 ``` ## 🎯 核心功能 1. **内容识别**: 支持图像和视频的智能识别 2. **结构化处理**: 使用 Gemini API 进行内容结构化 3. **数据存储**: 自动存储处理结果到数据库 4. **异步处理**: 支持后台异步任务处理 5. **RESTful API**: 现代化的 HTTP API 接口 ## 🚨 注意事项 1. 确保设置了正确的 `GEMINI_API_KEY` 环境变量 2. 确保数据库连接配置正确 3. 服务默认监听 8080 端口,可通过参数修改 ## 🔍 故障排除 ### 常见问题 1. **导入错误**: 确保所有依赖已正确安装 2. **数据库连接失败**: 检查数据库配置和网络连接 3. **Gemini API 错误**: 验证 API 密钥是否正确 ### 日志查看 服务运行时会输出详细日志,包括: - 请求处理状态 - 错误信息 - 处理进度 - 性能指标