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

Mistral AI

Mistral AI Embedding 模型用于将文本输入转换为稠密数值向量,从而有效捕捉文本的语义。这些模型针对语义搜索、自然语言理解和上下文感知应用等任务进行了优化,适用于多种 AI 应用场景。

Milvus 通过 MistralAIEmbeddingFunction 类集成 Mistral AI Embedding 模型。该类提供文档和查询编码方法,并以与 Milvus 索引兼容的稠密向量形式返回 Embedding。要使用此功能,请先从 Mistral AI 获取 API key。

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

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

然后,实例化 MistralAIEmbeddingFunction:

Python
from pymilvus.model.dense import MistralAIEmbeddingFunction

ef = MistralAIEmbeddingFunction(
model_name="mistral-embed", # Defaults to `mistral-embed`
api_key="MISTRAL_API_KEY" # Provide your Mistral AI API key
)

参数

  • model_name字符串

用于编码的 Mistral AI Embedding 模型名称。默认值为 mistral-embed。有关详细信息,请参阅 Embedding 文档

  • api_key字符串

用于访问 Mistral AI API 的 API key。

要为文档生成 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([-0.06051636, 0.03207397, 0.04684448, ..., -0.01618958,
0.02442932, -0.01302338]), array([-0.04675293, 0.06512451, 0.04290771, ..., -0.01454926,
0.0014801 , 0.00686646]), array([-0.05978394, 0.08728027, 0.02217102, ..., -0.00681305,
0.03634644, -0.01802063])]
Dim: 1024 (1024,)

要为查询生成 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([-0.04916382, 0.04568481, 0.03594971, ..., -0.02653503,
0.02804565, 0.00600815]), array([-0.05938721, 0.07098389, 0.01773071, ..., -0.01708984,
0.03582764, 0.00366592])]
Dim 1024 (1024,)