RAG管道
RAG使用 铬云 (密集搜索)和 LangChain.通过以下方式显示工具 主控程序 (模型上下文协议),以便客户端可以调用 rag_query 和 rag_query_with_chunks 通过HTTP。
设置
创建virtualenv并安装依赖项:```bash
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
CHROMA_API_KEY=...
CHROMA_TENANT=...
CHROMA_DATABASE=rag_dev
本地开发
运行MCP HTTP服务器```bash
uvicorn main:app --reload --port 8000
### 测试终点
**健康检查:**```bash
curl http://127.0.0.1:8000/health
调用MCP工具 (使用尾随斜线 /mcp/ 为了避免307重定向):
rag_query_with_chunks --以JSON格式返回answer+排名块:```bash
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?",
"request_id": "12345678",
"session_id": "123456"
}
}
}' \
http://localhost:8000/mcp/
响应为JSON-RPC;工具结果是一个JSON字符串,其中包含: `answer`, `chunks` (每个与 `rank`, `chunk_id`, `source`, `preview`, `text`, `scores` (例如。 `bm25_raw`, `bm25_norm`, `dense_raw`, `dense_norm`, `distance`, `hybrid`), `metadata`), `used_chunk_ids` (用于答案的唯一块ID), `retrieval` (`k`, `alpha`, `filters`, `warnings`).
______________________________________________________________________
## 码头工人
构建并运行:```bash
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-v2-{env} 哪里 {env} = dev, qa,或 prod每个应用程序都有自己的秘密;相同的 fly.toml 适用于所有人。
一次性设置```bash
brew install flyctl
fly auth login
fly auth token # Use output as GitHub Actions secret FLY_API_TOKEN if using CI
### 创建应用程序
每个环境运行一次:```bash
fly launch --name mcp-tool-rag-query-v2-dev
fly launch --name mcp-tool-rag-query-v2-qa
fly launch --name mcp-tool-rag-query-v2-prod
设置秘密```bash
fly deploy --no-cache --app mcp-tool-rag-query-v2-dev
fly deploy --no-cache --app mcp-tool-rag-query-v2-qa
fly deploy --no-cache --app mcp-tool-rag-query-v2-prod
**问答** (从 `.env.qa`):```bash
curl https://mcp-tool-rag-query-v2-dev.fly.dev/health
生产环境 (从 .env.prod):
> 提示: 如果 grep/cut 损坏密钥(例如401错误),直接粘贴密钥: `fly secrets set -a mcp-tool-rag-query-v2-dev OPENAI_API_KEY="sk-proj-YOUR_KEY"````bash
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-v2-dev.fly.dev/mcp/
### 测试部署的应用程序
替换 `{env}` 随着 `dev`, `qa`,或 `prod`:
**健康检查:**```bash
curl https://mcp-tool-rag-query-v2-dev.fly.dev/health
调用MCP工具:
rag_query_with_chunks --答案加上以JSON形式排列的块:bash
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-v2-dev.fly.dev/mcp/
