-
Notifications
You must be signed in to change notification settings - Fork 138
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
Add neural search default processor for non OpenAI/Cohere scenario #1274
Merged
zane-neo
merged 15 commits into
opensearch-project:main
from
zane-neo:neural-search-default-processor
Sep 26, 2023
Merged
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
38304c3
Add neural search default pre/post process function support
zane-neo 4d05c10
Fix UT failures
zane-neo 13fe1ae
Address PR comment to remove nonJson response case
zane-neo 15c206a
Fix low code coverage issue
zane-neo f34af72
fix format issue
zane-neo 1924618
Try to fix classNotFound issue in IT
zane-neo 8c42f02
revert Try to fix classNotFound issue in IT
zane-neo fe25fcf
Change gson dependency to compileOnly
zane-neo a26c686
Change default pre/post process function name
zane-neo ded4afc
Address code review comments
zane-neo 502add1
Make preprocess function to default
zane-neo 538ac3f
Remove GsonUtil since there already a single instance in StringUtils
zane-neo 09ce432
Fix UT failures
zane-neo 027b4ee
Address comments
zane-neo 9ec0bd8
use import instead of fully qualified name
zane-neo 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
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 |
---|---|---|
|
@@ -6,44 +6,37 @@ | |
package org.opensearch.ml.common.connector; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
public class MLPreProcessFunction { | ||
|
||
private static Map<String, String> PRE_PROCESS_FUNCTIONS; | ||
private static final Map<String, Function<List<String>, Map<String, Object>>> PRE_PROCESS_FUNCTIONS = new HashMap<>(); | ||
public static final String TEXT_DOCS_TO_COHERE_EMBEDDING_INPUT = "connector.pre_process.cohere.embedding"; | ||
public static final String TEXT_DOCS_TO_OPENAI_EMBEDDING_INPUT = "connector.pre_process.openai.embedding"; | ||
|
||
public static final String DEFAULT_EMBEDDING_INPUT = "connector.pre_process.default.embedding"; | ||
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. Just to match the other two pattern, may be: 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. done |
||
|
||
private static Function<List<String>, Map<String, Object>> cohereTextEmbeddingPreProcess() { | ||
return inputs -> Map.of("parameters", Map.of("texts", inputs)); | ||
} | ||
|
||
private static Function<List<String>, Map<String, Object>> openAiTextEmbeddingPreProcess() { | ||
return inputs -> Map.of("parameters", Map.of("input", inputs)); | ||
} | ||
|
||
static { | ||
PRE_PROCESS_FUNCTIONS = new HashMap<>(); | ||
//TODO: change to java for openAI, embedding and Titan | ||
PRE_PROCESS_FUNCTIONS.put(TEXT_DOCS_TO_COHERE_EMBEDDING_INPUT, "\n StringBuilder builder = new StringBuilder();\n" + | ||
" builder.append(\"[\");\n" + | ||
" for (int i=0; i< params.text_docs.length; i++) {\n" + | ||
" builder.append(\"\\\"\");\n" + | ||
" builder.append(params.text_docs[i]);\n" + | ||
" builder.append(\"\\\"\");\n" + | ||
" if (i < params.text_docs.length - 1) {\n" + | ||
" builder.append(\",\")\n" + | ||
" }\n" + | ||
" }\n" + | ||
" builder.append(\"]\");\n" + | ||
" def parameters = \"{\" +\"\\\"prompt\\\":\" + builder + \"}\";\n" + | ||
" return \"{\" +\"\\\"parameters\\\":\" + parameters + \"}\";"); | ||
|
||
PRE_PROCESS_FUNCTIONS.put(TEXT_DOCS_TO_OPENAI_EMBEDDING_INPUT, "\n StringBuilder builder = new StringBuilder();\n" + | ||
" builder.append(\"\\\"\");\n" + | ||
" builder.append(params.text_docs[0]);\n" + | ||
" builder.append(\"\\\"\");\n" + | ||
" def parameters = \"{\" +\"\\\"input\\\":\" + builder + \"}\";\n" + | ||
" return \"{\" +\"\\\"parameters\\\":\" + parameters + \"}\";"); | ||
PRE_PROCESS_FUNCTIONS.put(TEXT_DOCS_TO_COHERE_EMBEDDING_INPUT, cohereTextEmbeddingPreProcess()); | ||
PRE_PROCESS_FUNCTIONS.put(TEXT_DOCS_TO_OPENAI_EMBEDDING_INPUT, openAiTextEmbeddingPreProcess()); | ||
PRE_PROCESS_FUNCTIONS.put(DEFAULT_EMBEDDING_INPUT, openAiTextEmbeddingPreProcess()); | ||
} | ||
|
||
public static boolean contains(String functionName) { | ||
return PRE_PROCESS_FUNCTIONS.containsKey(functionName); | ||
} | ||
|
||
public static String get(String postProcessFunction) { | ||
public static Function<List<String>, Map<String, Object>> get(String postProcessFunction) { | ||
return PRE_PROCESS_FUNCTIONS.get(postProcessFunction); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
public class StringUtils { | ||
|
||
public static final Gson gson; | ||
|
||
static { | ||
gson = new Gson(); | ||
} | ||
|
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.
what do we mean by numberlist? Is that the list of embeddings?
numbers
is bit confusing here.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.
Changed the name to embeddings.
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.
Can we change the error message too? May be:
The list of embeddings is null when using the built-in post-processing function.