RAG-MCP应用
Spring Boot应用程序实现 检索增强生成(RAG) 随着 模型上下文协议(MCP) 使用Groq的LLM API访问文件系统、数据库、RESTAPI和web内容的集成。
🌟 特性
RAG能力
- ✅ 上下文增强的AI响应 -在生成响应之前检索相关文档
- ✅ 向量相似性搜索 -内存中余弦相似性搜索(可升级到Qdrant)
- ✅ 智能文档分块 -具有可配置重叠的基于句子的组块
- ✅ 多源摄入 -文件系统、数据库、网页和API
MCP集成
- ✅ 文件系统访问 -使用Apache Tika进行安全文件读取(PDF、DOCX、TXT、MD等)
- ✅ 数据库查询 -PostgreSQL、MySQL、SQLite支持
- ✅ REST API集成 -带有自定义标头的GET/POST请求
- ✅ Web剪贴 -从网页中提取文本
AI集成
- ✅ 格罗克API -使用llama-3.3-70b-进行快速推理
- ✅ 嵌入生成 -384维向量(模拟,可用于真实模型)
- ✅ 缓存 -嵌入缓存以提高性能
🚀 快速开始
先决条件
- Java 17+
- Maven 3.6+
- Groq API密钥(已配置)
安装
- 克隆存储库
git clone https://github.com/Mukundkumar07/mcp-application.git
cd mcp-application- 构建项目
mvn clean install- 运行应用程序
mvn spring-boot:run应用程序将于启动 http://localhost:8080
📖 API 文档
RAG聊天
POST http://localhost:8080/api/rag/chat
Content-Type: application/json
{
"message": "What are the main features?",
"topK": 5
}文件摄入
来自文件系统:
POST http://localhost:8080/api/rag/ingest/filesystem
Content-Type: application/json
{
"path": "my-docs",
"filePattern": ".*\\.md"
}来自数据库:
POST http://localhost:8080/api/rag/ingest/database
Content-Type: application/json
{
"query": "SELECT * FROM articles",
"connectionString": "jdbc:postgresql://localhost:5432/mydb?user=user&password=pass"
}来自网络:
POST http://localhost:8080/api/rag/ingest/web
Content-Type: application/json
{
"url": "https://example.com/article"
}搜索文档
GET http://localhost:8080/api/rag/search?query=machine learning&topK=10系统管理
# Health check
GET http://localhost:8080/api/rag/health
# Vector DB stats
GET http://localhost:8080/api/rag/stats/vector-db
# Clear database
DELETE http://localhost:8080/api/rag/clear⚙️ 配置
编辑 src/main/resources/application.properties:
# Groq API
groq.api.key=your-groq-api-key
groq.api.model=llama-3.3-70b-versatile
# Vector Database
vector.db.enabled=false # Set to true when using Qdrant
vector.db.host=localhost
vector.db.port=6333
# MCP Filesystem
mcp.filesystem.enabled=true
mcp.filesystem.root-path=/path/to/documents
mcp.filesystem.allowed-extensions=pdf,txt,md,docx,java,py,json,xml,html,css,js
# MCP External Resources
mcp.database.enabled=false # Enable for database access
mcp.api.enabled=true
mcp.cloud-storage.enabled=false
# RAG Settings
rag.chunk-size=512
rag.chunk-overlap=50
rag.retrieval-top-k=5
rag.max-context-length=4000🏗️ 建筑
┌─────────────┐
│ User │
└──────┬──────┘
│ HTTP
▼
┌─────────────────────┐
│ RAG Controller │
└──────┬──────────────┘
│
├──────────────────────┐
│ │
▼ ▼
┌─────────────┐ ┌──────────────────┐
│ RAG Service │ │ Ingestion Service│
└──────┬──────┘ └────────┬─────────┘
│ │
├──────┬───────────────┼──────────┐
│ │ │ │
▼ ▼ ▼ ▼
┌────────┐ ┌──────┐ ┌──────────┐ ┌─────────┐
│Embedding│ │Vector│ │Filesystem│ │External │
│ Service│ │ DB │ │ MCP │ │ MCP │
└────────┘ └──┬───┘ └────┬─────┘ └────┬────┘
│ │ │
▼ ▼ ▼
┌─────────┐ ┌─────────┐ ┌─────────┐
│ Groq API│ │ Files │ │DB/API/Web│
└─────────┘ └─────────┘ └─────────┘📁 项目结构
mcp-application/
├── src/main/java/com/example/
│ ├── controller/
│ │ ├── HelloController.java # Original Groq endpoints
│ │ └── RAGController.java # RAG & MCP endpoints
│ ├── service/
│ │ ├── GroqService.java # Groq API integration
│ │ ├── EmbeddingService.java # Text embeddings
│ │ ├── VectorDatabaseService.java # Vector storage
│ │ ├── DocumentChunker.java # Text chunking
│ │ ├── RAGService.java # RAG orchestration
│ │ └── DocumentIngestionService.java
│ ├── mcp/
│ │ ├── FileSystemMCPServer.java # Filesystem access
│ │ └── ExternalResourceMCPServer.java # DB/API/Web
│ ├── model/
│ │ ├── Document.java
│ │ ├── DocumentChunk.java
│ │ ├── RAGRequest.java
│ │ └── RAGResponse.java
│ └── config/
│ └── GroqConfig.java
├── src/main/resources/
│ └── application.properties
└── pom.xml🧪 测试
1.创建测试文档
mkdir -p /path/to/documents/test
echo "Spring Boot is a Java framework." > /path/to/documents/test/spring.txt
echo "RAG combines retrieval and generation." > /path/to/documents/test/rag.txt2.摄入文件
curl -X POST http://localhost:8080/api/rag/ingest/filesystem \
-H "Content-Type: application/json" \
-d '{"path": "test"}'3.使用RAG查询
curl -X POST http://localhost:8080/api/rag/chat \
-H "Content-Type: application/json" \
-d '{"message": "What is Spring Boot?", "topK": 3}'🔧 高级设置
启用Qdrant矢量数据库
# Start Qdrant
docker run -p 6333:6333 qdrant/qdrant
# Update application.properties
vector.db.enabled=true启用数据库访问
mcp.database.enabled=true📚 依赖项
- 弹簧靴3.2.1 -应用框架
- 格罗克API -LLM推理
- 阿帕奇蒂卡2.9.1 -文档解析(PDF、DOCX等)
- OkHttp 4.12.0 -HTTP客户端
- Qdrant客户端1.7.0 -矢量数据库(可选)
- DJL 0.25.0 -用于嵌入的深度学习库
🤝 贡献
欢迎投稿!请随时提交拉取请求。
📝 许可证
这个项目是开源的,可以在MIT许可证下使用。
👤 作者
穆昆德·库马尔
- github: @穆库德库马07
🙏 致谢
______________________________________________________________________
建于❤️ 使用Spring Boot、RAG和MCP
