多模态文档检索处理服务器

基于MCP协议的RAG-Anything服务,提供多模态文档处理与智能检索功能,适用于知识管理和信息查询场景。

  • by nguyenngocdue
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 nguyenngocdue/RAG-MCP

使用说明

🚀 RAG Anything MCP服务器

一个强大的 模型上下文协议(MCP) 暴露的服务器 RAG什么都行 功能,使任何兼容MCP的应用程序都能执行高级多模式文档处理和检索。

🌟 特性

  • 📄 多格式文档处理:PDF、Office文档、图像、文本文件
  • 🔍 智能RAG查询:文本和多模式查询支持
  • 🧠 知识图谱集成:由LightRAG提供技术支持
  • 🖼️ 视觉语言模型:分析图像、表格和方程式
  • 🔌 MCP协议:适用于任何客户端应用程序的标准化接口
  • ⚡ 异步操作:高性能异步处理

🏗️ 建筑

┌─────────────────────────────────────────────────────────┐
│                   MCP Clients                           │
│  (VS Code, Claude Desktop, Custom Apps, etc.)           │
└─────────────────────────────────────────────────────────┘
                         │
                    MCP Protocol
                         │
┌─────────────────────────────────────────────────────────┐
│              RAG-Anything MCP Server                    │
│  ┌──────────────────────────────────────────────────┐   │
│  │  Document Processing Tools                       │   │
│  │  - upload_document                               │   │
│  │  - process_document                              │   │
│  │  - batch_process                                 │   │
│  └──────────────────────────────────────────────────┘   │
│  ┌──────────────────────────────────────────────────┐   │
│  │  Query Tools                                     │   │
│  │  - query_text                                    │   │
│  │  - query_multimodal                              │   │
│  │  - query_with_context                            │   │
│  └──────────────────────────────────────────────────┘   │
│  ┌──────────────────────────────────────────────────┐   │
│  │  Management Tools                                │   │
│  │  - list_documents                                │   │
│  │  - get_document_info                             │   │
│  │  - delete_document                               │   │
│  └──────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────┘
                         │
                         ▼
┌─────────────────────────────────────────────────────────┐
│                  RAG-Anything Core                      │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐  │
│  │   MinerU     │  │   LightRAG   │  │     VLM      │  │
│  │   Parser     │  │  Knowledge   │  │   Analysis   │  │
│  │              │  │    Graph     │  │              │  │
│  └──────────────┘  └──────────────┘  └──────────────┘  │
└─────────────────────────────────────────────────────────┘

🚀 快速开始

安装

# Clone repository
git clone <your-repo-url>
cd twin-editor-rag

# Install dependencies
pip install -e .

# Or with development dependencies
pip install -e ".[dev]"

配置

创建一个 .env 文件:

# API Configuration
OPENAI_API_KEY=your_openai_api_key
OPENAI_BASE_URL=https://api.openai.com/v1  # Optional

# Model Configuration
LLM_MODEL=gpt-4o-mini
VISION_MODEL=gpt-4o
EMBEDDING_MODEL=text-embedding-3-large
EMBEDDING_DIM=3072

# Model Profile Configuration (optional)
MODEL_PROFILE=openai_default
MODEL_PROFILES_PATH=./model_profiles.json
# Or set MODEL_PROFILES to a JSON object string with profile definitions

# Storage Configuration
RAG_STORAGE_DIR=./rag_storage
UPLOAD_DIR=./uploads
MAX_FILE_SIZE=100  # MB

# Server Configuration
LOG_LEVEL=INFO

模型配置文件允许您使用单个环境变量切换整个模型集。如果 MODEL_PROFILE 已设置,它将覆盖 LLM_MODEL, VISION_MODEL, EMBEDDING_MODEL,以及 EMBEDDING_DIM 使用任一 MODEL_PROFILES (JSON 字符串)或 MODEL_PROFILES_PATH (JSON文件,默认为 ./model_profiles.json).

运行服务器

# Start MCP server
python src/server.py

# Or use with specific config
python src/server.py --config config.json

与MCP客户端一起使用

VS代码(带MCP扩展)

添加到您的VS代码设置中:

{
  "mcp.servers": {
    "rag-anything": {
      "command": "python",
      "args": ["/path/to/twin-editor-rag/src/server.py"],
      "env": {
        "OPENAI_API_KEY": "your_key"
      }
    }
  }
}

克劳德桌面版

添加到 claude_desktop_config.json:

{
  "mcpServers": {
    "rag-anything": {
      "command": "python",
      "args": ["/path/to/twin-editor-rag/src/server.py"],
      "env": {
        "OPENAI_API_KEY": "your_key"
      }
    }
  }
}

📚 可用工具

文档处理

upload_document

将文档文件上传到服务器。

# Parameters:
- file_path: str - Path to document file
- doc_id: Optional[str] - Custom document ID

# Returns:
- doc_id: str - Document identifier
- status: str - Upload status

process_document

使用RAG Anything处理上传的文档。

