Qdrant

以下是所有可用 Qdrant 过程的列表,请注意,过程列表和签名与其他过程(如 ChromaDB 过程)一致。

名称 描述

apoc.vectordb.qdrant.createCollection(hostOrKey, collection, similarity, size, $config)

创建集合,集合名称在第二个参数中指定,并具有指定的 similaritysize。默认端点为 <hostOrKey 参数>/collections/<collection 参数>

apoc.vectordb.qdrant.deleteCollection(hostOrKey, collection, $config)

删除第二个参数中指定的名称的集合。默认端点为 <hostOrKey 参数>/collections/<collection 参数>

apoc.vectordb.qdrant.upsert(hostOrKey, collection, vectors, $config)

在第二个参数中指定的名称的集合中更新或插入向量 [{id: 'id', vector: '<vectorDb>', medatada: '<metadata>'}]. 默认端点为 <hostOrKey 参数>/collections/<collection 参数>/points

apoc.vectordb.qdrant.delete(hostOrKey, collection, ids, $config)

删除指定 ids 的向量。默认端点为 <hostOrKey 参数>/collections/<collection 参数>/points/delete

apoc.vectordb.qdrant.get(hostOrKey, collection, ids, $config)

获取指定 ids 的向量。默认端点为 <hostOrKey 参数>/collections/<collection 参数>/points

apoc.vectordb.qdrant.getAndUpdate(hostOrKey, collection, ids, $config)

获取指定ids的向量,并可选择创建/更新neo4j实体。默认端点为<hostOrKey 参数>/collections/<collection 参数>/points

apoc.vectordb.qdrant.query(hostOrKey, collection, vector, filter, limit, $config)

从定义的vector中检索最接近的向量,limit个结果,在第二个参数中指定的名称的集合中。默认端点为<hostOrKey 参数>/collections/<collection 参数>/points/search

apoc.vectordb.qdrant.queryAndUpdate(hostOrKey, collection, vector, filter, limit, $config)

从定义的vector中检索最接近的向量,limit个结果,在第二个参数中指定的名称的集合中,并可选择创建/更新neo4j实体。默认端点为<hostOrKey 参数>/collections/<collection 参数>/points/search

其中第一个参数可以是apoc配置apoc.qdrant.<key>.host=myHost定义的键。当hostOrKey=null时,默认为'http://localhost:6333'。

示例

