面向多格式文档问答的代理式RAG聊天机器人(MCP)
这个项目实现了 基于代理的检索增强生成(RAG)聊天机器人 能够使用上传的各种格式文档来回答问题。它遵循一种 能动性建筑(或代理建筑) 并且使用 模型上下文协议(MCP) 用于智能体之间的通信。
特点/功能
-
支持多种文件格式:
-
PDF(可添加解析功能)
- DOCX(可添加解析功能)
- PPTX(可添加解析功能)
-
CSV(可添加解析功能)
-
代理式建筑(或“具有代理能力的建筑”)
- 摄入代理(或摄入组件)解析并预处理文档。
- 检索代理处理嵌入表示和语义搜索。
- LLM响应代理使用检索到的上下文生成最终答案。
-
模型上下文协议(MCP):
-
代理通过结构化的MCP消息进行通信。
-
每条信息包含
sender,receiver,type,trace_id,和payload。 -
模拟大型语言模型(Mock LLM)与嵌入(Embeddings):
-
提供一个无需API密钥即可工作的管道。
- 替换
MockLLM使用OpenAI/GPT获取真实回复。 - 替换
MockEmbedder使用sentence-transformers进行真实嵌入。
项目结构
│── app/(应用目录)
│ ├── main.py # 调度器/入口点
│ ├── llm_response_agent.py
│ ├── retrieval_agent.py(文件名可译为“检索代理.py”)
│ └── storage/(存储目录)
├── tests/(测试文件夹)
│ └── demo_run.py # 示例运行脚本(演示流程)
├── examples/(示例目录)
│ └── sample.txt # 示例文件
└── requirements.txt(需求文件)
YAML(YAML Ain't Markup Language)
复制代码
安装```bash
git clone https://github.com//.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.
```
