本地運行 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 操作、稀疏和密集向量搜尋、元資料過濾、多向量和混合搜尋。這兩項功能可在不同類型的環境中提供一致的體驗,從邊緣裝置到雲端集群,適合不同規模的使用個案。使用相同的客戶端程式碼,您可以在筆記型電腦或 Jupyter Notebook 上使用 Milvus Lite 執行 GenAI 應用程式,或在 Docker 容器上使用 Milvus Standalone 執行 GenAI 應用程式,或在大型 Kubernetes 叢集上使用 Milvus Distributed 執行 GenAI 應用程式,在生產中提供數十億向量的服務。
先決條件
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 或以上版本,您可以pip install
與-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 # The vectors we will use in this demo has 384 dimensions
)
# Text strings to search from.
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.",
]
# For illustration, here we use fake vectors with random numbers (384 dimension).
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
)
# This will exclude any text in "history" subject despite close to the query vector.
res = client.search(
collection_name="demo_collection",
data=[vectors[0]],
filter="subject == 'history'",
limit=2,
output_fields=["text", "subject"],
)
print(res)
# a query that retrieves all entities matching filter expressions.
res = client.query(
collection_name="demo_collection",
filter="subject == 'history'",
output_fields=["text", "subject"],
)
print(res)
# delete
res = client.delete(
collection_name="demo_collection",
filter="subject == 'history'",
)
print(res)
限制
當執行 Milvus Lite 時,請注意有些功能是不支援的。以下表格總結了 Milvus Lite 的使用限制。
收集
方法/參數 | 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_name | Y |
timeout | Y |
describe_collection() | num_shards 、consistency_level 及collection_id 回應無效。 |
timeout | Y |
has_collection() | 支援檢查集合是否存在。 |
collection_name | Y |
timeout | Y |
list_collections() | 支援列出所有的集合。 |
drop_collection() | 支援丟棄一個集合。 |
collection_name | Y |
timeout | Y |
rename_collection() | 不支援重新命名集合。 |
欄位與模式
方法/參數 | 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 |
插入與搜尋
方法/參數 | Milvus Lite 支援 |
---|---|
搜尋() | 支援有限的參數 |
collection_name | Y |
data | Y |
filter | Y |
limit | Y |
output_fields | Y |
search_params | Y |
timeout | Y |
partition_names | N |
anns_field | Y |
查詢() | 支援有限參數 |
collection_name | Y |
filter | Y |
output_fields | Y |
timeout | Y |
ids | Y |
partition_names | N |
獲取() | 支援有限參數 |
collection_name | Y |
ids | Y |
output_fields | Y |
timeout | Y |
partition_names | N |
刪除() | 支援有限參數 |
collection_name | Y |
ids | Y |
timeout | Y |
filter | Y |
partition_name | N |
插入() | 支援有限參數 |
collection_name | Y |
data | Y |
timeout | Y |
partition_name | N |
upsert() | 支援有限參數 |
collection_name | Y |
data | Y |
timeout | Y |
partition_name | N |
載入與釋放
方法/參數 | Milvus Lite 支援 |
---|---|
load_collection() | Y |
collection_name | Y |
timeout | Y |
release_collection() | Y |
collection_name | Y |
timeout | Y |
get_load_state() | 不支援取得載入狀態。 |
refresh_load() | 不支援載入已載入集合的未載入資料。 |
close() | Y |
索引
方法/參數 | 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索引類型。無論集合中指定的索引類型為何,它都使用 FLAT 類型。
搜尋功能
Milvus Lite 支援 Sparse Vector、Multi-vector、Hybrid Search。
分割
Milvus Lite 不支援分割和分割相關的方法。
使用者與角色
Milvus Lite 不支援使用者與角色及相關方法。
別名
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 套件一起安裝。
# Install
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 show this help message and exit
-d DB_FILE, --db-file DB_FILE
milvus lite db file
-c COLLECTION, --collection COLLECTION
collection that need to be dumped
-p PATH, --path PATH dump file storage dir
以下範例會將demo_collection
套件中儲存於./milvus_demo.db
(Milvus Lite 資料庫檔案) 的所有資料匯出。
匯出資料:
milvus-lite dump -d ./milvus_demo.db -c demo_collection -p ./data_dir
# ./milvus_demo.db: milvus lite db file
# demo_collection: collection that need to be dumped
#./data_dir : dump file storage dir
使用轉儲檔案,您可以透過資料匯入上傳資料到 Zilliz Cloud,或透過大量插入上傳資料到 Milvus 伺服器。
下一步
連接 Milvus Lite 後,您可以
查看快速入門,了解 Milvus 的功能。
學習 Milvus 的基本操作:
在雲端部署您的 Milvus 叢集:
探索Milvus 備份,Milvus 資料備份的開放原始碼工具。
探索Birdwatcher,用於調試 Milvus 和動態配置更新的開放源碼工具。
探索Attu,用於直覺式 Milvus 管理的開放原始碼 GUI 工具。