RAG管道
RAG使用 MongoDB 阿特拉斯 (密集矢量搜索+BM25文本搜索)和 LangChainRRF融合的双重召回。通过以下方式公开工具 主控程序 (模型上下文协议),以便客户端可以调用 rag_query_with_chunks 通过HTTP。
______________________________________________________________________
设置
创建virtualenv并安装依赖项:
python3.11 -m venv venv
source venv/bin/activate # or: venv\Scripts\activate on Windows
pip install --upgrade pip
pip install -r requirements.txt创建一个 .env 项目根目录中的文件:
APP_VERSION=v:1.01
OPENAI_API_KEY=your-openai-key
MONGODB_URI=mongodb+srv://...
MONGODB_DB=db_hunt
MONGODB_COLLECTION=collection_taixingbi_dev
ATLAS_VECTOR_INDEX=vector_index
ATLAS_SEARCH_INDEX=default______________________________________________________________________
本地开发
运行MCP HTTP服务器
uvicorn main:app --reload --port 8000测试终点
健康检查:
curl http://127.0.0.1:8000/health调用MCP工具 (使用尾随斜线 /mcp/ 为了避免307重定向):
rag_query_with_chunks --以JSON格式返回answer+排名块:
curl -s --max-time 60 -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{
"jsonrpc": "2.0",
"id": "call-001",
"method": "tools/call",
"params": {
"name": "rag_query_with_chunks",
"arguments": {
"question": "what is Taixing visa status?",
"request_id": "12345678",
"session_id": "123456"
}
}
}' \
http://localhost:8000/mcp/响应为JSON-RPC;工具结果是一个JSON字符串,其中包含: answer, chunks (每个与 rank, chunk_id, source, preview, text, scores (例如。 rrf_score, distance, search_score), metadata), used_chunk_ids (用于答案的唯一块ID), retrieval (k, top_k_dense, top_k_bm25, top_k_final, rrf_k, filters, warnings).
______________________________________________________________________
码头工人
构建并运行:
docker build -t rag-mcp .
docker run -p 8000:8000 --env-file .env rag-mcp注: 如果你看到 *“必须设置api_key客户端选项”*,确保--env-file .env指向包含以下内容的文件OPENAI_API_KEY.
______________________________________________________________________
Fly.io部署
. -->
每个环境使用一个应用程序: mcp-tool-rag-query-mongodb-v1-{env} 哪里 {env} = dev, qa,或 prod每个应用程序都有自己的秘密;相同的 fly.toml 适用于所有人。
一次性设置
brew install flyctl
fly auth login
fly auth token # Use output as GitHub Actions secret FLY_API_TOKEN if using CI创建应用程序
每个环境运行一次:
fly launch --name mcp-tool-rag-query-mongodb-v1-dev
fly launch --name mcp-tool-rag-query-mongodb-v1-qa
fly launch --name mcp-tool-rag-query-mongodb-v1-prod设置秘密
fly deploy --no-cache --app mcp-tool-rag-query-mongodb-v1-dev
fly deploy --no-cache --app mcp-tool-rag-query-mongodb-v1-qa
fly deploy --no-cache --app mcp-tool-rag-query-mongodb-v1-prod问答 (从 .env.qa):
curl https://mcp-tool-rag-query-mongodb-v1-dev.fly.dev/health生产环境 (从 .env.prod):
提示: 如果grep/cut损坏密钥(例如401错误),直接粘贴密钥:fly secrets set -a mcp-tool-rag-query-mongodb-v1-dev OPENAI_API_KEY="sk-proj-YOUR_KEY"
curl -s --max-time 60 -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{
"jsonrpc": "2.0",
"id": "call-001",
"method": "tools/call",
"params": {
"name": "rag_query_with_chunks",
"arguments": {
"question": "what is Taixing expected compensation?",
"request_id": "12345678",
"session_id": "123456"
}
}
}' \
https://mcp-tool-rag-query-mongodb-v1-dev.fly.dev/mcp/测试部署的应用程序
替换 {env} 随着 dev, qa,或 prod:
健康检查:
curl https://mcp-tool-rag-query-mongodb-v1-dev.fly.dev/health调用MCP工具:
rag_query_with_chunks --答案加上以JSON形式排列的块:
curl -s -X POST \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","id":"call-001","method":"tools/call","params":{"name":"rag_query_with_chunks","arguments":{"question":"what is Taixing visa?","request_id":"12345678","session_id":"123456"}}}' \
https://mcp-tool-rag-query-mongodb-v1-dev.fly.dev/mcp/