🌟 LangChain 30 天保姆级教程 · Day 30|终章!RAG 项目打包交付:Docker + CI/CD + 文档,打造企业级 AI 知识库!

14 阅读4分钟

系列目标:30 天从 LangChain 入门到企业级部署
今日任务:整合全部模块 → 容器化 → 自动化测试与部署 → 编写交付文档!


🎁 一、30 天成果回顾

我们已构建一个功能完备的 RAG 系统,包含:

表格

模块技术栈功能
文档处理PyPDFLoader + RecursiveTextSplitterPDF/Word 解析与分块
向量存储Milvus / PGVector千万级语义检索
大模型Ollama + Qwen 7B本地推理,中文优化
安全防护输入过滤 + 权限标签 + 脱敏防注入、防泄露
性能优化Redis 缓存 + 流式输出延迟 <500ms
监控告警Prometheus + Grafana实时可观测性
评估体系自定义规则 + 人工模板质量可量化

💡 今天,我们将这一切打包成“开箱即用”的企业产品


📦 二、项目结构标准化

enterprise-rag/
├── docker-compose.yml          # 一键启动所有服务
├── Dockerfile                  # RAG 应用镜像
├── src/
│   ├── rag_app.py              # 主应用(FastAPI)
│   ├── security/               # 安全模块
│   ├── monitoring/             # 监控埋点
│   └── evaluation/             # 评估流水线
├── docs/
│   ├── ARCHITECTURE.md         # 架构图
│   ├── DEPLOY.md               # 部署手册
│   └── API.md                  # 接口文档
├── tests/
│   ├── test_security.py        # 安全测试
│   └── test_performance.py     # 性能压测
├── .github/workflows/
│   └── ci-cd.yml               # GitHub Actions
└── requirements.txt

✅ 符合企业项目规范:代码 + 配置 + 文档 + 测试 四件套


🐳 三、动手实践 1:Docker 容器化

步骤 1:编写 Dockerfile

# Dockerfile
FROM python:3.10-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# 安装 Ollama(关键!)
RUN curl -fsSL https://ollama.com/install.sh | sh

COPY src/ ./src/
COPY models/ ./models/  # 可选:预下载模型

EXPOSE 8000 8001

# 启动脚本:先拉模型,再启服务
CMD ["sh", "-c", "ollama serve & sleep 5 && ollama pull qwen:7b && python src/rag_app.py"]

⚠️ 注意:Ollama 需在容器内运行,不能复用宿主机


步骤 2:编写 docker-compose.yml

# docker-compose.yml
version: '3.8'

services:
  rag-app:
    build: .
    ports:
      - "8000:8000"   # API
      - "8001:8001"   # Prometheus metrics
    depends_on:
      - milvus-standalone
      - redis
    environment:
      - MILVUS_HOST=milvus-standalone
      - REDIS_HOST=redis

  milvus-standalone:
    image: milvusdb/milvus:v2.4.6
    ports:
      - "19530:19530"
    volumes:
      - milvus_data:/var/lib/milvus

  redis:
    image: redis:7-alpine
    ports:
      - "6379:6379"

  prometheus:
    image: prom/prometheus
    ports:
      - "9090:9090"
    volumes:
      - ./monitoring/prometheus.yml:/etc/prometheus/prometheus.yml

  grafana:
    image: grafana/grafana
    ports:
      - "3000:3000"
    environment:
      - GF_SECURITY_ADMIN_PASSWORD=admin123

volumes:
  milvus_data:

✅ 一条命令启动全套系统

docker-compose up -d

🔄 四、动手实践 2:CI/CD 自动化(GitHub Actions)

.github/workflows/ci-cd.yml

name: RAG CI/CD

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.10'
      - run: pip install -r requirements.txt
      - run: pytest tests/  # 运行安全/性能测试

  build-and-push:
    needs: test
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      - name: Build and push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: your-dockerhub/enterprise-rag:latest

✅ 实现:代码提交 → 自动测试 → 构建镜像 → 推送仓库


📝 五、动手实践 3:编写交付文档

docs/DEPLOY.md(节选)

# 企业 RAG 系统部署手册

## 环境要求
- Linux 服务器(推荐 Ubuntu 22.04)
- 32GB RAM(Qwen 7B 最低要求)
- Docker 24.0+, Docker Compose v2+

## 快速启动
```bash
git clone https://github.com/your-team/enterprise-rag.git
cd enterprise-rag
docker-compose up -d

访问服务

  • API: http://:8000/docs
  • Grafana: http://:3000 (admin/admin123)
  • Milvus Attu: http://:30001

初始化知识库

curl -X POST http://localhost:8000/ingest \
  -F "file=@company_policy.pdf" \
  -H "Authorization: Bearer YOUR_API_KEY"

> ✅ 包含:**架构图、部署步骤、API 示例、故障排查**

---

## 🔐 六、安全加固(交付前必做)

| 项 | 措施 |
|----|------|
| **API 认证** | 添加 JWT 或 API Key 验证 |
| **镜像扫描** | `docker scan your-image` |
| **敏感信息** | 用 `.env` + `docker-compose.override.yml` 管理密钥 |
| **最小权限** | 容器以非 root 用户运行 |
| **日志脱敏** | 确保审计日志不记录原始 query(如含身份证) |

> 💡 在 `Dockerfile` 中添加:
> ```dockerfile
> USER 1001
> ```

---

## 🚀 七、一键部署脚本(给运维)

```bash
#!/bin/bash
# deploy.sh
echo "🚀 开始部署企业 RAG 系统..."

# 1. 检查依赖
if ! command -v docker &> /dev/null; then
    echo "❌ 请先安装 Docker"
    exit 1
fi

# 2. 拉取代码
git clone https://github.com/your-team/enterprise-rag.git
cd enterprise-rag

# 3. 启动服务
docker-compose up -d

# 4. 验证
sleep 30
if curl -s http://localhost:8000/health | grep "ok"; then
    echo "✅ 部署成功!访问 http://$(hostname -I | awk '{print $1}'):8000"
else
    echo "❌ 部署失败,请检查日志"
    docker-compose logs rag-app
fi

✅ 运维只需执行 ./deploy.sh,无需懂细节


🏁 八、30 天总结与展望

我们做到了:

  • ✅ 从零构建 安全、高性能、可监控 的 RAG 系统
  • ✅ 掌握 LangChain 核心组件:Document Loaders, Retrievers, Chains, Agents
  • ✅ 实践 企业级工程能力:容器化、CI/CD、文档、测试
  • ✅ 形成 完整方法论:开发 → 评估 → 安全 → 优化 → 监控 → 交付

下一步建议:

  • 🌐 支持多模态(图片/PPT)
  • 🤖 接入企业微信/钉钉机器人
  • 📊 构建用户反馈闭环(点赞/纠错)
  • ☁️ 迁移到 Kubernetes(K8s)

AI 不是未来,而是现在。而你,已是这场变革的构建者。

🌟 系列完结,但探索不止。愿你在 AI 工程化的道路上,继续创造价值!