本地运行 Milvus Lite
本页介绍如何使用 Milvus Lite 在本地运行 Milvus。Milvus Lite 是 Milvus 的轻量级版本,Milvus 是一个开源向量数据库,可通过向量嵌入和相似度搜索为 AI 应用提供支持。
概述
Milvus Lite 可以导入到您的 Python 应用程序中,提供 Milvus 的核心向量搜索功能。Milvus Lite 已包含在 Milvus 的 Python SDK 中,只需通过 pip install pymilvus
即可部署。
使用 Milvus Lite,您可以在几分钟内开始构建具有向量相似度搜索功能的 AI 应用!Milvus Lite 适合在以下环境中运行:
- Jupyter Notebook / Google Colab
- 笔记本电脑
- 边缘设备
Milvus Lite 与 Milvus Standalone 和 Distributed 共享相同的 API,并支持大多数功能,如向量数据持久化和管理、向量 CRUD 操作、Sparse Vector 和 Dense Vector 搜索、元数据过滤、Multi-vector 和 Hybrid Search。它们共同为不同类型的环境提供一致的体验,从边缘设备到云端集群,适应不同规模的使用场景。使用相同的客户端代码,您可以在笔记本电脑或 Jupyter Notebook 上用 Milvus Lite 运行 GenAI 应用,或在 Docker 容器上运行 Milvus Standalone,或在处理数十亿向量的大规模 Kubernetes 集群上运行 Milvus Distributed。
系统要求
Milvus Lite 目前支持以下环境:
- Ubuntu >= 20.04 (x86_64 和 arm64)
- MacOS >= 11.0 (Apple Silicon M1/M2 和 x86_64)
请注意,Milvus Lite 仅适用于小规模向量搜索场景。对于大规模使用场景,我们建议使用 Milvus Standalone 或 Milvus Distributed。您也可以考虑在 Zilliz Cloud 上使用完全托管的 Milvus。
设置 Milvus Lite
pip install -U pymilvus
我们推荐使用 pymilvus
。由于 milvus-lite
已包含在 pymilvus
2.4.2 或更高版本中,您可以使用 -U
参数强制更新到最新版本,milvus-lite
会自动安装。
如果您想明确安装 milvus-lite
包,或者已安装旧版本的 milvus-lite
并想更新它,可以执行 pip install -U milvus-lite
。
连接到 Milvus Lite
在 pymilvus
中,将本地文件名作为 MilvusClient 的 uri 参数即可使用 Milvus Lite。
from pymilvus import MilvusClient
client = MilvusClient("./milvus_demo.db")
运行上述代码后,将在当前文件夹生成名为 milvus_demo.db 的数据库文件。
注意: 相同的 API 也适用于 Milvus Standalone、Milvus Distributed 和 Zilliz Cloud,唯一的区别是将本地文件名替换为远程服务器端点和凭据,例如:
client = MilvusClient(uri="http://localhost:19530", token="username:password")
。
示例
以下是一个简单的演示,展示如何使用 Milvus Lite 进行文本搜索。还有更多全面的示例,展示如何使用 Milvus Lite 构建应用, 如 RAG、图像搜索,以及在流行的 RAG 框架中使用 Milvus Lite,如 LangChain 和 LlamaIndex!
from pymilvus import MilvusClient
import numpy as np
client = MilvusClient("./milvus_demo.db")
client.create_collection(
collection_name="demo_collection",
dimension=384 # 本示例中使用的向量维度为 384
)
# 待搜索的文本
docs = [
"Artificial intelligence was founded as an academic discipline in 1956.",
"Alan Turing was the first person to conduct substantial research in AI.",
"Born in Maida Vale, London, Turing was raised in southern England.",
]
# 为演示目的,这里使用随机数生成假向量(384维)
vectors = [[ np.random.uniform(-1, 1) for _ in range(384) ] for _ in range(len(docs)) ]
data = [ {"id": i, "vector": vectors[i], "text": docs[i], "subject": "history"} for i in range(len(vectors)) ]
res = client.insert(
collection_name="demo_collection",
data=data
)
# 只返回 subject 为 history 的文本
res = client.search(
collection_name="demo_collection",
data=[vectors[0]],
filter="subject == 'history'",
limit=2,
output_fields=["text", "subject"],
)
print(res)
# 查询所有匹配过滤表达式的实体
res = client.query(
collection_name="demo_collection",
filter="subject == 'history'",
output_fields=["text", "subject"],
)
print(res)
# 删除
res = client.delete(
collection_name="demo_collection",
filter="subject == 'history'",
)
print(res)
限制
运行 Milvus Lite 时,请注意某些功能不受支持。以下表格总结了 Milvus Lite 的使用限制。
Collection
方法 / 参数 | Milvus Lite 是否支持 |
---|---|
create_collection() | 支持,但参数有限 |
collection_name | Y |
dimension | Y |
primary_field_name | Y |
id_type | Y |
vector_field_name | Y |
metric_type | Y |
auto_id | Y |
schema | Y |
index_params | Y |
enable_dynamic_field | Y |
num_shards | N |
partition_key_field | N |
num_partitions | N |
consistency_level | N(仅支持 Strong ;任何配置都将被视为 Strong ) |
get_collection_stats() | 支持获取 Collection 统计信息 |
collection_name | Y |
timeout | Y |
describe_collection() | 响应中的 num_shards 、consistency_level 和 collection_id 无效 |
timeout | Y |
has_collection() | 支持检查 Collection 是否存在 |
collection_name | Y |
timeout | Y |
list_collections() | 支持列出所有 Collection |
drop_collection() | 支持删除 Collection |
collection_name | Y |
timeout | Y |
rename_collection() | 不支持重命名 Collection |
Field & Schema
方法 / 参数 | Milvus Lite 是否支持 |
---|---|
create_schema() | 支持,但参数有限 |
auto_id | Y |
enable_dynamic_field | Y |
primary_field | Y |
partition_key_field | N |
add_field() | 支持,但参数有限 |
field_name | Y |
datatype | Y |
is_primary | Y |
max_length | Y |
element_type | Y |
max_capacity | Y |
dim | Y |
is_partition_key | N |
Insert & Search
方法 / 参数 | Milvus Lite 是否支持 |
---|---|
search() | 支持,但参数有限 |
collection_name | Y |
data | Y |
filter | Y |
limit | Y |
output_fields | Y |
search_params | Y |
timeout | Y |
partition_names | N |
anns_field | Y |
query() | 支持,但参数有限 |
collection_name | Y |
filter | Y |
output_fields | Y |
timeout | Y |
ids | Y |
partition_names | N |
get() | 支持,但参数有限 |
collection_name | Y |
ids | Y |
output_fields | Y |
timeout | Y |
partition_names | N |
delete() | 支持,但参数有限 |
collection_name | Y |
ids | Y |
timeout | Y |
filter | Y |
partition_name | N |
insert() | 支持,但参数有限 |
collection_name | Y |
data | Y |
timeout | Y |
partition_name | N |
upsert() | 支持,但参数有限 |
collection_name | Y |
data | Y |
timeout | Y |
partition_name | N |
Load & Release
方法 / 参数 | Milvus Lite 是否支持 |
---|---|
load_collection() | Y |
collection_name | Y |
timeout | Y |
release_collection() | Y |
collection_name | Y |
timeout | Y |
get_load_state() | 不支持获取加载状态 |
refresh_load() | 不支持加载已加载 Collection 的未加载数据 |
close() | Y |
Index
方法 / 参数 | Milvus Lite 是否支持 |
---|---|
list_indexes() | 支持列出索引 |
collection_name | Y |
field_name | Y |
create_index() | 仅支持 FLAT 索引类型 |
index_params | Y |
timeout | Y |
drop_index() | 支持删除索引 |
collection_name | Y |
index_name | Y |
timeout | Y |
describe_index() | 支持描述索引 |
collection_name | Y |
index_name | Y |
timeout | Y |
向量索引类型
Milvus Lite 仅支持 FLAT 索引类型。无论在 Collection 中指定什么索引类型,它都会使用 FLAT 类型。
搜索功能
Milvus Lite 支持 Sparse Vector、Multi-vector、Hybrid Search。
Partition
Milvus Lite 不支持 Partition 和相关方法。
Users & Roles
Milvus Lite 不支持用户和角色及相关方法。
Alias
Milvus Lite 不支持别名和相关方法。
从 Milvus Lite 迁移数据
所有存储在 Milvus Lite 中的数据都可以轻松导出并加载到其他类型的 Milvus 部署中,如 Docker 上的 Milvus Standalone、K8s 上的 Milvus Distributed,或 Zilliz Cloud 上的完全托管 Milvus。
Milvus Lite 提供了一个命令行工具,可以将数据导出到 json 文件中,该文件可以导入到 milvus 和 Zilliz Cloud(Milvus 的完全托管云服务)。milvus-lite 命令会随 milvus-lite python 包一起安装。
# 安装
pip install -U "pymilvus[bulk_writer]"
milvus-lite dump -h
usage: milvus-lite dump [-h] [-d DB_FILE] [-c COLLECTION] [-p PATH]
optional arguments:
-h, --help 显示帮助信息并退出
-d DB_FILE, --db-file DB_FILE
milvus lite 数据库文件
-c COLLECTION, --collection COLLECTION
需要导出的 collection
-p PATH, --path PATH 导出文件存储目录
以下示例从 ./milvus_demo.db
(Milvus Lite 数据库文件)中导出 demo_collection
Collection 的所有数据:
导出数据:
milvus-lite dump -d ./milvus_demo.db -c demo_collection -p ./data_dir
# ./milvus_demo.db: milvus lite 数据库文件
# demo_collection: 需要导出的 collection
#./data_dir : 导出文件存储目录
有了导出文件,您可以通过 Data Import 将数据上传到 Zilliz Cloud,或通过 Bulk Insert 将数据上传到 Milvus 服务器。
下一步
连接到 Milvus Lite 后,您可以:
-
查看快速入门了解 Milvus 的功能。
-
学习 Milvus 的基本操作:
-
在云上部署 Milvus 集群:
-
探索 Milvus Backup,一个用于 Milvus 数据备份的开源工具
-
探索 Birdwatcher,一个用于调试 Milvus 和动态配置更新的开源工具
-
探索 Attu,一个用于直观管理 Milvus 的开源 GUI 工具