在 Qdrant 中使用 FastEmbed 进行向量搜索

安装 Qdrant 客户端和 FastEmbed

pip install "qdrant-client[fastembed]>=1.14.2"

初始化客户端

Qdrant 客户端提供了一种简单的内存模式,让您可以在本地尝试语义搜索。

from qdrant_client import QdrantClient, models

client = QdrantClient(":memory:")  # Qdrant is running from RAM.

添加数据

现在您可以添加两个示例文档、它们关联的元数据以及每个文档的点 id

docs = [
    "Qdrant has a LangChain integration for chatbots.",
    "Qdrant has a LlamaIndex integration for agents.",
]
metadata = [
    {"source": "langchain-docs"},
    {"source": "llamaindex-docs"},
]
ids = [42, 2]

创建一个集合

Qdrant 将向量和关联的元数据存储在集合(collection)中。在创建集合时,需要设置向量参数。在本教程中,我们将使用 BAAI/bge-small-en 来计算嵌入(embeddings)。

model_name = "BAAI/bge-small-en"
client.create_collection(
    collection_name="test_collection",
    vectors_config=models.VectorParams(
        size=client.get_embedding_size(model_name), 
        distance=models.Distance.COSINE
    ),  # size and distance are model dependent
)

将文档插入(Upsert)到集合中

通过 FastEmbed 集成,Qdrant 客户端可以在其方法内隐式执行推理。这需要使用 models.Document(如果您正在处理图像,则使用 models.Image)来封装您的数据。

metadata_with_docs = [
    {"document": doc, "source": meta["source"]} for doc, meta in zip(docs, metadata)
]
client.upload_collection(
    collection_name="test_collection",
    vectors=[models.Document(text=doc, model=model_name) for doc in docs],
    payload=metadata_with_docs,
    ids=ids,
)

在这里,您将输入一个虚拟问题,该问题将帮助您检索到语义相关的结果。

search_result = client.query_points(
    collection_name="test_collection",
    query=models.Document(
        text="Which integration is best for agents?", 
        model=model_name
    )
).points
print(search_result)

语义搜索引擎将按相关性顺序检索最相似的结果。在本例中,关于 LlamaIndex 的第二条陈述更相关。

[
    ScoredPoint(
        id=2, 
        score=0.87491801319731,
        payload={
            "document": "Qdrant has a LlamaIndex integration for agents.",
            "source": "llamaindex-docs",
        },
        ...
    ),
    ScoredPoint(
        id=42,
        score=0.8351846627714035,
        payload={
            "document": "Qdrant has a LangChain integration for chatbots.",
            "source": "langchain-docs",
        },
        ...
    ),
]
此页面有用吗?

感谢您的反馈!🙏

很遗憾听到这个消息。😔 您可以在 GitHub 上 编辑 此页面,或者 创建 一个 GitHub Issue。