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.",
]
让我们看看如何使用嵌入模型 API 来嵌入文档以进行检索。
以下示例展示了如何使用 models/embedding-001 和 retrieval_document 任务类型嵌入文档
嵌入文档
result = mistral_client.embeddings(
model="mistral-embed",
input=texts,
)
返回结果有一个带有键:embedding 的数据字段。这个键的值是一个浮点数列表,表示文档的嵌入。
将其转换为 Qdrant 点
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 嵌入模型与二进制量化一起使用
您可以将 Mistral 嵌入模型与二进制量化一起使用——这是一种可以将嵌入大小减少 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 嵌入模型与 Qdrant 一起使用了!