时间: 10 分钟级别: 初学者Open In Colab

双子座

Qdrant 兼容 Gemini 嵌入模型 API 及其官方 Python SDK,可以像安装其他包一样安装:

Gemini 是 Google PaLM 模型的新系列,于 2023 年 12 月发布。新的嵌入模型取代了之前的 Gecko 嵌入模型。

在最新的模型中,可以向API调用传递一个额外的参数task_type。此参数用于指定所使用的嵌入的预期目的。

嵌入模型API支持各种任务类型,概述如下:

  1. retrieval_query: query in a search/retrieval setting
  2. retrieval_document: document from the corpus being searched
  3. semantic_similarity: semantic text similarity
  4. classification: embeddings to be used for text classification
  5. clustering: the generated embeddings will be used for clustering
  6. task_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%。

过采样112233
重新评分FalseTrueFalseTrueFalseTrue
限制
100.5233330.8311110.5233330.9155560.5233330.950000
200.5100000.8366670.5100000.9122220.5100000.937778
500.4891110.8415560.4891110.9133330.4884440.947111
1000.4857780.8465560.4855560.9290000.4860000.956333

就是这样!您现在可以在Qdrant中使用Gemini嵌入模型了!

这个页面有用吗?

感谢您的反馈!🙏

我们很抱歉听到这个消息。😔 你可以在GitHub上编辑这个页面,或者创建一个GitHub问题。