# ai-service **Repository Path**: zw0627/ai-service ## Basic Information - **Project Name**: ai-service - **Description**: No description available - **Primary Language**: Python - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-03-27 - **Last Updated**: 2026-03-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: Python, AI ## README # AI Service - 智能向量存储与 AI Agent 平台 [![Python](https://img.shields.io/badge/Python-3.8+-blue.svg)](https://www.python.org/downloads/) [![License](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE) [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black) 基于 LangChain + LangGraph + MCP 的智能运维和会议室管理系统,支持 RAG 知识库、向量搜索和 AI Agent。 ## ✨ 特性 - 🧠 **统一的向量存储接口**: 支持 ChromaDB 和 Milvus 两种后端 - 🤖 **智能 AI Agent**: 基于 LangGraph 的 ReAct Agent,支持 RAG + 工具调用 - 🔧 **MCP 工具集成**: 支持运维场景和会议室场景的 MCP 工具链 - 🌐 **多端应用支持**: 提供命令行、Web界面、REST API和WebSocket接口 - 💬 **多轮对话管理**: 完整的会话历史记录和上下文管理 - 📊 **项目管理界面**: 可视化统计、批量操作和配置管理 - 📱 **响应式前端**: 现代化的React界面,支持移动端 - 📊 **完善的类型注解**: 使用 Python 类型提示和 Pydantic 数据验证 - 🧪 **全面的测试覆盖**: 单元测试和集成测试 - 🔒 **安全的配置管理**: 使用环境变量,支持配置验证 ## 📋 目录 - [安装](#安装) - [快速开始](#快速开始) - [使用指南](#使用指南) - [配置说明](#配置说明) - [开发指南](#开发指南) - [测试](#测试) - [贡献](#贡献) ## 安装 ### 环境要求 - Python 3.8 或更高版本 - pip 或 conda ### 安装步骤 1. 克隆项目 ```bash git clone cd ai-service ``` 2. 创建虚拟环境 ```bash python -m venv venv source venv/bin/activate # Linux/Mac # 或 venv\Scripts\activate # Windows ``` 3. 安装依赖 ```bash pip install -r requirements.txt ``` 4. 配置环境变量 ```bash cp .env.example .env # 编辑 .env 文件,填入你的 API Key 和其他配置 ``` ## 快速开始 ### 方式一:命令行界面(原有功能) #### 1. 导入文件到向量库 ```bash python src/main.py ingest --file manual.md --collection ops_manual ``` #### 2. 搜索向量 ```bash python src/main.py search --collection ops_manual --query "内存溢出" --limit 5 ``` #### 3. 启动 AI Agent (运维场景) ```bash python src/main.py agent --mode ops --query "订单服务响应极慢,好像挂了,请立刻处理!" ``` #### 4. 启动 AI Agent (会议室场景) ```bash python src/main.py agent --mode meeting --query "预定明天10点的会议室" ``` ### 方式二:Streamlit Web界面(新增) #### 1. 启动Web应用 ```bash streamlit run web_app.py ``` #### 2. 访问界面 打开浏览器访问 `http://localhost:8501` ### 方式三:FastAPI后端 + React前端(新增) #### 1. 启动后端API服务 ```bash python api/main.py ``` #### 2. 启动前端开发服务器 ```bash cd frontend npm install npm start ``` #### 3. 访问完整应用 打开浏览器访问 `http://localhost:3000` ## 使用指南 ### 向量服务 #### 使用 ChromaDB ```python from src.core.vector_service import ChromaVectorService # 创建服务 service = ChromaVectorService(persist_dir="./chroma_db") # 导入文件 count = service.ingest_file("manual.md", "ops_manual") # 搜索 results = service.search("ops_manual", "内存溢出", limit=5) ``` #### 使用 Milvus ```python from src.core.vector_service import MilvusVectorService # 创建服务 service = MilvusVectorService(db_path="milvus_demo.db") # 导入文件 count = service.ingest_file("manual.md", "ops_manual") # 搜索 results = service.search("ops_manual", "内存溢出", limit=5) ``` #### 统一接口 ```python from src.core.vector_service import get_vector_service # 自动选择后端 service = get_vector_service(backend="chroma") # 或 "milvus" ``` ### MCP 服务 ```python from src.core.mcp_service import get_mcp_service, MCPMode, LangChainMCPAdapter import asyncio async def main(): # 创建 MCP 服务 mcp_service = await get_mcp_service(MCPMode.OPS) await mcp_service.initialize() # 转换为 LangChain 工具 adapter = LangChainMCPAdapter(mcp_service) tools = await adapter.convert_tools() # 使用工具 for tool in tools: print(f"Tool: {tool.name}") asyncio.run(main()) ``` ### AI Agent 服务 ```python from src.core.agent_service import AgentService import asyncio async def main(): # 创建 Agent agent = AgentService(model_name="gpt-4o", verbose=True) # 添加工具 from langchain_core.tools import Tool def get_logs(service_name: str) -> str: return f"[{service_name}] Logs..." agent.add_tool(Tool(name="get_logs", description="获取日志", func=get_logs)) # 构建 Agent await agent.build_agent() # 运行 result = await agent.run("订单服务挂了,请处理!") print(result["final_answer"]) asyncio.run(main()) ``` ### Web应用服务 #### Streamlit界面 ```python # 启动Streamlit应用 streamlit run web_app.py ``` #### FastAPI后端 ```python # 启动API服务 python api/main.py ``` #### React前端 ```bash # 启动前端开发服务器 cd frontend npm install npm start ``` ### REST API接口 #### 聊天接口 ```bash # POST请求示例 curl -X POST http://localhost:8000/api/chat \ -H "Content-Type: application/json" \ -d '{ "message": "订单服务挂了,请处理!", "mode": "ops" }' ``` #### 会话管理 ```bash # 获取所有会话 curl http://localhost:8000/api/sessions # 获取会话详情 curl http://localhost:8000/api/sessions/{session_id} # 删除会话 curl -X DELETE http://localhost:8000/api/sessions/{session_id} ``` #### 向量库操作 ```bash # 导入文档 curl -X POST http://localhost:8000/api/vector/ingest \ -H "Content-Type: application/json" \ -d '{ "content": "文档内容...", "collection": "ops_manual" }' # 搜索向量 curl http://localhost:8000/api/vector/search?collection=ops_manual&query=内存溢出&limit=5 ``` #### WebSocket聊天 ```javascript // JavaScript示例 const ws = new WebSocket('ws://localhost:8000/ws/chat'); ws.onopen = () => { ws.send(JSON.stringify({ message: "预定明天10点的会议室", mode: "meeting" })); }; ws.onmessage = (event) => { const data = JSON.parse(event.data); console.log('AI回复:', data.response); }; ``` ## 配置说明 ### 环境变量 编辑 `.env` 文件配置以下参数: ```bash # LLM 配置 OPENAI_API_KEY=your-api-key-here OPENAI_MODEL=gpt-4o # Embedding 配置 EMBEDDING_MODEL=sentence-transformers/all-MiniLM-L6-v2 # 向量数据库配置 VECTOR_BACKEND=chroma # 或 milvus CHROMA_PERSIST_DIR=./chroma_db MILVUS_DB_PATH=milvus_demo.db # RAG 配置 KNOWLEDGE_BASE_PATH=manual.md CHUNK_SIZE=400 CHUNK_OVERLAP=50 TOP_K=3 # MCP 配置 MCP_SERVER_CMD=python MCP_SERVER_ARGS=mcp_server.py # 日志配置 LOG_LEVEL=INFO ``` ### 配置验证 ```python from src.config.settings import Config # 验证配置 errors = Config.validate() if errors: print("配置错误:") for error in errors: print(f" - {error}") else: print("✅ 配置验证通过") ``` ## 开发指南 ### 项目结构 ``` ai-service/ ├── src/ # Python后端源代码 │ ├── core/ # 核心模块 │ │ ├── vector_service.py # 向量服务 │ │ ├── mcp_service.py # MCP 服务 │ │ └── agent_service.py # Agent 服务 │ ├── agent/ # Agent 客户端 │ ├── mcp/ # MCP 服务器 │ ├── milvus_svc/ # Milvus 服务封装 │ └── main.py # 命令行入口 ├── api/ # FastAPI后端 │ └── main.py # API服务主程序 ├── frontend/ # React前端 │ ├── public/ │ ├── src/ │ │ ├── App.js # 主应用组件 │ │ ├── App.css # 样式文件 │ │ └── index.js # 入口文件 │ └── package.json # 前端依赖 ├── web_app.py # Streamlit Web应用 ├── requirements.txt # Python依赖 ├── .env.example # 环境变量示例 ├── pyproject.toml # Python项目配置 └── README.md # 项目文档 ``` ### 代码规范 - 使用 Black 格式化代码 - 使用 isort 排序导入 - 添加类型注解 - 编写文档字符串 ### 添加新工具 1. 在 `src/mcp/` 下创建 MCP 服务器 2. 使用 FastMCP 装饰器定义工具 3. 在 `src/core/mcp_service.py` 中注册 4. 编写单元测试 ### 扩展Web功能 #### 添加新API端点 1. 在 `api/main.py` 中添加新的路由 2. 定义对应的数据模型 3. 实现业务逻辑 4. 在前端添加对应界面 #### 修改前端界面 1. 在 `frontend/src/` 下添加新组件 2. 在 `App.js` 中集成 3. 添加对应的API调用 4. 更新样式文件 #### 会话管理扩展 项目已实现完整的会话管理系统,支持: - 多轮对话上下文保持 - 会话历史记录 - 会话持久化 - 工具使用跟踪 ## 测试 ### 运行所有测试 ```bash pytest tests/ -v ``` ### 运行特定测试 ```bash pytest tests/test_vector_service.py -v ``` ### 生成覆盖率报告 ```bash pytest tests/ --cov=src --cov-report=html ``` ## 贡献 欢迎贡献!请遵循以下步骤: 1. Fork 本项目 2. 创建特性分支 (`git checkout -b feature/AmazingFeature`) 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) 4. 推送到分支 (`git push origin feature/AmazingFeature`) 5. 提交 Pull Request ## 许可证 本项目采用 MIT 许可证 - 详见 [LICENSE](LICENSE) 文件 ## 联系方式 如有问题或建议,请提交 Issue 或 Pull Request。 ## 致谢 - [LangChain](https://github.com/langchain-ai/langchain) - [LangGraph](https://github.com/langchain-ai/langgraph) - [MCP](https://modelcontextprotocol.io/) - [ChromaDB](https://www.trychroma.com/) - [Milvus](https://milvus.io/) --- ⭐ 如果这个项目对你有帮助,请给一个 Star!