双子座
Qdrant 兼容 Gemini 嵌入模型 API 及其官方 Python SDK,可以像安装其他包一样安装:
Gemini 是 Google PaLM 模型的新系列,于 2023 年 12 月发布。新的嵌入模型取代了之前的 Gecko 嵌入模型。
在最新的模型中,可以向API调用传递一个额外的参数task_type。此参数用于指定所使用的嵌入的预期目的。
嵌入模型API支持各种任务类型,概述如下:
retrieval_query: query in a search/retrieval settingretrieval_document: document from the corpus being searchedsemantic_similarity: semantic text similarityclassification: embeddings to be used for text classificationclustering: the generated embeddings will be used for clusteringtask_type_unspecified: Unset value, which will default to one of the other values.
如果您正在构建一个语义搜索应用程序,例如RAG,您应该为索引文档使用task_type="retrieval_document",并为搜索查询使用task_type="retrieval_query"。
以下示例展示了如何使用 Qdrant 实现这一点:
设置
pip install google-generativeai
让我们看看如何使用嵌入模型API来嵌入文档以进行检索。
以下示例展示了如何使用models/embedding-001嵌入文档,任务类型为retrieval_document:
嵌入文档
import google.generativeai as gemini_client
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, PointStruct, VectorParams
collection_name = "example_collection"
GEMINI_API_KEY = "YOUR GEMINI API KEY" # add your key here
client = QdrantClient(url="http://localhost:6333")
gemini_client.configure(api_key=GEMINI_API_KEY)
texts = [
"Qdrant is a vector database that is compatible with Gemini.",
"Gemini is a new family of Google PaLM models, released in December 2023.",
]
results = [
gemini_client.embed_content(
model="models/embedding-001",
content=sentence,
task_type="retrieval_document",
title="Qdrant x Gemini",
)
for sentence in texts
]
使用Qdrant创建Qdrant点和索引文档
创建Qdrant点
points = [
PointStruct(
id=idx,
vector=response['embedding'],
payload={"text": text},
)
for idx, (response, text) in enumerate(zip(results, texts))
]
创建集合
client.create_collection(collection_name, vectors_config=
VectorParams(
size=768,
distance=Distance.COSINE,
)
)
将这些添加到集合中
client.upsert(collection_name, points)
使用Qdrant搜索文档
一旦文档被索引,你可以使用相同的模型和retrieval_query任务类型来搜索最相关的文档:
client.search(
collection_name=collection_name,
query_vector=gemini_client.embed_content(
model="models/embedding-001",
content="Is Qdrant compatible with Gemini?",
task_type="retrieval_query",
)["embedding"],
)
使用Gemini嵌入模型与二进制量化
您可以将Gemini嵌入模型与二进制量化结合使用——这是一种技术,可以让您将嵌入的大小减少32倍,而不会过多地影响搜索结果的质量。
在此表中,您可以看到使用models/embedding-001模型进行二进制量化搜索的结果与原始模型的比较:
在过采样为3且限制为100的情况下,启用重新评分后,我们对于精确最近邻的召回率为95%。
| 过采样 | 1 | 1 | 2 | 2 | 3 | 3 | |
|---|---|---|---|---|---|---|---|
| 重新评分 | False | True | False | True | False | True | |
| 限制 | |||||||
| 10 | 0.523333 | 0.831111 | 0.523333 | 0.915556 | 0.523333 | 0.950000 | |
| 20 | 0.510000 | 0.836667 | 0.510000 | 0.912222 | 0.510000 | 0.937778 | |
| 50 | 0.489111 | 0.841556 | 0.489111 | 0.913333 | 0.488444 | 0.947111 | |
| 100 | 0.485778 | 0.846556 | 0.485556 | 0.929000 | 0.486000 | 0.956333 |
就是这样!您现在可以在Qdrant中使用Gemini嵌入模型了!
