Mistral
Qdrant 兼容新发布的 Mistral Embed 及其官方 Python SDK,可以像安装其他任何包一样安装
设置
安装客户端
pip install mistralai
然后我们进行设置
from mistralai.client import MistralClient
from qdrant_client import QdrantClient
from qdrant_client.models import PointStruct, VectorParams, Distance
collection_name = "example_collection"
MISTRAL_API_KEY = "your_mistral_api_key"
client = QdrantClient(":memory:")
mistral_client = MistralClient(api_key=MISTRAL_API_KEY)
texts = [
"Qdrant is the best vector search engine!",
"Loved by Enterprises and everyone building for low latency, high performance, and scale.",
]
让我们看看如何使用 Embedding Model API 来嵌入文档以便检索。
以下示例展示了如何使用 models/embedding-001
和 retrieval_document
任务类型来嵌入文档
嵌入文档
result = mistral_client.embeddings(
model="mistral-embed",
input=texts,
)
返回结果包含一个 data 字段,其中有一个键:embedding
。此键的值是一个浮点数列表,表示文档的嵌入向量。
将其转换为 Qdrant Points
points = [
PointStruct(
id=idx,
vector=response.embedding,
payload={"text": text},
)
for idx, (response, text) in enumerate(zip(result.data, texts))
]
创建集合并插入文档
client.create_collection(collection_name, vectors_config=VectorParams(
size=1024,
distance=Distance.COSINE,
)
)
client.upsert(collection_name, points)
使用 Qdrant 搜索文档
文档索引完成后,您可以使用相同的模型和 retrieval_query
任务类型来搜索最相关的文档
client.search(
collection_name=collection_name,
query_vector=mistral_client.embeddings(
model="mistral-embed", input=["What is the best to use for vector search scaling?"]
).data[0].embedding,
)
将 Mistral Embedding 模型与二进制量化结合使用
您可以将 Mistral Embedding 模型与二进制量化结合使用 - 这是一种可以将嵌入向量的大小减少 32 倍,同时不过多损失搜索结果质量的技术。
在过采样率为 3、限制为 100 的情况下,启用重排后,我们对精确最近邻的召回率可达 95%。
过采样 | 1 | 1 | 2 | 2 | 3 | 3 | |
---|---|---|---|---|---|---|---|
重排 | 否 | 是 | 否 | 是 | 否 | 是 | |
限制 | |||||||
10 | 0.53444 | 0.857778 | 0.534444 | 0.918889 | 0.533333 | 0.941111 | |
20 | 0.508333 | 0.837778 | 0.508333 | 0.903889 | 0.508333 | 0.927778 | |
50 | 0.492222 | 0.834444 | 0.492222 | 0.903556 | 0.492889 | 0.940889 | |
100 | 0.499111 | 0.845444 | 0.498556 | 0.918333 | 0.497667 | 0.944556 |
就是这样!您现在可以将 Mistral Embedding 模型与 Qdrant 结合使用了!