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 | |
|---|---|---|---|---|---|---|---|
| 重新评分 | False | True | False | True | False | True | |
| 限制 | |||||||
| 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 |
就是这样!您现在可以在Qdrant中使用Mistral嵌入模型了!
