From 5db7fcf0349d254037711770df5a62f7d24e2462 Mon Sep 17 00:00:00 2001 From: vga91 Date: Mon, 27 May 2024 18:03:44 +0200 Subject: [PATCH] small code refactoring --- .../database-integration/vectordb/milvus.adoc | 22 ++++++++++++++----- .../vectordb/pinecone.adoc | 4 ++-- .../test/java/apoc/vectordb/MilvusTest.java | 2 +- .../src/main/java/apoc/vectordb/Milvus.java | 22 +++++++++---------- .../java/apoc/vectordb/MilvusHandler.java | 3 ++- .../src/main/java/apoc/vectordb/Pinecone.java | 9 ++++---- 6 files changed, 38 insertions(+), 24 deletions(-) diff --git a/docs/asciidoc/modules/ROOT/pages/database-integration/vectordb/milvus.adoc b/docs/asciidoc/modules/ROOT/pages/database-integration/vectordb/milvus.adoc index e9fcba27bd..39f4be8c24 100644 --- a/docs/asciidoc/modules/ROOT/pages/database-integration/vectordb/milvus.adoc +++ b/docs/asciidoc/modules/ROOT/pages/database-integration/vectordb/milvus.adoc @@ -8,16 +8,28 @@ Here is a list of all available Milvus procedures: | name | description | apoc.vectordb.milvus.createCollection(hostOrKey, collection, similarity, size, $config) | Creates a collection, with the name specified in the 2nd parameter, and with the specified `similarity` and `size`. + The default endpoint is `/v2/vectordb/collections/create`. | apoc.vectordb.milvus.deleteCollection(hostOrKey, collection, $config) | - Deletes a collection with the name specified in the 2nd parameter + Deletes a collection with the name specified in the 2nd parameter. + The default endpoint is `/v2/vectordb/collections/drop`. | apoc.vectordb.milvus.upsert(hostOrKey, collection, vectors, $config) | - Upserts, in the collection with the name specified in the 2nd parameter, the vectors [{id: 'id', vector: '', medatada: ''}] + Upserts, in the collection with the name specified in the 2nd parameter, the vectors [{id: 'id', vector: '', medatada: ''}]. + The default endpoint is `/v2/vectordb/entities/upsert`. | apoc.vectordb.milvus.delete(hostOrKey, collection, ids, $config) | Delete the vectors with the specified `ids`. + The default endpoint is `/v2/vectordb/entities/delete`. | apoc.vectordb.milvus.get(hostOrKey, collection, ids, $config) | Get the vectors with the specified `ids`. + The default endpoint is `/v2/vectordb/entities/get`. | apoc.vectordb.milvus.query(hostOrKey, collection, vector, filter, limit, $config) | Retrieve closest vectors the the defined `vector`, `limit` of results, in the collection with the name specified in the 2nd parameter. + The default endpoint is `/v2/vectordb/entities/search`. +| apoc.vectordb.milvus.getAndUpdate(hostOrKey, collection, ids, $config) | + Get the vectors with the specified `ids`. + The default endpoint is `/v2/vectordb/entities/get`, and optionally creates/updates neo4j entities. +| apoc.vectordb.milvus.queryAndUpdate(hostOrKey, collection, vector, filter, limit, $config) | + Retrieve closest vectors the the defined `vector`, `limit` of results, in the collection with the name specified in the 2nd parameter, and optionally creates/updates neo4j entities. + The default endpoint is `/v2/vectordb/entities/search`. |=== where the 1st parameter can be a key defined by the apoc config `apoc.milvus..host=myHost`. @@ -118,7 +130,7 @@ we can populate some existing nodes (i.e. `(:Test {myId: 'one'})` and `(:Test {m [source,cypher] ---- -CALL apoc.vectordb.milvus.query('http://localhost:19531', 'test_collection', +CALL apoc.vectordb.milvus.queryAndUpdate('http://localhost:19531', 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, @@ -139,7 +151,7 @@ Or else, we can create a node if not exists, via `create: true`: [source,cypher] ---- -CALL apoc.vectordb.milvus.query('http://localhost:19531', 'test_collection', +CALL apoc.vectordb.milvus.queryAndUpdate('http://localhost:19531', 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, @@ -160,7 +172,7 @@ Or, we can populate an existing relationship (i.e. `(:Start)-[:TEST {myId: 'one' [source,cypher] ---- -CALL apoc.vectordb.milvus.query('http://localhost:19531', 'test_collection', +CALL apoc.vectordb.milvus.queryAndUpdate('http://localhost:19531', 'test_collection', [0.2, 0.1, 0.9, 0.7], {}, 5, diff --git a/docs/asciidoc/modules/ROOT/pages/database-integration/vectordb/pinecone.adoc b/docs/asciidoc/modules/ROOT/pages/database-integration/vectordb/pinecone.adoc index edc9a29648..bf582b0c39 100644 --- a/docs/asciidoc/modules/ROOT/pages/database-integration/vectordb/pinecone.adoc +++ b/docs/asciidoc/modules/ROOT/pages/database-integration/vectordb/pinecone.adoc @@ -25,10 +25,10 @@ Here is a list of all available Pinecone procedures: Get the vectors with the specified `ids`. The default endpoint is `/vectors/fetch`. | apoc.vectordb.pinecone.query(hostOrKey, index, vector, filter, limit, $config) | - Retrieve closest vectors the the defined `vector`, `limit` of results, in the index with the name specified in the 2nd parameter. + Retrieve closest vectors the the defined `vector`, `limit` of results, in the index with the name specified in the 2nd parameter, and optionally creates/updates neo4j entities. The default endpoint is `/query`. | apoc.vectordb.pinecone.queryAndUpdate(hostOrKey, index, vector, filter, limit, $config) | - Retrieve closest vectors the the defined `vector`, `limit` of results, in the index with the name specified in the 2nd parameter. + Retrieve closest vectors the the defined `vector`, `limit` of results, in the index with the name specified in the 2nd parameter, and optionally creates/updates neo4j entities. The default endpoint is `/query`. |=== diff --git a/extended-it/src/test/java/apoc/vectordb/MilvusTest.java b/extended-it/src/test/java/apoc/vectordb/MilvusTest.java index c4588a5bf8..513b6167ec 100644 --- a/extended-it/src/test/java/apoc/vectordb/MilvusTest.java +++ b/extended-it/src/test/java/apoc/vectordb/MilvusTest.java @@ -382,7 +382,7 @@ public void queryVectorsWithSystemDbStorage() { "keyConfig", keyConfig, "databaseName", DEFAULT_DATABASE_NAME, "conf", map( - "host", HOST, + "host", HOST + "/v2/vectordb", "mapping", mapping ) ) diff --git a/extended/src/main/java/apoc/vectordb/Milvus.java b/extended/src/main/java/apoc/vectordb/Milvus.java index de06efe852..ae4d9b7ff6 100644 --- a/extended/src/main/java/apoc/vectordb/Milvus.java +++ b/extended/src/main/java/apoc/vectordb/Milvus.java @@ -26,7 +26,8 @@ @Extended public class Milvus { - + public static final VectorDbHandler DB_HANDLER = MILVUS.get(); + @Context public ProcedureCallContext procedureCallContext; @@ -46,7 +47,7 @@ public Stream createCollection(@Name("hostOrKey") String hostOrKey, @Name("similarity") String similarity, @Name("size") Long size, @Name(value = "configuration", defaultValue = "{}") Map configuration) throws Exception { - String url = "%s/v2/vectordb/collections/create"; + String url = "%s/collections/create"; Map config = getVectorDbInfo(hostOrKey, collection, configuration, url); config.putIfAbsent(METHOD_KEY, "POST"); @@ -67,7 +68,7 @@ public Stream deleteCollection( @Name("collection") String collection, @Name(value = "configuration", defaultValue = "{}") Map configuration) throws Exception { - String url = "%s/v2/vectordb/collections/drop"; + String url = "%s/collections/drop"; Map config = getVectorDbInfo(hostOrKey, collection, configuration, url); config.putIfAbsent(METHOD_KEY, "POST"); Map additionalBodies = Map.of("collectionName", collection); @@ -86,7 +87,7 @@ public Stream upsert( @Name("vectors") List> vectors, @Name(value = "configuration", defaultValue = "{}") Map configuration) throws Exception { - String url = "%s/v2/vectordb/entities/upsert"; + String url = "%s/entities/upsert"; Map config = getVectorDbInfo(hostOrKey, collection, configuration, url); config.putIfAbsent(METHOD_KEY, "POST"); @@ -114,12 +115,11 @@ public Stream delete( @Name("vectors") List ids, @Name(value = "configuration", defaultValue = "{}") Map configuration) throws Exception { - String url = "%s/v2/vectordb/entities/delete"; + String url = "%s/entities/delete"; Map config = getVectorDbInfo(hostOrKey, collection, configuration, url); config.putIfAbsent(METHOD_KEY, "POST"); String filter = "id in " + ids; - System.out.println("filter = " + filter); Map additionalBodies = Map.of("collectionName", collection, "filter", filter); RestAPIConfig apiConfig = new RestAPIConfig(config, Map.of(), additionalBodies); return executeRequest(apiConfig, urlAccessChecker) @@ -146,14 +146,14 @@ public Stream getAndUpdate(@Name("hostOrKey") String hostOrKey, } private Stream getCommon(String hostOrKey, String collection, List ids, Map configuration, boolean readOnly) throws Exception { - String url = "%s/v2/vectordb/entities/get"; + String url = "%s/entities/get"; Map config = getVectorDbInfo(hostOrKey, collection, configuration, url); if (readOnly) { checkMappingConf(configuration, "apoc.vectordb.milvus.getAndUpdate"); } - VectorEmbeddingConfig apiConfig = MILVUS.get().getEmbedding().fromGet(config, procedureCallContext, ids, collection); + VectorEmbeddingConfig apiConfig = DB_HANDLER.getEmbedding().fromGet(config, procedureCallContext, ids, collection); return getEmbeddingResultStream(apiConfig, procedureCallContext, urlAccessChecker, tx, v -> getMapStream((Map) v)); } @@ -199,20 +199,20 @@ private Stream getMapStream(Map v) { } private Stream queryCommon(String hostOrKey, String collection, List vector, Object filter, long limit, Map configuration, boolean readOnly) throws Exception { - String url = "%s/v2/vectordb/entities/search"; + String url = "%s/entities/search"; Map config = getVectorDbInfo(hostOrKey, collection, configuration, url); if (readOnly) { checkMappingConf(configuration, "apoc.vectordb.milvus.queryAndUpdate"); } - VectorEmbeddingConfig apiConfig = MILVUS.get().getEmbedding().fromQuery(config, procedureCallContext, vector, filter, limit, collection); + VectorEmbeddingConfig apiConfig = DB_HANDLER.getEmbedding().fromQuery(config, procedureCallContext, vector, filter, limit, collection); return getEmbeddingResultStream(apiConfig, procedureCallContext, urlAccessChecker, tx, v -> getMapStream((Map) v)); } private Map getVectorDbInfo( String hostOrKey, String collection, Map configuration, String templateUrl) { - return getCommonVectorDbInfo(hostOrKey, collection, configuration, templateUrl, MILVUS.get()); + return getCommonVectorDbInfo(hostOrKey, collection, configuration, templateUrl, DB_HANDLER); } } diff --git a/extended/src/main/java/apoc/vectordb/MilvusHandler.java b/extended/src/main/java/apoc/vectordb/MilvusHandler.java index bd87e71009..bfed42afe0 100644 --- a/extended/src/main/java/apoc/vectordb/MilvusHandler.java +++ b/extended/src/main/java/apoc/vectordb/MilvusHandler.java @@ -15,7 +15,8 @@ public class MilvusHandler implements VectorDbHandler { @Override public String getUrl(String hostOrKey) { - return new UrlResolver("http", "localhost", 19530).getUrl("milvus", hostOrKey); + String url = new UrlResolver("http", "localhost", 19530).getUrl("milvus", hostOrKey); + return url + "/v2/vectordb"; } @Override diff --git a/extended/src/main/java/apoc/vectordb/Pinecone.java b/extended/src/main/java/apoc/vectordb/Pinecone.java index 3371cae707..6ece95d871 100644 --- a/extended/src/main/java/apoc/vectordb/Pinecone.java +++ b/extended/src/main/java/apoc/vectordb/Pinecone.java @@ -27,7 +27,8 @@ @Extended public class Pinecone { - + public static final VectorDbHandler DB_HANDLER = PINECONE.get(); + @Context public ProcedureCallContext procedureCallContext; @@ -152,7 +153,7 @@ private Stream getCommon(String hostOrKey, String checkMappingConf(configuration, "apoc.vectordb.pinecone.getAndUpdate"); } - VectorEmbeddingConfig apiConfig = PINECONE.get().getEmbedding().fromGet(config, procedureCallContext, ids, collection); + VectorEmbeddingConfig apiConfig = DB_HANDLER.getEmbedding().fromGet(config, procedureCallContext, ids, collection); return getEmbeddingResultStream(apiConfig, procedureCallContext, urlAccessChecker, tx, v -> { Object vectors = ((Map) v).get("vectors"); @@ -191,7 +192,7 @@ private Stream queryCommon(String hostOrKey, Strin checkMappingConf(configuration, "apoc.vectordb.pinecone.queryAndUpdate"); } - VectorEmbeddingConfig apiConfig = PINECONE.get().getEmbedding().fromQuery(config, procedureCallContext, vector, filter, limit, collection); + VectorEmbeddingConfig apiConfig = DB_HANDLER.getEmbedding().fromQuery(config, procedureCallContext, vector, filter, limit, collection); return getEmbeddingResultStream(apiConfig, procedureCallContext, urlAccessChecker, tx, v -> { Map map = (Map) v; @@ -202,6 +203,6 @@ private Stream queryCommon(String hostOrKey, Strin private Map getVectorDbInfo( String hostOrKey, String collection, Map configuration, String templateUrl) { - return getCommonVectorDbInfo(hostOrKey, collection, configuration, templateUrl, PINECONE.get()); + return getCommonVectorDbInfo(hostOrKey, collection, configuration, templateUrl, DB_HANDLER); } }