Skip to content

Commit

Permalink
fix stopwords npe (opensearch-project#2311)
Browse files Browse the repository at this point in the history
Signed-off-by: Jing Zhang <[email protected]>
  • Loading branch information
jngz-es authored Apr 12, 2024
1 parent abba0ec commit acfb4a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ private void fillStopWordsToMap(@NonNull Guardrail guardrail, Map<String, List<S
return;
}
for (StopWords e : stopWords) {
map.put(e.getIndex(), Arrays.asList(e.getSourceFields()));
if (e.getIndex() != null && e.getSourceFields() != null) {
map.put(e.getIndex(), Arrays.asList(e.getSourceFields()));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public void validateInput() {
Assert.assertFalse(res);
}

@Test
public void validateInitializedStopWordsEmpty() {
stopWords = new StopWords(null, null);
regex = List.of("(.|\n)*stop words(.|\n)*").toArray(new String[0]);
regexPatterns = List.of(Pattern.compile("(.|\n)*stop words(.|\n)*"));
inputGuardrail = new Guardrail(List.of(stopWords), regex);
outputGuardrail = new Guardrail(List.of(stopWords), regex);
guardrails = new Guardrails("test_type", inputGuardrail, outputGuardrail);
mlGuard = new MLGuard(guardrails, xContentRegistry, client);

String input = "\n\nHuman:hello good words.\n\nAssistant:";
Boolean res = mlGuard.validate(input, MLGuard.Type.INPUT);
Assert.assertTrue(res);
}

@Test
public void validateOutput() {
String input = "\n\nHuman:hello stop words.\n\nAssistant:";
Expand Down

0 comments on commit acfb4a9

Please sign in to comment.