ChromaDB

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

名称 (name) description(描述)

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

获取指定集合的信息;如果集合不存在,则抛出 500 错误。

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

创建一个名称由第 2 个参数指定的集合,并使用指定的 similarity(相似度)和 size(大小)。默认端点为 <hostOrKey 参数>/api/v1/collections

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

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

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

在第 2 个参数指定的集合中执行 Upsert(插入或更新)操作,向量格式为 [{id: 'id', vector: '<vectorDb>', medatada: '<metadata>'}]。默认端点为 <hostOrKey 参数>/api/v1/collections/<collection 参数>/upsert

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

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

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

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

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

在第 2 个参数指定的集合中,根据定义的 vector 检索最相似的向量,并返回 limit 数量的结果。默认端点为 <hostOrKey 参数>/api/v1/collections/<collection 参数>/query

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

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

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

在第 2 个参数指定的集合中,根据定义的 vector 检索最相似的向量,返回 limit 数量的结果,并可选择创建/更新 Neo4j 实体。默认端点为 <hostOrKey 参数>/api/v1/collections/<collection 参数>/query

其中第 1 个参数可以是 APOC 配置中定义的键,即 apoc.chroma.<key>.host=myHost。如果 hostOrKey 为 null,则默认为 'https://:8000'。

示例

获取集合信息(利用了此 API
CALL apoc.vectordb.chroma.info(hostOrKey, 'test_collection', {<optional config>})
表 1. 结果示例

{"name": "test_collection", "metadata": {"size": 4, "hnsw:space": "cosine"}, "database": "default_database", "id": "74ebe008-1ccb-4d3d-8c5d-cdd7cfa526c2", "tenant": "default_tenant"}

创建集合(利用了此 API
CALL apoc.vectordb.chroma.createCollection($host, 'test_collection', 'Cosine', 4, {<optional config>})
表 2. 结果示例
名称 (name) 元数据 (metadata) database id 租户

test_collection

{"size": 4, "hnsw:space": "cosine"}

default_database

9c046861-f46f-417d-bd01-ca8c9f99aee5

default_tenant

删除集合(利用了此 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
表 3. 结果示例
score 元数据 (metadata) id 向量 (vector) 文本 (text) 实体 (entity)

null

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

null

null

null

null

null

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

null

null

null

null

获取带有 {allResults: true} 的向量
CALL apoc.vectordb.chroma.get($host, '<collection_id>', ['1','2'], {<optional config>}), text
表 4. 结果示例
score 元数据 (metadata) id 向量 (vector) 文本 (text) 实体 (entity)

null

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

1

[…​]

ajeje

null

null

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

2

[…​]

brazorf

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
表 5. 结果示例
score 元数据 (metadata) id 向量 (vector) 文本 (text)

1,

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

1

[…​]

ajeje

0.1

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

2

[…​]

brazorf

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

例如,如果我们通过上述 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"
        }
    })

这会像上面一样创建 2 个新节点。

或者,我们可以填充现有关系(例如 (: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 过程中使用映射(mapping),在不进行更新的情况下搜索符合 label/type 和 metadataKey 的节点/关系(即等同于 *.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* 过程中使用映射。

为了优化性能,我们可以选择在使用 apoc.vectordb.chroma.query 和 apoc.vectordb.chroma.get 过程时通过 YIELD 获取哪些内容。例如,执行 CALL apoc.vectordb.chroma.query(…​) YIELD metadata, score, id 时,REST API 请求将包含 {"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

这将返回一个字符串,通过利用向量数据库的嵌入(embeddings)来回答 $question

删除向量(利用了此 API
CALL apoc.vectordb.chroma.delete($host, '<collection_id>', [1,2], {<optional config>})

该操作返回一个已删除 ID 的字符串数组。例如:["1", "2"]

© . This site is unofficial and not affiliated with Neo4j, Inc.