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', # 或本地目录
)
参数:
-
model_source (string)
指定用于生成嵌入的 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:", docs_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:", query_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,)