Model2Vec
Model2Vec是一種輕量且高效能的嵌入技術,可將 Sentence Transformer 模型轉換為精簡的靜態模型。它可將模型大小減少達 50 倍,推論速度加快達 500 倍,且效能損失極小。Model2Vec 是資源有限的裝置的理想選擇。
Milvus 透過Model2VecEmbeddingFunction類與Model2Vec的模型整合。這個類別提供了使用預先訓練的 Model2Vec 模型來編碼文件和查詢的方法,並將嵌入回傳成與 Milvus 索引相容的密集向量。
它支援從 Hugging Face Hub 載入模型和上傳本機 Model2Vec 模型,提供在各種環境中部署的彈性。
要使用此功能,請安裝必要的相依性:
pip install --upgrade pymilvus
pip install "pymilvus[model]"
然後,實體化Model2VecEmbeddingFunction:
from pymilvus import model
model2vec_ef = model.dense.Model2VecEmbeddingFunction(
model_source='minishlab/potion-base-8M', # or local directory
)
參數:
model_source(string)
指定用於產生 embeddings 的 Model2Vec 模型來源。它支援兩種載入模型的方法:
從 Hugging Face Hub 載入 (建議):
- 以字串形式提供模型名稱 (例如:
"minishlab/potion-base-8M")。 - 模型選項如下:
minishlab/potion-base-8M(預設)minishlab/potion-base-4Mminishlab/potion-base-2Mminishlab/potion-base-32Mminishlab/potion-retrieval-32M
- 以字串形式提供模型名稱 (例如:
本地載入:
- 提供儲存 Model2Vec 模型的本機檔案路徑 (例如:
"/path/to/local/model")。
- 提供儲存 Model2Vec 模型的本機檔案路徑 (例如:
要為文件建立內嵌,請使用encode_documents()方法:
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.",
]
docs_embeddings = model2vec_ef.encode_documents(docs)
# Print embeddings
print("Embeddings:", docs_embeddings)
# Print dimension and shape of embeddings
print("Dim:", model2vec_ef.dim, docs_embeddings[0].shape)
預期的輸出與下面相似:
Embeddings: [array([ 0.02220882, 0.11436888, -0.15094341, 0.08149259, 0.20425692,
-0.15727402, -0.25320682, -0.00669029, 0.03157463, 0.08974048,
-0.00148778, -0.01803541, 0.00230828, -0.0137875 , -0.19242321,
...
-7.29782460e-03, -2.15345751e-02, -4.13905866e-02, 3.70773636e-02,
5.45082428e-02, 1.36436718e-02, 1.38598625e-02, 3.91175086e-03],
dtype=float32)]
Dim: 256 (256,)
若要為查詢建立內嵌資料,請使用encode_queries()方法:
queries = ["When was artificial intelligence founded",
"Where was Alan Turing born?"]
query_embeddings = model2vec_ef.encode_queries(queries)
# Print embeddings
print("Embeddings:", query_embeddings)
# Print dimension and shape of embeddings
print("Dim", model2vec_ef.dim, query_embeddings[0].shape)
預期的輸出與下列內容相似:
Embeddings: [array([-1.87109038e-02, -2.81724217e-03, -1.67356253e-01, -5.30372337e-02,
1.08304240e-01, -1.09269567e-01, -2.53464818e-01, -1.77880954e-02,
3.05427872e-02, 1.68244764e-01, -7.25950347e-03, -2.52178032e-02,
...
8.60440824e-03, 2.12906860e-03, 1.50156394e-02, -1.29304864e-02,
-3.66544276e-02, 5.01735881e-03, -1.53137008e-02, 9.57900891e-04],
dtype=float32)]
Dim 256 (256,)