# Parameters:
- doc_id: str - Document identifier
- parser: Optional[str] - Parser to use (mineru/docling)
- parse_method: Optional[str] - Parse method (auto/ocr/txt)

# Returns:
- doc_id: str
- status: str
- stats: dict - Processing statistics

batch_process_documents

批量处理多个文档。

# Parameters:
- file_paths: List[str] - List of file paths
- max_concurrent: Optional[int] - Max parallel processing

# Returns:
- results: List[dict] - Processing results

查询操作

query_text

使用文本查询RAG系统。

# Parameters:
- query: str - Query text
- mode: str - Query mode (hybrid/local/global/naive)
- top_k: Optional[int] - Number of results

# Returns:
- answer: str - RAG answer
- sources: List[dict] - Source documents

query_multimodal

使用多模态内容(图像、表格、方程式)进行查询。

# Parameters:
- query: str - Query text
- multimodal_content: List[dict] - Multimodal content
- mode: str - Query mode

# Returns:
- answer: str - RAG answer with multimodal understanding

管理

list_documents

列出所有已处理的文档。

# Returns:
- documents: List[dict] - Document list with metadata

get_document_info

获取文档的详细信息。

# Parameters:
- doc_id: str - Document identifier

# Returns:
- doc_info: dict - Detailed document information

delete_document

从系统中删除文档。

# Parameters:
- doc_id: str - Document identifier

# Returns:
- status: str - Deletion status

🔧 高级配置

自定义RAG配置

# config.json
{
  "rag_config": {
    "enable_image_processing": true,
    "enable_table_processing": true,
    "enable_equation_processing": true,
    "context_window": 2,
    "max_context_tokens": 3000
  },
  "parser_config": {
    "default_parser": "mineru",
    "default_parse_method": "auto"
  }
}

LLM提供程序配置

支持多个提供商:

  • 开放人工智能
  • Azure OpenAI
  • 人类学(克劳德)
  • 当地模特(LM工作室,Ollama)

📖 例子

示例1:处理和查询文档

# Using MCP client
import mcp

client = mcp.Client("rag-anything")

# Upload and process
result = await client.call_tool("upload_document", {
    "file_path": "./research_paper.pdf"
})
doc_id = result["doc_id"]

await client.call_tool("process_document", {
    "doc_id": doc_id
})

# Query
answer = await client.call_tool("query_text", {
    "query": "What are the main findings?",
    "mode": "hybrid"
})
print(answer["answer"])

示例2:多模式查询

# Query with table data
result = await client.call_tool("query_multimodal", {
    "query": "Compare these metrics with document content",
    "multimodal_content": [{
        "type": "table",
        "table_data": "Method,Accuracy\nRAG,95.2%\nBaseline,87.3%",
        "table_caption": "Performance Comparison"
    }],
    "mode": "hybrid"
})

🧪 测试

# Run tests
pytest tests/

# Run with coverage
pytest --cov=src tests/

# Test specific functionality
pytest tests/test_tools.py -v

🤝 贡献

欢迎投稿!拜托:

  1. 分叉存储库
  2. 创建要素分支
  3. 为新功能添加测试
  4. 提交拉取请求

📝 许可证

MIT许可证-有关详细信息,请参阅许可证文件

🔗 相关项目

⭐ 星迹

如果你觉得这很有用,请在repo上加星!

🚀 部署到Render.com

先决条件

  1. 渲染帐户:注册地址: render.com
  2. OpenAI API密钥:从以下位置获取钥匙 OpenAI平台
  3. Git仓库:将代码推送到GitHub/GitLab

选项1:使用render.yaml部署(推荐)

  1. 连接存储库:将您的GitHub/GitLab仓库链接到Render
  2. 设置环境变量:在“渲染”仪表板中,添加:
    • OPENAI_API_KEY:您的OpenAI API密钥
    • 其他可选变量来自 .env.example
  3. 部署:渲染将自动检测 render.yaml 并部署

选项2:手动部署

  1. 创建新Web服务 在渲染仪表板中
  2. 配置服务:
    • 环境:Docker
    • Dockerfile路径: ./Dockerfile
    • 实例类型:标准(建议使用512 MB+RAM)
  3. 添加磁盘 (用于持久存储):
    • 名字: rag-storage
    • 安装路径: /app/rag_storage
    • 尺寸:10 GB
  4. 设置环境变量:
    OPENAI_API_KEY=your_key_here
    LLM_MODEL=gpt-4o-mini
    VISION_MODEL=gpt-4o
    EMBEDDING_MODEL=text-embedding-3-large
    
  5. 部署:单击“创建Web服务”

部署后

您的MCP服务器将在以下网址提供:

https://your-app-name.onrender.com/mcp

使用HTTP传输从MCP客户端连接:

{
  "servers": {
    "rag-document-mcp": {
      "url": "https://your-app-name.onrender.com/mcp",
      "type": "http"
    }
  }
}

成本估算

  • 免费层:750小时/月(适合测试)
  • 启动器:7美元/月(建议用于生产)
  • 存储:0.25美元/GB/月

📧 联系

如有疑问或支持,请在GitHub上发布问题。