Milvus

以下是所有可用 Milvus 过程的列表

名称 (name) description(描述)

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

获取指定现有集合的信息,如果集合不存在,则返回代码为 100 的响应

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

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

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

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

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

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

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

删除具有指定 ids 的向量。默认端点为 <hostOrKey 参数>/v2/vectordb/entities/delete

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

获取具有指定 ids 的向量。默认端点为 <hostOrKey 参数>/v2/vectordb/entities/get

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

在第 2 个参数指定的集合中,检索与定义的 vector 最相似的向量(结果数量由 limit 限制)。默认端点为 <hostOrKey 参数>/v2/vectordb/entities/search

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

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

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

在第 2 个参数指定的集合中检索与定义的 vector 最相似的向量(结果数量由 limit 限制),并可选择创建/更新 Neo4j 实体。默认端点为 <hostOrKey 参数>/v2/vectordb/entities/search

其中第 1 个参数可以是 apoc 配置中定义的键 apoc.milvus.<key>.host=myHost。当 hostOrKey 为 null 时,默认主机为 'https://:19530'。

示例

以下是使用默认端口 19531 的本地安装示例列表。

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

{"data": {"shardsNum": 1, "aliases": [], "autoId": false, "description": "", "partitionsNum": 1, "collectionName": "test_collection", "indexes": [{"metricType": "COSINE", "indexName": "vector", "fieldName": "vector"}], "load": "LoadStateLoading", "consistencyLevel": "Bounded", "fields": [{"partitionKey": false, "autoId": false, "name": "id", "description": "", "id": 100, "type": "Int64", "primaryKey": true}, {"partitionKey": false, "autoId": false, "name": "vector", "description": "", "id": 101, "params": [{"value": 4, "key": "dim"}], "type": "FloatVector", "primaryKey": false} ], "collectionID": "451046728334049293", "enableDynamicField": true, "properties": []}, "message": "", "code": 200 }

创建集合(利用了此 API
CALL apoc.vectordb.milvus.createCollection('https://:19531', 'test_collection', 'COSINE', 4, {<optional config>})
表 2. 结果示例
data code

null

200

删除集合(利用了此 API
CALL apoc.vectordb.milvus.deleteCollection('https://:19531', 'test_collection', {<optional config>})
表 3. 结果示例
data code

null

200

Upsert 向量(利用了此 API
CALL apoc.vectordb.milvus.upsert('https://:19531', '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>})
表 4. 结果示例
data code

{"upsertCount": 2, "upsertId": [1, 2]}

200

获取向量(利用了此 API
CALL apoc.vectordb.milvus.get('https://:19531', 'test_collection', [1,2], {<optional config>})
表 5. 结果示例
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.milvus.get('https://:19531', 'test_collection', [1,2], {allResults: true, <optional config>})
表 6. 结果示例
score 元数据 (metadata) id 向量 (vector) 文本 (text) 实体 (entity)

null

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

1

[…​]

null

null

null

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

2

[…​]

null

null

查询向量(利用了此 API
CALL apoc.vectordb.milvus.query('https://:19531',
    'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    { must:
        [ { key: "city", match: { value: "London" } } ]
    },
    5,
    {allResults: true, <optional config>})
表 7. 结果示例
score 元数据 (metadata) id 向量 (vector) 文本 (text) 实体 (entity)

1,

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

1

[…​]

null

null

0.1

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

2

[…​]

null

null

我们可以定义映射,利用向量元数据自动创建一个或多个节点和关系。

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

CALL apoc.vectordb.milvus.queryAndUpdate('https://:19531', '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.milvus.queryAndUpdate('https://:19531', '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.milvus.queryAndUpdate('https://:19531', '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.milvus.query 过程中使用映射,以搜索符合 label/type 和 metadataKey 的节点/关系,而无需进行更新(即等同于映射配置中 mode: "READ_ONLY"*.queryOrUpdate 过程)。

例如,对于之前的关系,我们可以执行以下过程,该过程仅返回 rel 列中的关系:

CALL apoc.vectordb.milvus.query('https://:19531', 'test_collection',
    [0.2, 0.1, 0.9, 0.7],
    {},
    5,
    { mapping: {
            embeddingKey: "vect",
            relType: "TEST",
            entityKey: "myId",
            metadataKey: "foo"
        }
    })

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

为了优化性能,我们可以选择使用 apoc.vectordb.milvus.query*apoc.vectordb.milvus.get* 过程的 YIELD 子句来指定返回内容。

例如,执行 CALL apoc.vectordb.milvus.query(…​) YIELD metadata, score, id 时,RestAPI 请求将包含 {"with_payload": false, "with_vectors": false},从而避免返回我们不需要的其他值。

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

CALL apoc.vectordb.milvus.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.milvus.delete('https://:19531', 'test_collection', [1,2], {<optional config>})
表 8. 结果示例
data code

null

200

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