基于MCP的半导体元件搜索RAG系统
该项目展示了 MCP(模型上下文协议) 与ChromaDB和HuggingFace模型集成,用于检索增强生成(RAG)。
项目概述
该系统显示了如何 MCP工作 及其 目的:
- 主控程序 为上下文检索提供了一个标准化的协议
- ChromaDB 存储和检索语义嵌入
- RAG管道 将检索与LLM生成相结合
- 后端API 允许文档上传和问答
建筑
User Question → API Endpoint → RAG Pipeline
↓
Retrieval from ChromaDB (via embeddings)
↓
LLM generates answer with context
↓
Response to user关键组件
- MCP服务器 (
mcp_server.py):演示用于结构化上下文检索的MCP协议 - RAG管道 (
rag_pipeline.py):处理嵌入(编码)和LLM(解码) - FastAPI后端 (
main.py):用于文档上传和问答的REST API - ChromaDB:用于语义搜索的矢量数据库
安装
- 安装依赖项:
pip install -r requirements.txt- 设置环境变量:
创建一个 .env 文件或在中使用提供的HF API密钥 config.py
- 创建示例Excel文件:
python create_example_excel.py用法
1.启动API服务器
python main.pyAPI将于 http://localhost:8000
2.上传Excel文档
curl -X POST "http://localhost:8000/upload" \
-H "accept: application/json" \
-F "file=@examples/semiconductor_components.xlsx"或者使用FastAPI文档 http://localhost:8000/docs
3.提问
curl -X POST "http://localhost:8000/ask" \
-H "Content-Type: application/json" \
-d '{"question": "What MOSFET components are available?", "n_results": 3}'API终点
GET /-API信息GET /health-健康检查POST /upload-上传Excel文档POST /ask-问一个问题GET /info-获取收藏信息
MCP的工作原理
MCP(模型上下文协议) 作为以下内容的标准化接口:
- 上下文检索:查询和检索相关信息的结构化方式
- 工具定义:明确可用操作规范
- 协议通信:组件之间的标准化通信
在本项目中:
- MCP服务器定义了查询ChromaDB的工具
- RAG管道使用MCP原理进行上下文检索
- 后端集成了MCP概念用于文档处理
模型使用
- 编码(嵌入):
sentence-transformers/all-MiniLM-L6-v2 - 解码(LLM):HuggingFace的Llama模型(或回退到GPT-2)
示例问题
- “有哪些MOSFET元件可用?”
- “给我看看德州仪器的电压调节器”
- “哪些组件适用于5V?”
- “列出所有温度传感器”
项目结构
MCP2/
├── main.py # FastAPI backend
├── rag_pipeline.py # RAG pipeline with embeddings & LLM
├── mcp_server.py # MCP server for ChromaDB
├── config.py # Configuration
├── create_example_excel.py # Generate example data
├── requirements.txt # Dependencies
├── examples/ # Example Excel files
└── chroma_db/ # ChromaDB storage (created automatically)备注
- 首次运行将从HuggingFace下载模型(需要API密钥)
- ChromaDB数据保存在
./chroma_db/目录 - 上传的文件存储在
./uploads/目录
