Pinecone
|
在 Pinecone 中,集合 (collection) 是索引的静态且不可查询的副本,因此,与其他向量数据库不同,Pinecone 过程是针对索引而非集合进行操作的。 然而,用于处理集合 CRUD 操作的 vectordb 过程通常被命名为 |
以下是所有可用 Pinecone 过程的列表
| 名称 (name) | description(描述) |
|---|---|
apoc.vectordb.pinecone.info(hostOrKey, index, $config) |
获取有关指定现有索引的信息,如果索引不存在则抛出 404 错误 |
apoc.vectordb.pinecone.createCollection(hostOrKey, index, similarity, size, $config) |
创建一个名称由第 2 个参数指定的索引,并设置指定的 |
apoc.vectordb.pinecone.deleteCollection(hostOrKey, index, $config) |
删除名称由第 2 个参数指定的索引。默认端点为 |
apoc.vectordb.pinecone.upsert(hostOrKey, index, vectors, $config) |
在名称由第 2 个参数指定的索引中,插入向量 [{id: 'id', vector: '<vectorDb>', medatada: '<metadata>'}]。默认端点为 |
apoc.vectordb.pinecone.delete(hostOrKey, index, ids, $config) |
删除具有指定 |
apoc.vectordb.pinecone.get(hostOrKey, index, ids, $config) |
获取具有指定 |
apoc.vectordb.pinecone.getAndUpdate(hostOrKey, index, ids, $config) |
获取具有指定 |
apoc.vectordb.pinecone.query(hostOrKey, index, vector, filter, limit, $config) |
在名称由第 2 个参数指定的索引中,检索与定义的 |
apoc.vectordb.pinecone.queryAndUpdate(hostOrKey, index, vector, filter, limit, $config) |
在名称由第 2 个参数指定的索引中,检索与定义的 |
其中第 1 个参数可以是 apoc 配置中定义的键,例如 apoc.pinecone.<key>.host=myHost。
默认的 hostOrKey 是 "https://api.pinecone.io",因此在使用 createCollection 和 deleteCollection 过程时通常可以为 null;而在其他过程中,它等于主机名(即 Pinecone 控制面板中指示的主机名)。
示例
以下示例假设我们要创建并管理一个名为 test-index 的索引。
CALL apoc.vectordb.pinecone.info(hostOrKey, 'test-index', {<optional config>})
| 值 |
|---|
{ "dimension": 3, "environment": "us-east1-gcp", "name": "tiny-index", "size": 3126700, "status": "Ready", "vector_count": 99 } |
CALL apoc.vectordb.pinecone.createCollection(null, 'test-index', 'cosine', 4, {<optional config>})
CALL apoc.vectordb.pinecone.deleteCollection(null, 'test-index', {<optional config>})
CALL apoc.vectordb.pinecone.upsert('https://test-index-ilx67g5.svc.aped-4627-b74a.pinecone.io',
'test-index',
[
{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>})
CALL apoc.vectordb.pinecone.get($host, 'test-index', [1,2], {<optional config>})
| 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.pinecone.get($host, 'test-index', ['1','2'], {allResults: true, <optional config>})
| score | 元数据 (metadata) | id | 向量 (vector) | 文本 (text) | 实体 (entity) |
|---|---|---|---|---|---|
null |
{city: "Berlin", foo: "one"} |
1 |
[…] |
null |
null |
null |
{city: "Berlin", foo: "two"} |
2 |
[…] |
null |
null |
CALL apoc.vectordb.pinecone.query($host,
'test-index',
[0.2, 0.1, 0.9, 0.7],
{ city: { `$eq`: "London" } },
5,
{allResults: true, <optional config>})
| 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.pinecone.queryAndUpdate($host, 'test-index',
[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.pinecone.queryAndUpdate($host, 'test-index',
[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.pinecone.queryAndUpdate($host, 'test-index',
[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.pinecone.query 过程使用映射,以搜索符合标签/类型和元数据键的节点/关系,而无需进行更新(即等同于映射配置中 mode: "READ_ONLY" 的 *.queryOrUpdate 过程)。
例如,对于之前的关系,我们可以执行以下过程,该过程仅返回 rel 列中的关系:
CALL apoc.vectordb.pinecone.query($host, 'test-index',
[0.2, 0.1, 0.9, 0.7],
{},
5,
{ mapping: {
embeddingKey: "vect",
relType: "TEST",
entityKey: "myId",
metadataKey: "foo"
}
})
|
我们也可以对 |
|
为了优化性能,我们可以选择使用 例如,通过执行 |
可以将向量数据库过程与 apoc.ml.rag 一起使用,如下所示:
CALL apoc.vectordb.pinecone.getAndUpdate($host, $index, [<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
CALL apoc.vectordb.pinecone.delete($host, 'test-index', ['1','2'], {<optional config>})