多格式文档问答代理RAG聊天机器人

基于代理架构和模型上下文协议(MCP)的RAG聊天机器人,支持多种文件格式的文档问答。

  • by Atusha18
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Atusha18/Agentic-RAG-Chatbot-for-Multi-Format-Document-QA-using-Model-Context-Protocol-MCP-

使用说明

面向多格式文档问答的代理式RAG聊天机器人(MCP)

这个项目实现了 基于代理的检索增强生成(RAG)聊天机器人 能够使用上传的各种格式文档来回答问题。它遵循一种 能动性建筑(或代理建筑) 并且使用 模型上下文协议(MCP) 用于智能体之间的通信。


特点/功能

  • 支持多种文件格式:

    • TXT / Markdown(保持原样,因为这是文件格式名称,无需翻译)
    • PDF(可添加解析功能)
    • DOCX(可添加解析功能)
    • PPTX(可添加解析功能)
    • CSV(可添加解析功能)
  • 代理式建筑(或“具有代理能力的建筑”)

    1. **摄入代理(或摄入组件)**解析并预处理文档。
    2. 检索代理处理嵌入表示和语义搜索。
    3. LLM响应代理使用检索到的上下文生成最终答案。
  • 模型上下文协议(MCP):

    • 代理通过结构化的MCP消息进行通信。
    • 每条信息包含 sender, receiver, type, trace_id,和 payload。
  • 模拟大型语言模型(Mock LLM)与嵌入(Embeddings):

    • 提供一个无需API密钥即可工作的管道。
    • 替换 MockLLM 使用OpenAI/GPT获取真实回复。
    • 替换 MockEmbedder 使用sentence-transformers进行真实嵌入。

项目结构

agentic_rag_mcp/ 可以翻译为“代理式检索增强生成多上下文处理(或类似含义的系统/模块名称,具体根据上下文确定)/” │── app/(应用目录) │ ├── main.py # 调度器/入口点 │ ├── llm_response_agent.py │ ├── ingestion_agent.py(翻译为中文可保持原名,若需解释则为“摄入代理.py”) │ ├── retrieval_agent.py(文件名可译为“检索代理.py”) │ ├── embedder.py(可翻译为:│ ├── 嵌入器.py 或 │ ├── 嵌入模块.py) │ ├── mcp.py 翻译为中文是:│ ├── mcp.py(文件名保持不变,因为文件名通常不需要翻译,除非有特定的含义或需要根据上下文进行解释) │ └── storage/(存储目录) │ └── store.py(翻译为中文:│ └── 存储模块/文件.py) ├── tests/(测试文件夹) │ └── demo_run.py # 示例运行脚本(演示流程) ├── examples/(示例目录) │ └── sample.txt # 示例文件 └── requirements.txt(需求文件)

YAML(YAML Ain't Markup Language) 复制代码


安装

git clone https://github.com/<USERNAME>/<REPO_NAME>.git
cd agentic_rag_mcp
python -m venv .venv
source .venv/bin/activate       # Windows: .venv\Scripts\activate
pip install -r requirements.txt
Running the Demo
Option 1: Run main orchestrator

bash
Copy code
python -m app.main
Option 2: Run demo test script

bash
Copy code
python -m tests.demo_run
You should see:

MCP messages from ingestion, retrieval, and LLM.

A final mock answer generated by the LLMResponseAgent.

Next Steps / Extensions
Replace MockLLM with a real LLM (OpenAI, GPT-J, or HuggingFace).

Replace MockEmbedder with real embeddings (SentenceTransformers, OpenAI embeddings).

Implement parsing for PDF, DOCX, PPTX, and CSV.

Add a FastAPI Web UI for interactive document QA.

Integrate a vector database (Chroma, FAISS, Pinecone) for large document retrieval.


---

## Challenges & Improvements

During development, one key challenge was handling **multi-format document parsing** efficiently — each file type (PDF, PPTX, DOCX, CSV) required a separate parsing logic and consistent text preprocessing.  
Coordinating message passing between agents through the **Model Context Protocol (MCP)** also required careful design to maintain structure and traceability.  

In future iterations, integrating **real embeddings** (e.g., SentenceTransformers or OpenAI embeddings), adding a **vector database** like FAISS or Chroma, and building an **interactive UI** (FastAPI or Streamlit) would significantly improve usability and performance.