-
Notifications
You must be signed in to change notification settings - Fork 143
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
Guardrails for remote model input and output #2209
Conversation
Signed-off-by: Jing Zhang <[email protected]>
Signed-off-by: Jing Zhang <[email protected]>
Signed-off-by: Jing Zhang <[email protected]>
common/src/main/java/org/opensearch/ml/common/model/MLGuard.java
Outdated
Show resolved
Hide resolved
common/src/main/java/org/opensearch/ml/common/model/MLGuard.java
Outdated
Show resolved
Hide resolved
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.
I think we need to create more of a guardrails framework here. Down the road we would add many more different guardrail mechanisms as the LLM space with guardrails is new and constantly evolving. We should think with that in mind.
public class Guardrail implements ToXContentObject { | ||
public static final String STOP_WORDS_FIELD = "stop_words"; | ||
public static final String REGEX_FIELD = "regex"; |
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.
Guardrail should be a super class and stop words is a sub class for a type of guardrail and same with regex being a sub class of type guardrail. This would be easier to integrate new types of guardrails and create a framework around it.
public Boolean validateRegexList(String input, List<Pattern> regexPatterns) { | ||
for (Pattern pattern : regexPatterns) { | ||
if (!validateRegex(input, pattern)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public Boolean validateRegex(String input, Pattern pattern) { | ||
Matcher matcher = pattern.matcher(input); | ||
return !matcher.matches(); | ||
} | ||
|
||
public Boolean validateStopWords(String input, Map<String, List<String>> stopWordsIndices) { | ||
for (Map.Entry entry : stopWordsIndices.entrySet()) { | ||
if (!validateStopWordsSingleIndex(input, (String) entry.getKey(), (List<String>) entry.getValue())) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public Boolean validateStopWordsSingleIndex(String input, String indexName, List<String> fieldNames) { |
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.
These function should be a part of that specific guardrail model class and we just call a validate function on the different guardrail component and that will determine how to correctly validate the input.
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.
Good suggestion, will refactor it in next pr.
I am fine with these changes happening post this PR |
Signed-off-by: Jing Zhang <[email protected]>
* guardrails Signed-off-by: Jing Zhang <[email protected]> * update guardrails Signed-off-by: Jing Zhang <[email protected]> * bug fix Signed-off-by: Jing Zhang <[email protected]> * add some UT Signed-off-by: Jing Zhang <[email protected]> * change stop words search to unblocking way Signed-off-by: Jing Zhang <[email protected]> * add more UT Signed-off-by: Jing Zhang <[email protected]> * address comments Signed-off-by: Jing Zhang <[email protected]> * add latch countdown when catching exception Signed-off-by: Jing Zhang <[email protected]> --------- Signed-off-by: Jing Zhang <[email protected]> (cherry picked from commit 2d401bc)
* guardrails Signed-off-by: Jing Zhang <[email protected]> * update guardrails Signed-off-by: Jing Zhang <[email protected]> * bug fix Signed-off-by: Jing Zhang <[email protected]> * add some UT Signed-off-by: Jing Zhang <[email protected]> * change stop words search to unblocking way Signed-off-by: Jing Zhang <[email protected]> * add more UT Signed-off-by: Jing Zhang <[email protected]> * address comments Signed-off-by: Jing Zhang <[email protected]> * add latch countdown when catching exception Signed-off-by: Jing Zhang <[email protected]> --------- Signed-off-by: Jing Zhang <[email protected]> (cherry picked from commit 2d401bc) Co-authored-by: Jing Zhang <[email protected]>
@@ -265,7 +265,10 @@ public class CommonValue { | |||
+ MLModel.CONNECTOR_FIELD | |||
+ "\": {" + ML_CONNECTOR_INDEX_FIELDS + " }\n}," | |||
+ USER_FIELD_MAPPING | |||
+ " }\n" | |||
+ " },\n" |
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.
you added comma here.
searchSourceBuilder.parseXContent(queryParser); | ||
searchSourceBuilder.size(1); //Only need 1 doc returned, if hit. | ||
searchRequest = new SearchRequest().source(searchSourceBuilder).indices(indexName); | ||
context.restore(); |
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.
Why restore context here? I see already have restore in action listener in line 147
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.
.of("query", Map.of("percolate", Map.of("field", "query", "document", documentMap))); | ||
CountDownLatch latch = new CountDownLatch(1); | ||
|
||
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) { |
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 if the indexName
is not system index ? I think we should only stash context for system index.
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.
Description
Add guardrails for remote model input and output. We support two ways stop words and regex.
Example:
Issues Resolved
[List any issues this PR will resolve]
Check List
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.