|
41 | 41 | import org.opensearch.core.action.ActionListener;
|
42 | 42 | import org.opensearch.core.xcontent.NamedXContentRegistry;
|
43 | 43 | import org.opensearch.core.xcontent.XContentParser;
|
| 44 | +import org.opensearch.index.query.MatchAllQueryBuilder; |
44 | 45 | import org.opensearch.index.query.QueryBuilder;
|
45 | 46 | import org.opensearch.index.query.QueryBuilders;
|
46 | 47 | import org.opensearch.index.query.RangeQueryBuilder;
|
@@ -1316,7 +1317,7 @@ public void onFailure(Exception e) {
|
1316 | 1317 |
|
1317 | 1318 | /**
|
1318 | 1319 | * Tests the successful rewriting of a complex nested array in query extension based on the model output.
|
1319 |
| - * verify the pipelineConext is set from the extension |
| 1320 | + * verify the pipelineContext is set from the extension |
1320 | 1321 | * @throws Exception if an error occurs during the test
|
1321 | 1322 | */
|
1322 | 1323 | public void testExecute_rewriteTermQueryReadAndWriteComplexNestedArrayToExtensionSuccess() throws Exception {
|
@@ -1612,6 +1613,107 @@ public void onFailure(Exception e) {
|
1612 | 1613 | requestProcessor.processRequestAsync(request, requestContext, Listener);
|
1613 | 1614 | }
|
1614 | 1615 |
|
| 1616 | + /** |
| 1617 | + * Tests ML Processor can return a OpenSearch Query correctly when performing a rewrite query. |
| 1618 | + * |
| 1619 | + * This simulates a real world scenario where user has a llm return a OpenSearch Query to help them generate a new |
| 1620 | + * query based on the context given in the prompt. |
| 1621 | + * |
| 1622 | + * @throws Exception when an error occurs on the test |
| 1623 | + */ |
| 1624 | + public void testExecute_rewriteTermQueryWithNewQuerySuccess() throws Exception { |
| 1625 | + String modelInputField = "inputs"; |
| 1626 | + String originalQueryField = "query.term.text.value"; |
| 1627 | + String newQueryField = "llm_query"; |
| 1628 | + String modelInferenceJsonPathInput = "$.inference_results[0].output[0].dataAsMap.content[0].text"; |
| 1629 | + |
| 1630 | + String queryTemplate = "${llm_query}"; |
| 1631 | + |
| 1632 | + String llmQuery = "{\n" + |
| 1633 | + " \"query\": {\n" + |
| 1634 | + " \"match_all\": {}\n" + |
| 1635 | + " }\n" + |
| 1636 | + "}"; |
| 1637 | + Map content = Map.of("content", List.of(Map.of("text", llmQuery))); |
| 1638 | + |
| 1639 | + |
| 1640 | + List<Map<String, String>> optionalInputMap = new ArrayList<>(); |
| 1641 | + Map<String, String> input = new HashMap<>(); |
| 1642 | + input.put(modelInputField, originalQueryField); |
| 1643 | + optionalInputMap.add(input); |
| 1644 | + |
| 1645 | + List<Map<String, String>> optionalOutputMap = new ArrayList<>(); |
| 1646 | + Map<String, String> output = new HashMap<>(); |
| 1647 | + output.put(newQueryField, modelInferenceJsonPathInput); |
| 1648 | + optionalOutputMap.add(output); |
| 1649 | + |
| 1650 | + MLInferenceSearchRequestProcessor requestProcessor = new MLInferenceSearchRequestProcessor( |
| 1651 | + "model1", |
| 1652 | + queryTemplate, |
| 1653 | + null, |
| 1654 | + null, |
| 1655 | + optionalInputMap, |
| 1656 | + optionalOutputMap, |
| 1657 | + null, |
| 1658 | + DEFAULT_MAX_PREDICTION_TASKS, |
| 1659 | + PROCESSOR_TAG, |
| 1660 | + DESCRIPTION, |
| 1661 | + false, |
| 1662 | + "remote", |
| 1663 | + true, |
| 1664 | + false, |
| 1665 | + "{ \"parameters\": ${ml_inference.parameters} }", |
| 1666 | + client, |
| 1667 | + TEST_XCONTENT_REGISTRY_FOR_QUERY |
| 1668 | + ); |
| 1669 | + |
| 1670 | + /** |
| 1671 | + * { |
| 1672 | + * "inference_results" : [ { |
| 1673 | + * "output" : [ { |
| 1674 | + * "name" : "response", |
| 1675 | + * "dataAsMap" : { |
| 1676 | + * "content": [ |
| 1677 | + * "text": "{\"query\": \"match_all\" : {}}" |
| 1678 | + * } |
| 1679 | + * } ] |
| 1680 | + * } ] |
| 1681 | + * } |
| 1682 | + */ |
| 1683 | + ModelTensor modelTensor = ModelTensor |
| 1684 | + .builder() |
| 1685 | + .name("response") |
| 1686 | + .dataAsMap(content) |
| 1687 | + .build(); |
| 1688 | + ModelTensors modelTensors = ModelTensors.builder().mlModelTensors(Arrays.asList(modelTensor)).build(); |
| 1689 | + ModelTensorOutput mlModelTensorOutput = ModelTensorOutput.builder().mlModelOutputs(Arrays.asList(modelTensors)).build(); |
| 1690 | + |
| 1691 | + doAnswer(invocation -> { |
| 1692 | + ActionListener<MLTaskResponse> actionListener = invocation.getArgument(2); |
| 1693 | + actionListener.onResponse(MLTaskResponse.builder().output(mlModelTensorOutput).build()); |
| 1694 | + return null; |
| 1695 | + }).when(client).execute(any(), any(), any()); |
| 1696 | + |
| 1697 | + QueryBuilder incomingQuery = new TermQueryBuilder("text", "foo"); |
| 1698 | + SearchSourceBuilder source = new SearchSourceBuilder().query(incomingQuery); |
| 1699 | + SearchRequest sampleRequest = new SearchRequest().source(source); |
| 1700 | + |
| 1701 | + ActionListener<SearchRequest> Listener = new ActionListener<>() { |
| 1702 | + @Override |
| 1703 | + public void onResponse(SearchRequest newSearchRequest) { |
| 1704 | + MatchAllQueryBuilder expectedQuery = new MatchAllQueryBuilder(); |
| 1705 | + assertEquals(expectedQuery, newSearchRequest.source().query()); |
| 1706 | + } |
| 1707 | + |
| 1708 | + @Override |
| 1709 | + public void onFailure(Exception e) { |
| 1710 | + throw new RuntimeException("Failed in executing processRequestAsync.", e); |
| 1711 | + } |
| 1712 | + }; |
| 1713 | + |
| 1714 | + requestProcessor.processRequestAsync(sampleRequest, requestContext, Listener); |
| 1715 | + } |
| 1716 | + |
1615 | 1717 | /**
|
1616 | 1718 | * Tests when there are two optional input fields
|
1617 | 1719 | * but only the second optional input is present in the query
|
|
0 commit comments