RAG 学习笔记:Milvus 多模态检索实战

RAGMilvus向量数据库多模态检索Docker

RAG 学习笔记:Milvus 多模态检索实战

从零开始掌握 Milvus 向量数据库的部署、核心组件和多模态检索实践,构建生产级向量检索系统。

目录


一、为什么选择 Milvus?

核心原因

原因说明影响
生产级设计云原生架构,高可用、高性能适合生产环境部署
大规模支持处理十亿、百亿级向量数据满足企业级需求
多模态能力支持文本、图像等多模态检索扩展应用场景
开源生态活跃社区,丰富文档降低学习成本

Milvus vs 其他方案

特性MilvusFAISSChromaDB
架构分布式数据库算法库轻量级数据库
规模十亿级+百万级百万级
部署Docker/K8s本地文件本地文件
适用场景生产环境原型开发小型应用

二、Milvus 部署安装

2.1 环境准备

前置要求

  • Docker 和 Docker Compose 已安装并运行
  • 至少 4GB 可用内存
  • 网络连接正常

2.2 部署步骤

步骤 1:下载配置文件

# macOS / Linux (使用 wget)
wget https://github.com/milvus-io/milvus/releases/download/v2.5.14/milvus-standalone-docker-compose.yml -O docker-compose.yml

# Windows (使用 PowerShell)
Invoke-WebRequest -Uri "https://github.com/milvus-io/milvus/releases/download/v2.5.14/milvus-standalone-docker-compose.yml" -OutFile "docker-compose.yml"

步骤 2:启动 Milvus 服务

docker compose up -d

步骤 3:验证安装

# 查看容器状态
docker ps

# 确认三个容器运行中:
# - milvus-standalone
# - milvus-minio
# - milvus-etcd

2.3 常用管理命令

命令功能说明
docker compose up -d启动服务后台运行
docker compose down停止服务删除容器
docker compose logs -f查看日志实时日志
docker compose ps查看状态容器状态

三、核心组件

3.1 Milvus 架构

┌─────────────────────────────────────┐
│           客户端 SDK                │
│  Python / Java / Go / RESTful API  │
└─────────────────────────────────────┘
┌─────────────────────────────────────┐
│           Milvus 服务               │
│  ├── Proxy(接入层)               │
│  ├── Coordinator(协调层)         │
│  ├── Query Node(查询节点)        │
│  ├── Data Node(数据节点)         │
│  └── Index Node(索引节点)        │
└─────────────────────────────────────┘
┌─────────────────────────────────────┐
│           存储层                    │
│  ├── etcd(元数据)                │
│  ├── MinIO(对象存储)             │
│  └── Pulsar(消息队列)            │
└─────────────────────────────────────┘

3.2 核心概念

概念说明类比
Collection向量集合数据库表
Partition分区表分区
Index向量索引B-Tree 索引
Entity数据实体行记录
Field字段

四、多模态检索实践

4.1 安装 Python SDK

pip install pymilvus

4.2 连接 Milvus

from pymilvus import connections, Collection, FieldSchema, CollectionSchema, DataType

# 连接 Milvus
connections.connect("default", host="localhost", port="19530")

4.3 创建 Collection

# 定义字段
fields = [
    FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=True),
    FieldSchema(name="text", dtype=DataType.VARCHAR, max_length=65535),
    FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=768),
]

# 创建 Schema
schema = CollectionSchema(fields, description="多模态检索示例")

# 创建 Collection
collection = Collection("multimodal_search", schema)

4.4 插入数据

import numpy as np

# 准备数据
texts = ["这是一段文本", "这是另一段文本", "这是第三段文本"]
embeddings = np.random.random((3, 768)).tolist()

# 插入数据
collection.insert([texts, embeddings])

# 刷新数据到存储
collection.flush()

4.5 创建索引

# 创建 HNSW 索引
index_params = {
    "metric_type": "L2",
    "index_type": "HNSW",
    "params": {"M": 16, "efConstruction": 200},
}

collection.create_index("embedding", index_params)

4.6 搜索

# 加载 Collection 到内存
collection.load()

# 准备查询向量
query_vector = np.random.random((1, 768)).tolist()

# 搜索
search_params = {"metric_type": "L2", "params": {"ef": 100}}
results = collection.search(
    data=query_vector,
    anns_field="embedding",
    param=search_params,
    limit=5,
    output_fields=["text"]
)

# 打印结果
for hits in results:
    for hit in hits:
        print(f"ID: {hit.id}, Distance: {hit.distance}, Text: {hit.entity.get('text')}")

五、选型建议

5.1 决策树

数据量有多大?
├─ < 100 万
│  └─ 用 FAISS(内存索引,部署简单)
├─ 100 万 - 1000 万
│  ├─ 需要持久化?→ Chroma / Qdrant
│  └─ 不需要?→ FAISS
└─ > 1000 万
   ├─ 需要分布式?→ Milvus
   └─ 单机够用?→ FAISS + 优化

5.2 最佳实践

实践说明重要性
选择合适的索引HNSW 适合高精度,IVF 适合大规模⭐⭐⭐⭐⭐
合理设置参数M、efConstruction 等参数影响性能⭐⭐⭐⭐
使用分区按业务逻辑分区提升查询效率⭐⭐⭐⭐
监控和调优定期监控性能,调整参数⭐⭐⭐

结语

Milvus 是生产环境向量数据库的首选,但大多数项目不需要它的分布式能力。建议读者根据实际数据量和业务需求选择合适的方案:小规模用 FAISS,中等规模用 Chroma/Qdrant,大规模用 Milvus。

关键要点:Milvus 是生产级向量数据库的首选,但大多数项目用 FAISS 就够了。根据数据量和业务需求选择,不要过度设计。