neo4j-contrib / neo4j-apoc-procedures

Awesome Procedures On Cypher for Neo4j - codenamed "apoc"                     If you like it, please ★ above ⇧            
https://neo4j.com/labs/apoc
Apache License 2.0
1.69k stars 495 forks source link

$path configuration parameter adds terminating slash #4169

Open bprager opened 4 weeks ago

bprager commented 4 weeks ago

Expected Behavior (Mandatory)

When I add a 'path' parameter (e.g. "text-embedding-ada-002/embeddings") I expect this actual path to be called without an additional "/" at the end.

Actual Behavior (Mandatory)

'apoc.ml.openai.embedding' adds an additional slash ('/') to the parameter, so the endpoint "text-embedding-ada-002/embeddings/?api-version=2023-12-01-preview" is called instead which causes the call to fail

//Insert here a set of Cypher statements that helps us to reproduce the problem
          CALL apoc.ml.openai.embedding(
            [chunk.text],
            $openAiApiKey,
            {
              endpoint: "*endpoint*,
              apiVersion: "*version*",
              apiType: "AZURE",
              path: "*path*"
            }
          )

Steps (Mandatory)

Screenshots (where it's possibile)

image

Specifications (Mandatory)

https://neo4j.com/labs/apoc/5/ml/openai/

Currently used versions 5.20.0

Versions

bprager commented 3 weeks ago

The error is in OpenAIRequestHandler:

    public String getFullUrl(String method, Map<String, Object> procConfig, ApocConfig apocConfig) {
        return Stream.of(getEndpoint(procConfig, apocConfig), method, getApiVersion(procConfig, apocConfig))
                .filter(StringUtils::isNotBlank)
                .collect(Collectors.joining("/"));
    }

It will always terminate with an "/", which is wrong.

bprager commented 3 weeks ago

This is how a quick fix could look like:

diff --git a/extended/src/main/java/apoc/ml/OpenAIRequestHandler.java b/extended/src/main/java/apoc/ml/OpenAIRequestHandler.java
index cca7bcd54..13c1b767e 100644
--- a/extended/src/main/java/apoc/ml/OpenAIRequestHandler.java
+++ b/extended/src/main/java/apoc/ml/OpenAIRequestHandler.java
@@ -31,6 +31,7 @@ abstract class OpenAIRequestHandler {
     public String getEndpoint(Map<String, Object> procConfig, ApocConfig apocConfig) {
         String url = (String) procConfig.getOrDefault(ENDPOINT_CONF_KEY,
                 apocConfig.getString(APOC_ML_OPENAI_URL, System.getProperty(APOC_ML_OPENAI_URL)));
+
         if (url == null) {
             return getDefaultUrl();
         }
@@ -40,7 +41,8 @@ abstract class OpenAIRequestHandler {
     public String getFullUrl(String method, Map<String, Object> procConfig, ApocConfig apocConfig) {
         return Stream.of(getEndpoint(procConfig, apocConfig), method, getApiVersion(procConfig, apocConfig))
                 .filter(StringUtils::isNotBlank)
-                .collect(Collectors.joining("/"));
+                .collect(Collectors.joining("/"))
+                .replaceAll("/\\?", "?"); // fix broken endpoint