Skip to content

Commit

Permalink
created Extended utils
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 committed Oct 19, 2023
1 parent 0ebc486 commit 57ba306
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 42 deletions.
5 changes: 2 additions & 3 deletions extended/src/main/java/apoc/get/GetProcedures.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import apoc.result.NodeResult;
import apoc.result.RelationshipResult;
import org.neo4j.graphdb.Transaction;
import org.neo4j.kernel.impl.coreapi.InternalTransaction;
import org.neo4j.procedure.Context;
import org.neo4j.procedure.Description;
import org.neo4j.procedure.Name;
Expand All @@ -21,13 +20,13 @@ public class GetProcedures {
@Procedure
@Description("apoc.get.nodes(node|id|[ids]) - quickly returns all nodes with these id's")
public Stream<NodeResult> nodes(@Name("nodes") Object ids) {
return new Get((InternalTransaction) tx).nodes(ids);
return new Get(tx).nodes(ids);
}

@Procedure
@Description("apoc.get.rels(rel|id|[ids]) - quickly returns all relationships with these id's")
public Stream<RelationshipResult> rels(@Name("relationships") Object ids) {
return new Get((InternalTransaction) tx).rels(ids);
return new Get(tx).rels(ids);
}

}
3 changes: 1 addition & 2 deletions extended/src/main/java/apoc/ml/OpenAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public EmbeddingResult(long index, String text, List<Double> embedding) {
}

static Stream<Object> executeRequest(String apiKey, Map<String, Object> configuration, String path, String model, String key, Object inputs, String jsonPath, ApocConfig apocConfig) throws JsonProcessingException, MalformedURLException {
// todo - something like that?
apiKey = apocConfig.getString(APOC_OPENAI_KEY, apiKey);
if (apiKey == null || apiKey.isBlank())
throw new IllegalArgumentException("API Key must not be empty");
Expand All @@ -60,7 +59,7 @@ static Stream<Object> executeRequest(String apiKey, Map<String, Object> configur
String payload = new ObjectMapper().writeValueAsString(config);

var url = new URL(new URL(endpoint), path).toString();
return JsonUtil.loadJson(url, headers, payload, jsonPath);
return JsonUtil.loadJson(url, headers, payload, jsonPath, true, List.of());
}

@Procedure("apoc.ml.openai.embedding")
Expand Down
2 changes: 1 addition & 1 deletion extended/src/main/java/apoc/ml/VertexAI.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static Stream<Object> executeRequest(String accessToken, String project,
Map<String, Object> data = Map.of("instances", inputs, "parameters", getParameters(configuration, retainConfigKeys));
String payload = new ObjectMapper().writeValueAsString(data);

return JsonUtil.loadJson(endpoint, headers, payload, jsonPath);
return JsonUtil.loadJson(endpoint, headers, payload, jsonPath, true, List.of());
}

@Procedure("apoc.ml.vertexai.embedding")
Expand Down
55 changes: 24 additions & 31 deletions extended/src/main/java/apoc/util/ExtendedUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@

import static apoc.export.cypher.formatter.CypherFormatterUtils.formatProperties;
import static apoc.export.cypher.formatter.CypherFormatterUtils.formatToString;
import static apoc.util.JsonUtil.streamObjetsFromIStream;
import static apoc.util.JsonUtil.OBJECT_MAPPER;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.time.Duration;
import java.time.temporal.TemporalAccessor;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.Spliterators;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.MappingIterator;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
Expand All @@ -28,9 +33,6 @@
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.neo4j.graphdb.Entity;

public class ExtendedUtil
Expand All @@ -57,48 +59,39 @@ public static HttpRequestBase fromMethodName(String method, String uri) {
* Similar to JsonUtil.loadJson(..) but works e.g. with GET method as well,
* for which it would return a FileNotFoundException
*/
public static Stream<Object> getModelItemResultStream(String method, HttpClient httpClient, String payloadString, Map<String, Object> headers, String endpoint, String path, List<String> of
/*Function<Stream<Object>, Stream<Object>> function*/) {
public static Stream<Object> getModelItemResultStream(String method, HttpClient httpClient, String payloadString, Map<String, Object> headers, String endpoint, String path, List<String> pathOptions) {

try {
// -- request with headers and payload
HttpRequestBase request = fromMethodName(method, endpoint);

headers.forEach((k, v) -> request.setHeader(k, v.toString()));

if (request instanceof HttpEntityEnclosingRequestBase entityRequest) {
try {
entityRequest.setEntity(new StringEntity(payloadString));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
entityRequest.setEntity(new StringEntity(payloadString));
}
// try (
// HttpClient httpClient = HttpClientBuilder.create().build();//) {

// DefaultHttpClient httpClient = new DefaultHttpClient();
// -- response
HttpResponse response = httpClient.execute(request);

InputStream stream = response.getEntity().getContent();

return streamObjetsFromIStream(stream, path, of);
// Stream<Object> objStream = streamObjetsFromIStream(stream, path, of);
//
// return function.apply(objStream);
// .onClose(() -> {
// try {
// httpClient.close();
// } catch (IOException e) {
// throw new RuntimeException(e);
// }
// });
// return objectStream
// .flatMap(i -> ((List<Map<String, Object>>) i).stream())
// .map(ModelItemResult::new);
return streamObjetsFromIStream(stream, path, pathOptions);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

/**
* Along the lines of {@link JsonUtil#loadJson(Object, Map, String, String, boolean, List)}
* after the `FileUtils.inputStreamFor` method
*/
public static Stream<Object> streamObjetsFromIStream(InputStream input, String path, List<String> options) throws IOException {
JsonParser parser = OBJECT_MAPPER.getFactory().createParser(input);
MappingIterator<Object> it = OBJECT_MAPPER.readValues(parser, Object.class);
Stream<Object> stream = StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, 0), false);
return StringUtils.isBlank(path) ? stream : stream.map((value) -> JsonPath.parse(value, Configuration.builder().build()).read(path));
}


public static String dateFormat( TemporalAccessor value, String format){
return Util.getFormat(format).format(value);
Expand Down
2 changes: 1 addition & 1 deletion extended/src/test/java/apoc/ml/OpenAITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void getEmbedding() {

@Test
public void completion() {
testCall(db, "CALL apoc.ml.openai.completion('What color is the sky? Answer: ', 'fake-api-key1')", (row) -> {
testCall(db, "CALL apoc.ml.openai.completion('What color is the sky? Answer: ', 'fake-api-key')", (row) -> {
var result = (Map<String,Object>)row.get("value");
assertEquals(true, result.get("created") instanceof Number);
assertEquals(true, result.containsKey("choices"));
Expand Down
1 change: 0 additions & 1 deletion extended/src/test/java/apoc/ml/VertexAIIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public void setUp() throws Exception {
parameters = Map.of("apiKey", vertexAiKey, "project", vertexAiProject);
}


@Test
public void getEmbedding() {
testCall(db, "CALL apoc.ml.vertexai.embedding(['Some Text'], $apiKey, $project)", parameters,(row) -> {
Expand Down
5 changes: 2 additions & 3 deletions extended/src/test/java/apoc/ml/bedrock/BedrockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@


/**
* NB: WORK IN PROGRESS.
* NB: WORK IN PROGRESS. Mock Test, with local endpoint
* TODO:
* // test with fake api like OpenAITest??
* // via fake url
* // like OpenAITest, via fake url
* // try with apocConfig() and wrong confMap --> should work
* // try with wrong apocConfig() and right confMap --> should NOT work
* // try deactivating apocConfig() and put in confMap
Expand Down

0 comments on commit 57ba306

Please sign in to comment.