创建集合(它利用了此API
CALL apoc.vectordb.qdrant.createCollection($hostOrKey, 'test_collection', 'Cosine', 4, {<optional config>})
删除集合(它利用了此API
CALL apoc.vectordb.qdrant.deleteCollection($hostOrKey, 'test_collection', {<optional config>})
Upsert向量(它利用了此API
CALL apoc.vectordb.qdrant.upsert($hostOrKey, 'test_collection',
    [
        {id: 1, vector: [0.05, 0.61, 0.76, 0.74], metadata: {city: "Berlin", foo: "one"}},
        {id: 2, vector: [0.19, 0.81, 0.75, 0.11], metadata: {city: "London", foo: "two"}}
    ],
    {<optional config>})
获取向量(它利用了此API
CALL apoc.vectordb.qdrant.get($hostOrKey, 'test_collection', [1,2], {<optional config>})
表1. 示例结果
分数 元数据 ID 向量 文本 实体 错误

{city: "Berlin", foo: "one"}

{city: "Berlin", foo: "two"}

使用{allResults: true}获取向量
CALL apoc.vectordb.qdrant.get($hostOrKey, 'test_collection', [1,2], {allResults: true, <optional config>})
表2. 示例结果
分数 元数据 ID 向量 文本 实体 错误

{city: "Berlin", foo: "one"}

1

[…​]

{city: "Berlin", foo: "two"}

2

[…​]

查询向量(它利用了此API
CALL apoc.vectordb.qdrant.query($hostOrKey,
    'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    { must:
        [ { key: "city", match: { value: "London" } } ]
    },
    5,
    {allResults: true, <optional config>})
表3. 示例结果
分数 元数据 ID 向量 文本 实体 错误

1,

{city: "Berlin", foo: "one"}

1

[…​]

0.1

{city: "Berlin", foo: "two"}

2

[…​]

我们可以定义一个映射,通过利用向量元数据来获取关联的节点和关系,并可选择创建它们。

例如,如果我们使用上述upsert过程创建了2个向量,我们可以填充一些现有的节点(即(:Test {myId: 'one'})(:Test {myId: 'two'})

CALL apoc.vectordb.qdrant.queryAndUpdate($hostOrKey, 'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            embeddingKey: "vect",
            nodeLabel: "Test",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

这将填充两个节点为:(:Test {myId: 'one', city: 'Berlin', vect: [vector1]})(:Test {myId: 'two', city: 'London', vect: [vector2]}),这将在entity列结果中返回。

我们还可以将映射配置mode设置为CREATE_IF_MISSING(如果不存在则创建节点)、READ_ONLY(搜索节点/关系,而不进行更新)或UPDATE_EXISTING(默认行为)

CALL apoc.vectordb.qdrant.queryAndUpdate($hostOrKey, 'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            mode: "CREATE_IF_MISSING",
            embeddingKey: "vect",
            nodeLabel: "Test",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

这将创建2个新的节点,如上所示。

或者,我们可以填充现有的关系(即(:Start)-[:TEST {myId: 'one'}]→(:End)(:Start)-[:TEST {myId: 'two'}]→(:End)

CALL apoc.vectordb.qdrant.queryAndUpdate($hostOrKey, 'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            embeddingKey: "vect",
            relType: "TEST",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

这将填充两个关系为:()-[:TEST {myId: 'one', city: 'Berlin', vect: [vector1]}]-()()-[:TEST {myId: 'two', city: 'London', vect: [vector2]}]-(),这将在entity列结果中返回。

我们还可以将映射用于apoc.vectordb.qdrant.query过程,以搜索符合标签/类型和元数据键的节点/关系,而不进行更新(即等效于具有映射配置的*.queryOrUpdate过程,该配置具有mode: "READ_ONLY")。

例如,使用先前的关系,我们可以执行以下过程,该过程仅在rel列中返回关系

CALL apoc.vectordb.qdrant.query($hostOrKey, 'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            relType: "TEST",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

我们也可以将映射与apoc.vectordb.qdrant.get*过程一起使用。

为了优化性能,我们可以选择使用apoc.vectordb.qdrant.query*apoc.vectordb.qdrant.get*过程YIELD什么。

例如,通过执行CALL apoc.vectordb.qdrant.query(…​) YIELD metadata, score, id,RestAPI请求将具有一个{"with_payload": false, "with_vectors": false},以便我们不返回不需要的其他值。

可以将向量数据库过程与apoc.ml.rag一起执行,如下所示

CALL apoc.vectordb.qdrant.getAndUpdate($host, $collection, [<id1>, <id2>], $conf) YIELD node, metadata, id, vector
WITH collect(node) as paths
CALL apoc.ml.rag(paths, $attributes, $question, $confPrompt) YIELD value
RETURN value
删除向量(它利用了此API
CALL apoc.vectordb.qdrant.delete($hostOrKey, 'test_collection', [1,2], {<optional config>})

性能

下表显示了在230,000条记录样本上所有操作花费的时间,使用MacBook Pro M3 Pro 18GB Ram,使用带有8个CPU的Docker进行测试,内存限制为10GB,交换空间为1.5GB。

表4. 性能结果
操作 时间(毫秒)

apoc.vectordb.qdrant.createCollection

129

apoc.vectordb.qdrant.upsert

1962

apoc.vectordb.qdrant.get

4567

apoc.vectordb.qdrant.query

81

apoc.vectordb.qdrant.delete

21

apoc.vectordb.qdrant.deleteCollection

37