内容
# 本项目的博客
https://daybreak.hashnode.dev/mcp-server-setup-part-1-building-a-local-rag-with-deepseek-for-enhanced-security-and-compliance
# 多上下文处理 (MCP) 服务器
本仓库包含一个多上下文处理 (MCP) 服务器的实现,该服务器使用 DeepSeek-R1 模型通过 RAG 框架 - lightRAG 处理和查询文档,能够处理文本和 PDF 文件。该服务器支持多种查询模式,并提供 RESTful API 接口。
## 特性
- **多种查询模式**:
- Naive Search:直接文档搜索
- Local Search:上下文感知的本地搜索
- Global Search:全面的文档搜索
- Hybrid Search:结合本地和全球搜索策略
- **PDF 支持**:内置支持使用 pdfplumber 处理 PDF 文档
- **FastAPI 集成**:现代、快速的 Web 框架,用于构建 API
- **异步处理**:异步文档处理和查询
- **错误处理**:强大的错误处理和重试机制
- **健康监测**:内置健康检查端点
## 先决条件
- Python 3.12 或更高版本
- 虚拟环境(推荐)
- DeepSeek API 密钥
- 本地运行的 Ollama(用于嵌入)
## 安装
1. 克隆仓库:
```bash
git clone <repository-url>
cd <repository-name>
```
2. 创建并激活虚拟环境:
```bash
python -m venv .venv
source .venv/bin/activate # 在 Windows 上使用 `.venv\Scripts\activate`
```
3. 安装依赖:
```bash
pip install -r requirements.txt
```
## 配置
1. 设置环境变量:
```bash
export DEEPSEEK_API_KEY=your_api_key_here
```
2. 确保 Ollama 在本地运行(用于嵌入):
```bash
# 检查 Ollama 是否在运行
curl http://localhost:11434/api/tags
```
## 使用
1. 启动服务器:
```bash
python mcp_server.py
```
2. 服务器将在 `http://localhost:8000` 可用
### API 端点
- `POST /insert`:插入新文档
```bash
curl -X POST "http://localhost:8000/insert" \
-H "Content-Type: application/json" \
-d '{"content": "Your document content here"}'
```
- `POST /query`:查询文档
```bash
curl -X POST "http://localhost:8000/query" \
-H "Content-Type: application/json" \
-d '{"query": "Your query here", "mode": "naive"}'
```
- `GET /modes`:列出可用的查询模式
```bash
curl "http://localhost:8000/modes"
```
- `GET /health`:检查服务器健康状态
```bash
curl "http://localhost:8000/health"
```
### 处理 PDF 文件
您可以使用提供的脚本处理 PDF 文件:
```python
import pdfplumber
pdf_path = "./your_document.pdf"
pdf_text = ""
with pdfplumber.open(pdf_path) as pdf:
for page in pdf.pages:
pdf_text += page.extract_text() + "\n"
# 然后可以通过 RAG 系统处理文本
```
## 依赖
- fastapi>=0.104.1:Web 框架
- uvicorn>=0.24.0:ASGI 服务器
- pydantic>=2.4.2:数据验证
- tenacity>=8.2.3:重试机制
- python-dotenv>=1.0.0:环境变量管理
- httpx>=0.25.1:HTTP 客户端
- numpy<2.0.0:数值计算
- scipy>=1.8.0,<1.14.0:科学计算
- tqdm>=4.66.1:进度条
- pdfplumber>=0.10.3:PDF 处理
## API 文档
服务器运行后,您可以访问交互式 API 文档:
- Swagger UI:`http://localhost:8000/docs`
- ReDoc:`http://localhost:8000/redoc`
## 错误处理
服务器包含全面的错误处理:
- API 连接问题
- PDF 处理错误
- 查询执行错误
- 无效的模式选择
## 贡献
1. Fork 仓库
2. 创建您的功能分支
3. 提交您的更改
4. 推送到分支
5. 创建新的 Pull Request
## 许可证
[在此处添加您的许可证信息]
## 联系
[在此处添加您的联系信息]