forked from neo4j-contrib/neo4j-apoc-procedures
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes 3611: ML API procedures handle null values being passed in better #439
Closed
Closed
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0816f2b
Fixes 3611: ML API procedures handle null values being passed in better
vga91 ba9cd79
Changed other ml procs
vga91 6746c14
changed null with RuntimeException
vga91 3b0066f
added null with exception in embedding procs
vga91 52d65cf
added check to apoc.ml.bedrock.image
vga91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static apoc.ExtendedApocConfig.APOC_ML_OPENAI_TYPE; | ||
|
@@ -111,13 +113,26 @@ public Stream<EmbeddingResult> getEmbedding(@Name("texts") List<String> texts, @ | |
"model": "text-embedding-ada-002", | ||
"usage": { "prompt_tokens": 8, "total_tokens": 8 } } | ||
*/ | ||
Stream<Object> resultStream = executeRequest(apiKey, configuration, "embeddings", "text-embedding-ada-002", "input", texts, "$.data", apocConfig, urlAccessChecker); | ||
return resultStream | ||
Map<Boolean, List<String>> collect = texts.stream() | ||
.collect(Collectors.groupingBy(Objects::nonNull)); | ||
|
||
List<String> nonNullTexts = collect.get(true); | ||
|
||
Stream<Object> resultStream = executeRequest(apiKey, configuration, "embeddings", "text-embedding-ada-002", "input", nonNullTexts, "$.data", apocConfig, urlAccessChecker); | ||
Stream<EmbeddingResult> embeddingResultStream = resultStream | ||
.flatMap(v -> ((List<Map<String, Object>>) v).stream()) | ||
.map(m -> { | ||
Long index = (Long) m.get("index"); | ||
return new EmbeddingResult(index, texts.get(index.intValue()), (List<Double>) m.get("embedding")); | ||
return new EmbeddingResult(index, nonNullTexts.get(index.intValue()), (List<Double>) m.get("embedding")); | ||
}); | ||
|
||
List<String> nullTexts = collect.getOrDefault(false, List.of()); | ||
Stream<EmbeddingResult> nullResultStream = nullTexts.stream() | ||
.map(i -> { | ||
// null text return index -1 to indicate that are not coming from `/embeddings` RestAPI | ||
return new EmbeddingResult(-1, i, List.of()); | ||
}); | ||
return Stream.concat(embeddingResultStream, nullResultStream); | ||
} | ||
|
||
|
||
|
@@ -132,13 +147,19 @@ public Stream<MapResult> completion(@Name("prompt") String prompt, @Name("api_ke | |
"usage": { "prompt_tokens": 5, "completion_tokens": 7, "total_tokens": 12 } | ||
} | ||
*/ | ||
if (prompt == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. qui ugualmente non sarebbe meglio fallire prima di fare la richiesta? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cambiato |
||
return Stream.of(new MapResult(null)); | ||
} | ||
return executeRequest(apiKey, configuration, "completions", "gpt-3.5-turbo-instruct", "prompt", prompt, "$", apocConfig, urlAccessChecker) | ||
.map(v -> (Map<String,Object>)v).map(MapResult::new); | ||
} | ||
|
||
@Procedure("apoc.ml.openai.chat") | ||
@Description("apoc.ml.openai.chat(messages, api_key, configuration]) - prompts the completion API") | ||
public Stream<MapResult> chatCompletion(@Name("messages") List<Map<String, Object>> messages, @Name("api_key") String apiKey, @Name(value = "configuration", defaultValue = "{}") Map<String, Object> configuration) throws Exception { | ||
if (messages == null) { | ||
return Stream.of(new MapResult(null)); | ||
} | ||
return executeRequest(apiKey, configuration, "chat/completions", "gpt-3.5-turbo", "messages", messages, "$", apocConfig, urlAccessChecker) | ||
.map(v -> (Map<String,Object>)v).map(MapResult::new); | ||
// https://platform.openai.com/docs/api-reference/chat/create | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
qui non servirebbe vedere se la collezione è nulla di modo da poter lanciare un eccezione prima di fare la richiesta?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sì in effetti è meglio, ho aggiunto il check con l'eccezione sopra.
Questa parte l'ho lasciata cosi perché è richiesto dalla issue: