|
27 | 27 | import java.util.Map;
|
28 | 28 | import java.util.function.BooleanSupplier;
|
29 | 29 |
|
| 30 | +import org.opensearch.OpenSearchException; |
30 | 31 | import org.opensearch.action.search.SearchRequest;
|
31 | 32 | import org.opensearch.action.search.SearchResponse;
|
32 | 33 | import org.opensearch.client.Client;
|
|
58 | 59 | */
|
59 | 60 | @Log4j2
|
60 | 61 | public class GenerativeQAResponseProcessor extends AbstractProcessor implements SearchResponseProcessor {
|
61 |
| - |
| 62 | + public static String IllegalArgumentMessage = "Please check the provided generative_qa_parameters are complete and non-null(https://opensearch.org/docs/latest/search-plugins/conversational-search/#rag-pipeline). Messages in the memory can not have Null value for input and response"; |
62 | 63 | private static final int DEFAULT_CHAT_HISTORY_WINDOW = 10;
|
63 | 64 |
|
64 | 65 | private static final int DEFAULT_PROCESSOR_TIME_IN_SECONDS = 30;
|
@@ -148,37 +149,43 @@ public SearchResponse processResponse(SearchRequest request, SearchResponse resp
|
148 | 149 | log.info("system_prompt: {}", systemPrompt);
|
149 | 150 | log.info("user_instructions: {}", userInstructions);
|
150 | 151 | start = Instant.now();
|
151 |
| - ChatCompletionOutput output = llm |
152 |
| - .doChatCompletion( |
153 |
| - LlmIOUtil |
154 |
| - .createChatCompletionInput(systemPrompt, userInstructions, llmModel, llmQuestion, chatHistory, searchResults, timeout) |
155 |
| - ); |
156 |
| - log.info("doChatCompletion complete. ({})", getDuration(start)); |
157 |
| - |
158 |
| - String answer = null; |
159 |
| - String errorMessage = null; |
160 |
| - String interactionId = null; |
161 |
| - if (output.isErrorOccurred()) { |
162 |
| - errorMessage = output.getErrors().get(0); |
163 |
| - } else { |
164 |
| - answer = (String) output.getAnswers().get(0); |
165 |
| - |
166 |
| - if (conversationId != null) { |
167 |
| - start = Instant.now(); |
168 |
| - interactionId = memoryClient |
169 |
| - .createInteraction( |
170 |
| - conversationId, |
171 |
| - llmQuestion, |
172 |
| - PromptUtil.getPromptTemplate(systemPrompt, userInstructions), |
173 |
| - answer, |
174 |
| - GenerativeQAProcessorConstants.RESPONSE_PROCESSOR_TYPE, |
175 |
| - Collections.singletonMap("metadata", jsonArrayToString(searchResults)) |
| 152 | + try { |
| 153 | + ChatCompletionOutput output = llm |
| 154 | + .doChatCompletion( |
| 155 | + LlmIOUtil |
| 156 | + .createChatCompletionInput(systemPrompt, userInstructions, llmModel, llmQuestion, chatHistory, searchResults, timeout) |
176 | 157 | );
|
177 |
| - log.info("Created a new interaction: {} ({})", interactionId, getDuration(start)); |
| 158 | + log.info("doChatCompletion complete. ({})", getDuration(start)); |
| 159 | + |
| 160 | + String answer = null; |
| 161 | + String errorMessage = null; |
| 162 | + String interactionId = null; |
| 163 | + if (output.isErrorOccurred()) { |
| 164 | + errorMessage = output.getErrors().get(0); |
| 165 | + } else { |
| 166 | + answer = (String) output.getAnswers().get(0); |
| 167 | + |
| 168 | + if (conversationId != null) { |
| 169 | + start = Instant.now(); |
| 170 | + interactionId = memoryClient |
| 171 | + .createInteraction( |
| 172 | + conversationId, |
| 173 | + llmQuestion, |
| 174 | + PromptUtil.getPromptTemplate(systemPrompt, userInstructions), |
| 175 | + answer, |
| 176 | + GenerativeQAProcessorConstants.RESPONSE_PROCESSOR_TYPE, |
| 177 | + Collections.singletonMap("metadata", jsonArrayToString(searchResults)) |
| 178 | + ); |
| 179 | + log.info("Created a new interaction: {} ({})", interactionId, getDuration(start)); |
| 180 | + } |
178 | 181 | }
|
179 |
| - } |
180 | 182 |
|
181 |
| - return insertAnswer(response, answer, errorMessage, interactionId); |
| 183 | + return insertAnswer(response, answer, errorMessage, interactionId); |
| 184 | + } catch (NullPointerException nullPointerException) { |
| 185 | + throw new IllegalArgumentException(IllegalArgumentMessage); |
| 186 | + } catch (Exception e) { |
| 187 | + throw new OpenSearchException("GenerativeQAResponseProcessor failed in precessing response"); |
| 188 | + } |
182 | 189 | }
|
183 | 190 |
|
184 | 191 | long getDuration(Instant start) {
|
|
0 commit comments