🚀 免費嘗試 Zilliz Cloud,完全托管的 Milvus,體驗速度提升 10 倍!立即嘗試

milvus-logo
LFAI
主頁
  • 開始使用
    • 安裝 Milvus
  • Home
  • Docs
  • 開始使用

  • 安裝 Milvus

  • 運行 Milvus Lite

本地運行 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 StandaloneMilvus 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,例如LangChainLlamaIndex

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_nameY
dimensionY
primary_field_nameY
id_typeY
vector_field_nameY
metric_typeY
auto_idY
schemaY
index_paramsY
enable_dynamic_fieldY
num_shardsN
partition_key_fieldN
num_partitionsN
consistency_levelN (僅支援Strong; 任何設定都會被視為Strong.)
get_collection_stats()支援取得集合統計資料。
collection_nameY
timeoutY
describe_collection()num_shardsconsistency_levelcollection_id 回應無效。
timeoutY
has_collection()支援檢查集合是否存在。
collection_nameY
timeoutY
list_collections()支援列出所有的集合。
drop_collection()支援丟棄一個集合。
collection_nameY
timeoutY
rename_collection()不支援重新命名集合。

欄位與模式

方法/參數Milvus Lite 支援
create_schema()支援有限的參數
auto_idY
enable_dynamic_fieldY
primary_fieldY
partition_key_fieldN
add_field()支援有限的參數
field_nameY
datatypeY
is_primaryY
max_lengthY
element_typeY
max_capacityY
dimY
is_partition_keyN
方法/參數Milvus Lite 支援
搜尋()支援有限的參數
collection_nameY
dataY
filterY
limitY
output_fieldsY
search_paramsY
timeoutY
partition_namesN
anns_fieldY
查詢()支援有限參數
collection_nameY
filterY
output_fieldsY
timeoutY
idsY
partition_namesN
獲取()支援有限參數
collection_nameY
idsY
output_fieldsY
timeoutY
partition_namesN
刪除()支援有限參數
collection_nameY
idsY
timeoutY
filterY
partition_nameN
插入()支援有限參數
collection_nameY
dataY
timeoutY
partition_nameN
upsert()支援有限參數
collection_nameY
dataY
timeoutY
partition_nameN

載入與釋放

方法/參數Milvus Lite 支援
load_collection()Y
collection_nameY
timeoutY
release_collection()Y
collection_nameY
timeoutY
get_load_state()不支援取得載入狀態。
refresh_load()不支援載入已載入集合的未載入資料。
close()Y

索引

方法/參數Milvus Lite 支援
list_indexes()支援列出索引。
collection_nameY
field_nameY
create_index()僅支援FLAT 索引類型。
index_paramsY
timeoutY
drop_index()支援丟失索引。
collection_nameY
index_nameY
timeoutY
describe_index()支援描述索引。
collection_nameY
index_nameY
timeoutY

向量索引類型

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 檔案,並將其匯入MilvusZilliz 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

Zilliz Cloud 無縫接入,由 Milvus 提供動力,速度提升 10 倍。

開始使用
反饋

這個頁面有幫助嗎?