Skip to content

Commit 5ef4711

Browse files
committed
Fix: Gracefully handle error when generative_qa_parameters is not provided (opensearch-project#3100)
* fix: gracefully handle error when generative_qa_parameters is not provided Signed-off-by: Pavan Yekbote <mail2pavanyekbote@gmail.com> * fix: spotless apply Signed-off-by: Pavan Yekbote <mail2pavanyekbote@gmail.com> * docs: adding documentation link to error message Signed-off-by: Pavan Yekbote <mail2pavanyekbote@gmail.com> * tests: adding UT to test null params Signed-off-by: Pavan Yekbote <mail2pavanyekbote@gmail.com> --------- Signed-off-by: Pavan Yekbote <mail2pavanyekbote@gmail.com>
1 parent 45e93d7 commit 5ef4711

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

search-processors/src/main/java/org/opensearch/searchpipelines/questionanswering/generative/GenerativeQAProcessorConstants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,8 @@ public class GenerativeQAProcessorConstants {
4343
.boolSetting("plugins.ml_commons.rag_pipeline_feature_enabled", true, Setting.Property.NodeScope, Setting.Property.Dynamic);
4444

4545
public static final String FEATURE_NOT_ENABLED_ERROR_MSG = RAG_PIPELINE_FEATURE_ENABLED.getKey() + " is not enabled.";
46+
47+
public static final String RAG_NULL_GEN_QA_PARAMS_ERROR_MSG = "generative_qa_parameters not found."
48+
+ " Please provide ext.generative_qa_parameters to proceed."
49+
+ " For more info, refer: https://opensearch.org/docs/latest/search-plugins/conversational-search/#step-6-use-the-pipeline-for-rag";
4650
}

search-processors/src/main/java/org/opensearch/searchpipelines/questionanswering/generative/GenerativeQAResponseProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.opensearch.searchpipelines.questionanswering.generative;
1919

2020
import static org.opensearch.ingest.ConfigurationUtils.newConfigurationException;
21+
import static org.opensearch.searchpipelines.questionanswering.generative.GenerativeQAProcessorConstants.RAG_NULL_GEN_QA_PARAMS_ERROR_MSG;
2122

2223
import java.time.Duration;
2324
import java.time.Instant;
@@ -115,6 +116,9 @@ public SearchResponse processResponse(SearchRequest request, SearchResponse resp
115116
}
116117

117118
GenerativeQAParameters params = GenerativeQAParamUtil.getGenerativeQAParameters(request);
119+
if (params == null) {
120+
throw new IllegalArgumentException(RAG_NULL_GEN_QA_PARAMS_ERROR_MSG);
121+
}
118122

119123
Integer timeout = params.getTimeout();
120124
if (timeout == null || timeout == GenerativeQAParameters.SIZE_NULL_VALUE) {

search-processors/src/test/java/org/opensearch/searchpipelines/questionanswering/generative/GenerativeQAResponseProcessorTests.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.mockito.Mockito.mock;
2323
import static org.mockito.Mockito.verify;
2424
import static org.mockito.Mockito.when;
25+
import static org.opensearch.searchpipelines.questionanswering.generative.GenerativeQAProcessorConstants.RAG_NULL_GEN_QA_PARAMS_ERROR_MSG;
2526
import static org.opensearch.searchpipelines.questionanswering.generative.GenerativeQAResponseProcessor.IllegalArgumentMessage;
2627

2728
import java.time.Instant;
@@ -461,4 +462,52 @@ public void testProcessResponseNullValueInteractions() throws Exception {
461462

462463
SearchResponse res = processor.processResponse(request, response);
463464
}
465+
466+
public void testProcessResponseIllegalArgumentForNullParams() throws Exception {
467+
exceptionRule.expect(IllegalArgumentException.class);
468+
exceptionRule.expectMessage(RAG_NULL_GEN_QA_PARAMS_ERROR_MSG);
469+
470+
Client client = mock(Client.class);
471+
Map<String, Object> config = new HashMap<>();
472+
config.put(GenerativeQAProcessorConstants.CONFIG_NAME_MODEL_ID, "dummy-model");
473+
config.put(GenerativeQAProcessorConstants.CONFIG_NAME_CONTEXT_FIELD_LIST, List.of("text"));
474+
475+
GenerativeQAResponseProcessor processor = (GenerativeQAResponseProcessor) new GenerativeQAResponseProcessor.Factory(
476+
client,
477+
alwaysOn
478+
).create(null, "tag", "desc", true, config, null);
479+
480+
ConversationalMemoryClient memoryClient = mock(ConversationalMemoryClient.class);
481+
when(memoryClient.getInteractions(any(), anyInt()))
482+
.thenReturn(List.of(new Interaction("0", Instant.now(), "1", null, null, null, null, null)));
483+
processor.setMemoryClient(memoryClient);
484+
485+
SearchRequest request = new SearchRequest();
486+
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
487+
488+
GenerativeQAParamExtBuilder extBuilder = new GenerativeQAParamExtBuilder();
489+
extBuilder.setParams(null);
490+
request.source(sourceBuilder);
491+
sourceBuilder.ext(List.of(extBuilder));
492+
493+
int numHits = 10;
494+
SearchHit[] hitsArray = new SearchHit[numHits];
495+
for (int i = 0; i < numHits; i++) {
496+
XContentBuilder sourceContent = JsonXContent
497+
.contentBuilder()
498+
.startObject()
499+
.field("_id", String.valueOf(i))
500+
.field("text", "passage" + i)
501+
.field("title", "This is the title for document " + i)
502+
.endObject();
503+
hitsArray[i] = new SearchHit(i, "doc" + i, Map.of(), Map.of());
504+
hitsArray[i].sourceRef(BytesReference.bytes(sourceContent));
505+
}
506+
507+
SearchHits searchHits = new SearchHits(hitsArray, null, 1.0f);
508+
SearchResponseSections internal = new SearchResponseSections(searchHits, null, null, false, false, null, 0);
509+
SearchResponse response = new SearchResponse(internal, null, 1, 1, 0, 1, null, null, null);
510+
511+
SearchResponse res = processor.processResponse(request, response);
512+
}
464513
}

0 commit comments

Comments
 (0)