跳到主要内容
版本:v2.4.x

Instructor

Instructor 是一种经过指令微调的文本 Embedding 模型。只需提供任务指令,无需额外微调,它就能生成适用于不同任务(例如分类、检索、聚类和文本评估)及不同领域(例如科学和金融)的文本 Embedding。

Milvus 通过 InstructorEmbeddingFunction 类集成 Instructor Embedding 模型。该类提供文档和查询编码方法,并以与 Milvus 索引兼容的稠密向量形式返回 Embedding。

要使用此功能,请安装所需依赖:

Shell
pip install --upgrade pymilvus
pip install "pymilvus[model]"

然后,实例化 InstructorEmbeddingFunction:

Python
from pymilvus.model.dense import InstructorEmbeddingFunction

ef = InstructorEmbeddingFunction(
model_name="hkunlp/instructor-xl", # Defaults to `hkunlp/instructor-xl`
query_instruction="Represent the question for retrieval:",
doc_instruction="Represent the document for retrieval:"
)

参数

  • model_name字符串

用于编码的 Instructor Embedding 模型名称。默认值为 hkunlp/instructor-xl。有关可用模型的详细信息,请参阅 模型列表

  • query_instruction字符串

面向特定任务的查询指令,用于指导模型为查询或问题生成 Embedding。

  • doc_instruction字符串

面向特定任务的文档指令,用于指导模型为文档生成 Embedding。

要为文档生成 Embedding,请使用 encode_documents() 方法:

Python
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 = ef.encode_documents(docs)

# Print embeddings
print("Embeddings:", docs_embeddings)
# Print dimension and shape of embeddings
print("Dim:", ef.dim, docs_embeddings[0].shape)

预期输出类似如下:

Text
Embeddings: [array([ 1.08575663e-02, 3.87877878e-03, 3.18090729e-02, -8.12458917e-02,
-4.68971021e-02, -5.85585833e-02, -5.95418774e-02, -8.55880603e-03,
-5.54775111e-02, -6.08020350e-02, 1.76202394e-02, 1.06648318e-02,
-5.89960292e-02, -7.46861771e-02, 6.60329172e-03, -4.25189249e-02,
...
-1.26921125e-02, 3.01475357e-02, 8.25323071e-03, -1.88470203e-02,
6.04814291e-03, -2.81618331e-02, 5.91602828e-03, 7.13866428e-02],
dtype=float32)]
Dim: 768 (768,)

要为查询生成 Embedding,请使用 encode_queries() 方法:

Python
queries = ["When was artificial intelligence founded",
"Where was Alan Turing born?"]

query_embeddings = ef.encode_queries(queries)

print("Embeddings:", query_embeddings)
print("Dim", ef.dim, query_embeddings[0].shape)

预期输出类似如下:

Text
Embeddings: [array([ 1.21721877e-02, 1.88485277e-03, 3.01732980e-02, -8.10302645e-02,
-6.13401756e-02, -3.98149453e-02, -5.18723316e-02, -6.76784338e-03,
-6.59285188e-02, -5.38365729e-02, -5.13435388e-03, -2.49210224e-02,
-5.74403182e-02, -7.03031123e-02, 6.63730130e-03, -3.42259370e-02,
...
7.36595877e-03, 2.85532661e-02, -1.55952033e-02, 2.13342719e-02,
1.51187545e-02, -2.82798670e-02, 2.69396193e-02, 6.16136603e-02],
dtype=float32)]
Dim 768 (768,)