弹性MCP聊天
一个智能多索引搜索应用程序,具有人工智能驱动的决策能力,通过MCP(模型上下文协议)使用FastAPI、LangGraph和Elasticsearch构建。
特性
- 🔍 智能多索引搜索:跨多个Elasticsearch索引的人工智能搜索
- 🤖 LangGraph代理:搜索策略和上下文检索的复杂决策
- 🚀 FastAPI后端:具有全面错误处理功能的高性能异步API
- 💬 实时聊天界面:WebSocket支持的聊天,带有流式响应
- 📊 监控和指标:内置性能监控和健康检查
- 🔧 MCP集成:通过模型上下文协议与Elasticsearch无缝集成
- 🎯 Olama集成:用于智能查询处理的本地AI模型集成
建筑
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ Chat UI │ │ FastAPI │ │ LangGraph │
│ (WebSocket) │◄──►│ Backend │◄──►│ Agent │
└─────────────────┘ └─────────────────┘ └─────────────────┘
│ │
▼ ▼
┌─────────────────┐ ┌─────────────────┐
│ MCP Client │ │ Ollama │
│ (Elasticsearch)│ │ Client │
└─────────────────┘ └─────────────────┘快速开始
先决条件
- Python 3.11+
- Elasticsearch集群(可通过MCP服务器访问)
- Ollama与兼容型号
- 为Elasticsearch运行的MCP服务器
安装
- 克隆仓库:
git clone
cd elastic_mcp- 创建虚拟环境:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- 安装依赖项:
pip install -r requirements.txt- 配置环境:
cp .env.example .env
# Edit .env with your configuration- 运行应用程序:
python -m app.main配置
应用程序使用环境变量进行配置。关键设置:
Elasticsearch和MCP
# MCP Server Configuration
MCP_SERVER_URL=http://10.11.200.109:9999/
MCP_TIMEOUT=30
# Elasticsearch Configuration (via MCP)
ELASTICSEARCH_INDICES=index1,index2,index3Ollama配置
# Ollama Configuration
OLLAMA_BASE_URL=http://localhost:11434
OLLAMA_MODEL=llama2
OLLAMA_TIMEOUT=60应用程序设置
# Application Configuration
APP_HOST=0.0.0.0
APP_PORT=8000
DEBUG=true
LOG_LEVEL=INFO
ENABLE_MONITORING=trueAPI终点
聊天端点
POST /api/chat-发送聊天消息GET /api/chat/history-获取对话历史记录WebSocket /api/chat/ws-实时聊天连接GET /api/chat/stream-服务器已发送流媒体事件
搜索端点
POST /api/search-执行智能搜索GET /api/indices-获取可用索引信息
监控端点
GET /health-全面健康检查GET /metrics-应用程序指标GET /-API信息
使用示例
基本聊天
import httpx
async with httpx.AsyncClient() as client:
response = await client.post(
"http://localhost:8000/api/chat",
json={
"message": "Find documents about machine learning",
"conversation_id": "conv_123"
}
)
print(response.json())Websocket聊天
const ws = new WebSocket('ws://localhost:8000/api/chat/ws');
ws.onopen = function() {
ws.send(JSON.stringify({
type: 'message',
content: 'Hello, how can I search for documents?'
}));
};
ws.onmessage = function(event) {
const data = JSON.parse(event.data);
console.log('Response:', data);
};直接搜索
response = await client.post(
"http://localhost:8000/api/search",
json={
"query": "artificial intelligence",
"search_type": "semantic",
"indices": ["tech_docs", "research_papers"],
"max_results": 10
}
)发展
项目结构
elastic_mcp/
├── app/
│ ├── api/ # API endpoints
│ ├── core/ # Core configuration
│ ├── middleware/ # Custom middleware
│ ├── models/ # Pydantic models
│ ├── services/ # Business logic services
│ ├── static/ # Static files (UI)
│ ├── utils/ # Utility functions
│ └── main.py # Application entry point
├── tests/ # Test files
├── .env # Environment configuration
├── requirements.txt # Python dependencies
└── README.md # This file运行测试
pytest tests/ -v --cov=app代码质量
# Format code
black app/ tests/
# Lint code
flake8 app/ tests/
# Type checking
mypy app/详细功能
智能搜索
该应用程序使用LangGraph创建一个智能代理,该代理:
- 分析用户查询以确定搜索意图
- 选择适当的搜索策略(关键字、语义、混合)
- 根据查询上下文选择相关索引
- 对来自多个来源的结果进行组合和排名
错误处理
全面的错误处理,包括:
- 自定义异常层次结构
- 详细的错误响应
- 自动重试机制
- 优雅降级
监控
内置监控包括:
- 请求/响应指标
- 系统资源使用情况
- 所有服务的健康检查
- 性能分析
安全
安全功能:
- 输入验证和净化
- 速率限制
- CORS配置
- 安全错误消息
部署
Docker部署
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY app/ ./app/
EXPOSE 8000
CMD ["python", "-m", "app.main"]生产配置
对于生产部署:
- 集
DEBUG=false - 配置适当的日志记录级别
- 设置反向代理(nginx)
- 启用监控和警报
- 配置备份策略
故障排除
常见问题
- MCP连接失败
- 验证MCP服务器是否正在运行 - 检查网络连接 - 验证MCP服务器URL
- Ollama没有回应
- 确保Ollama服务正在运行 - 检查型号可用性 - 验证网络配置
- 搜索错误
- 验证Elasticsearch索引是否存在 - 检查索引权限 - 验证查询语法
日志
应用程序日志的结构包括:
- 请求跟踪
- 错误详细信息
- 性能指标
- 服务健康状态
贡献
- 复刻仓库
- 创建要素分支
- 进行更改
- 添加测试
- 提交拉取请求
许可证
此项目根据MIT许可证获得许可-有关详细信息,请参阅许可证文件。
支持
有关支持和问题:
- 在存储库中创建问题
- 检查文档
- 查看故障排除指南
