ChromaDB

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

名称 描述

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

创建集合,在第二个参数中指定名称,并使用指定的 similaritysize。默认端点为 <hostOrKey param>/api/v1/collections

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

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

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

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

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

删除具有指定ids的向量。默认端点为<hostOrKey param>/api/v1/collections/<collection param>/delete

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

获取具有指定ids的向量。默认端点为<hostOrKey param>/api/v1/collections/<collection param>/get

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

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

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

获取具有指定ids的向量,并选择性创建/更新neo4j实体。默认端点为<hostOrKey param>/api/v1/collections/<collection param>/get

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

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

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

示例

创建集合(它利用此API
CALL apoc.vectordb.chroma.createCollection($host, 'test_collection', 'Cosine', 4, {<optional config>})
删除集合(它利用此API
CALL apoc.vectordb.chroma.deleteCollection($host, '<collection_id>', {<optional config>})
Upsert向量(它利用此API
CALL apoc.vectordb.qdrant.upsert($host, '<collection_id>',
    [
        {id: 1, vector: [0.05, 0.61, 0.76, 0.74], metadata: {city: "Berlin", foo: "one"}, text: 'ajeje'},
        {id: 2, vector: [0.19, 0.81, 0.75, 0.11], metadata: {city: "London", foo: "two"}, text: 'brazorf'}
    ],
    {<optional config>})
获取向量(它利用此API
CALL apoc.vectordb.chroma.get($host, '<collection_id>', ['1','2'], {<optional config>}), text
表1. 示例结果
score metadata id vector text entity errors

null

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

null

null

null

null

null

null

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

null

null

null

null

null

使用{allResults: true}获取向量
CALL apoc.vectordb.chroma.get($host, '<collection_id>', ['1','2'], {<optional config>}), text
表2. 示例结果
score metadata id vector text entity errors

null

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

1

[…​]

ajeje

null

null

null

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

2

[…​]

brazorf

null

null

查询向量(它利用此API
CALL apoc.vectordb.chroma.queryAndUpdate($host,
    '<collection_id>',
    [0.2, 0.1, 0.9, 0.7],
    {city: 'London'},
    5,
    {allResults: true, <optional config>}), text
表3. 示例结果
score metadata id vector text errors

1,

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

1

[…​]

ajeje

null

0.1

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

2

[…​]

brazorf

null

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

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

查询向量
CALL apoc.vectordb.chroma.queryAndUpdate($host, '<collection_id>',
    [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.chroma.queryAndUpdate($host, '<collection_id>',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            mode: "CREATE_IF_MISSING",
            embeddingKey: "vect",
            nodeLabel: "Test",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

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

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

CALL apoc.vectordb.chroma.queryAndUpdate($host, '<collection_id>',
    [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.chroma.query过程,以搜索符合标签/类型和元数据键的节点/关系,而不进行更新(即等效于*.queryOrUpdate过程,其中映射配置具有mode: "READ_ONLY")。

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

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

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

为了优化性能,我们可以选择使用YIELD与apoc.vectordb.chroma.query和apoc.vectordb.chroma.get过程一起使用。例如,通过执行CALL apoc.vectordb.chroma.query(…​) YIELD metadata, score, id,RestAPI请求将包含一个{"include": ["metadatas", "documents", "distances"]},这样我们不会返回不需要的其他值。

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

CALL apoc.vectordb.chroma.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.chroma.delete($host, '<collection_id>', [1,2], {<optional config>})

性能

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

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

apoc.vectordb.chroma.createCollection

158

apoc.vectordb.chroma.upsert

10650

apoc.vectordb.chroma.get

2357

apoc.vectordb.chroma.query

1068

apoc.vectordb.chroma.delete

9827

apoc.vectordb.chroma.deleteCollection

141