向量数据库 Chroma 和 Milvus的使用

一、什么是向量数据库?

向量数据库(Vector Database)是专门用来存储和检索向量数据的数据库。它广泛应用于图像搜索、推荐系统、自然语言处理等领域。

简单理解:

  • 你给数据库一堆「特征向量」(比如图片、文本的数字表达)
  • 你问数据库「最像这个向量的有哪些?」
  • 数据库快速返回「最相似」的结果

二、Chroma 和 Milvus 简介

名称 特点 语言支持 适用场景
Chroma 轻量级、Python友好、易上手 Python 小项目、原型、快速开发
Milvus 企业级、高性能、支持多种部署方案 多语言(Python、Go等) 大规模、高并发、复杂场景

三、环境准备

  • 操作系统:Windows / Mac / Linux 都可以
  • Python 版本:3.7 及以上
  • 安装包管理器:pip

四、安装与配置

1 、安装 Chroma

直接安装Python库

bash 复制代码
pip install chromadb

2 、安装 Milvus

Milvus 分为两个部分:

  • Milvus Server(核心数据库服务,需单独安装或用Docker运行)
  • Milvus Python SDK(客户端,方便Python调用)

2.1、使用官方推荐脚本(最省心)

Milvus 官方提供的脚本会自动启用嵌入式 etcd 并正确配置启动:.

bash 复制代码
curl -sfL https://raw.githubusercontent.com/milvus-io/milvus/master/scripts/standalone_embed.sh -o standalone_embed.sh
bash standalone_embed.sh start

2.2、验证安装

启动后查看容器状态:

plain 复制代码
docker ps

应显示 milvus_standalone 正常运行

查看日志确认 embedded etcd 启动成功,无连接错误:

plain 复制代码
docker logs milvus_standalone

启动日志无报错

测试连接端口:

plain 复制代码
nc -zv localhost 19530

成功连接表示 Milvus 已正常监听端口。

2.3、安装 Milvus Python SDK

bash 复制代码
pip install pymilvus

五、使用示例

1、Chroma 简单示例

python 复制代码
import chromadb

# 创建客户端 - 使用新的配置方式
client = chromadb.PersistentClient(path=".chromadb/")

# 创建/获取集合 - 使用 get_or_create_collection 避免重复创建错误
collection = client.get_or_create_collection("test_collection")

# 插入向量数据
collection.add(
    documents=["苹果", "香蕉", "橘子"],  # 文本描述
    embeddings=[[0.1, 0.2, 0.3], [0.2, 0.1, 0.4], [0.15, 0.22, 0.35]],  # 对应向量(示例)
    ids=["1", "2", "3"]
)

# 查询最相似向量
results = collection.query(
    query_embeddings=[[0.1, 0.2, 0.31]],
    n_results=1
)

print(results)

返回结果

说明

  • documents 是你给数据库的文本
  • embeddings 是文本的向量表示(通常由模型生成)
  • 查询时传入一个向量,返回最接近的n个结果

2 、Milvus 简单示例

python 复制代码
from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection

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

# 定义集合结构
fields = [
    FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=False),
    FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=3)
]

schema = CollectionSchema(fields, "test collection")

# 创建集合
collection = Collection("test_collection", schema)

# 插入数据
ids = [1, 2, 3]
embeddings = [
    [0.1, 0.2, 0.3],
    [0.2, 0.1, 0.4],
    [0.15, 0.22, 0.35]
]

collection.insert([ids, embeddings])

# 创建索引
index_params = {
    "index_type": "IVF_FLAT",
    "params": {"nlist": 10},
    "metric_type": "L2"
}
collection.create_index("embedding", index_params)

# 加载集合
collection.load()

# 查询向量
search_params = {"metric_type": "L2", "params": {"nprobe": 10}}
results = collection.search([[0.1, 0.2, 0.31]], "embedding", search_params, limit=2)

for result in results[0]:
    print(f"id: {result.id}, distance: {result.distance}")

运行结果

六、总结

功能 Chroma Milvus
安装 纯Python库,简单快速 需要运行服务,推荐Docker部署
适合项目规模 小型、开发测试 大规模、生产环境
语言支持 Python优先 多语言支持
性能 适中 高性能,支持分布式