-
Notifications
You must be signed in to change notification settings - Fork 140
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
Fix custom prompt substitute with List issue #2871
Fix custom prompt substitute with List issue #2871
Conversation
Signed-off-by: Mingshi Liu <[email protected]>
common/src/main/java/org/opensearch/ml/common/connector/HttpConnector.java
Show resolved
Hide resolved
* @param parameters A map containing key-value pairs where the values may contain toString() method calls. | ||
* @return A new map with the processed values for the toString() method calls. | ||
*/ | ||
public static Map<String, String> parseParameters(Map<String, String> parameters) { |
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.
Add prefix to method parameter list ? For connector, it will be parameters
. For other usecase, it maybe different, like local model could be model_config
public static Map<String, String> parseParameters(Map<String, String> parameters) { | |
public static Map<String, String> parseParameters(String prefix, Map<String, String> parameters) { |
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.
all parameters will be prefix with parameters
in here
ml-commons/common/src/main/java/org/opensearch/ml/common/connector/HttpConnector.java
Line 327 in 59b417b
StringSubstitutor substitutor = new StringSubstitutor(parameters, "${parameters.", "}"); |
I added the test to test when it's parameters.model_config.context
, then it would be taking the prefix as model_config.context
properly, please see this UT
ml-commons/common/src/test/java/org/opensearch/ml/common/utils/StringUtilsTest.java
Lines 316 to 340 in 59b417b
public void testParseParametersListToStringModelConfig() { | |
Map<String, String> parameters = new HashMap<>(); | |
parameters | |
.put( | |
"prompt", | |
"\\n\\nHuman: You are a professional data analyst. You will always answer question based on the given context first. If the answer is not directly shown in the context, you will analyze the data and find the answer. If you don't know the answer, just say I don't know. Context: ${parameters.model_config.context.toString()}. \\n\\n Human: please summarize the documents \\n\\n Assistant:" | |
); | |
ArrayList<String> listOfDocuments = new ArrayList<>(); | |
listOfDocuments.add("document1"); | |
parameters.put("model_config.context", toJson(listOfDocuments)); | |
parseParameters(parameters); | |
System.out.println(parameters.get("context" + TO_STRING_FUNCTION_NAME)); | |
System.out.println(parameters); | |
assertEquals(parameters.get("model_config.context" + TO_STRING_FUNCTION_NAME), "[\\\"document1\\\"]"); | |
String requestBody = "{\"prompt\": \"${parameters.prompt}\"}"; | |
StringSubstitutor substitutor = new StringSubstitutor(parameters, "${parameters.", "}"); | |
requestBody = substitutor.replace(requestBody); | |
System.out.println(requestBody); | |
assertEquals( | |
requestBody, | |
"{\"prompt\": \"\\n\\nHuman: You are a professional data analyst. You will always answer question based on the given context first. If the answer is not directly shown in the context, you will analyze the data and find the answer. If you don't know the answer, just say I don't know. Context: [\\\"document1\\\"]. \\n\\n Human: please summarize the documents \\n\\n Assistant:\"}" | |
); | |
} |
common/src/main/java/org/opensearch/ml/common/utils/StringUtils.java
Outdated
Show resolved
Hide resolved
common/src/test/java/org/opensearch/ml/common/utils/StringUtilsTest.java
Outdated
Show resolved
Hide resolved
47bb6d8
to
59b417b
Compare
Signed-off-by: Mingshi Liu <[email protected]>
59b417b
to
da15967
Compare
This reverts commit f64e3f3 Signed-off-by: Mingshi Liu <[email protected]>
da15967
to
247b955
Compare
…esponse processor (#2871) (#2874) (cherry picked from commit 49d4a01) Co-authored-by: Mingshi Liu <[email protected]>
Shouldn't we use model interface instead of adding |
Description
fix custom prompt substitute with List issue in ml inference search response processor
using toString() method in placeholder to convert list into string, for example,
${parameters.context.toString()}
in the following processors config:reverting https://github.com/opensearch-project/ml-commons/pull/2811/files as a more appropriate approach to fix the toString issue #2839
#2880
Check List
--signoff
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.