|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.ml.rest; |
| 7 | + |
| 8 | +import java.io.IOException; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +import org.apache.hc.core5.http.HttpHeaders; |
| 14 | +import org.apache.hc.core5.http.message.BasicHeader; |
| 15 | +import org.junit.After; |
| 16 | +import org.junit.Before; |
| 17 | +import org.junit.Test; |
| 18 | +import org.opensearch.client.Response; |
| 19 | +import org.opensearch.common.settings.Settings; |
| 20 | +import org.opensearch.core.rest.RestStatus; |
| 21 | +import org.opensearch.ml.utils.TestHelper; |
| 22 | + |
| 23 | +import com.google.common.collect.ImmutableList; |
| 24 | + |
| 25 | +public class RestMLInlineFlowAgentIT extends MLCommonsRestTestCase { |
| 26 | + |
| 27 | + private final String TEST_INDEX_NAME = "test_index"; |
| 28 | + private final String TEST_INDEX_NAME2 = "test_index2"; |
| 29 | + |
| 30 | + private static final String INLINE_AGENT_TEMPLATE = """ |
| 31 | + { |
| 32 | + "name": "test agent", |
| 33 | + "type": "flow", |
| 34 | + "description": "Inline flow agent with list index tool", |
| 35 | + "tools": [ |
| 36 | + { |
| 37 | + "type": "ListIndexTool", |
| 38 | + "name": "list_indices", |
| 39 | + "description": "Tool to list all indices", |
| 40 | + "parameters": {} |
| 41 | + } |
| 42 | + ] |
| 43 | + }"""; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setup() throws IOException, InterruptedException { |
| 47 | + createIndex(TEST_INDEX_NAME, Settings.EMPTY); |
| 48 | + createIndex(TEST_INDEX_NAME2, Settings.EMPTY); |
| 49 | + |
| 50 | + List<String> dataList = new ArrayList<>(); |
| 51 | + dataList.add("{\"name\":\"John Doe\",\"age\":30,\"city\":\"New York\",\"description\":\"Software Engineer\"}"); |
| 52 | + dataList.add("{\"name\":\"Jane Smith\",\"age\":25,\"city\":\"Los Angeles\",\"description\":\"Data Scientist\"}"); |
| 53 | + dataList.add("{\"name\":\"Bob Johnson\",\"age\":35,\"city\":\"Chicago\",\"description\":\"DevOps Engineer\"}"); |
| 54 | + dataList.add("{\"name\":\"Alice Brown\",\"age\":28,\"city\":\"Seattle\",\"description\":\"Product Manager\"}"); |
| 55 | + |
| 56 | + for (String data : dataList) { |
| 57 | + ingestData(TEST_INDEX_NAME, data); |
| 58 | + } |
| 59 | + |
| 60 | + Thread.sleep(1000); |
| 61 | + } |
| 62 | + |
| 63 | + @After |
| 64 | + public void deleteIndex() throws IOException { |
| 65 | + deleteIndex(TEST_INDEX_NAME); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testInlineFlowAgentWithListIndexTool() throws IOException, InterruptedException { |
| 70 | + // Test inline flow agent with listIndexTool |
| 71 | + String requestBody = """ |
| 72 | + { |
| 73 | + "agent": %s, |
| 74 | + "parameters": {} |
| 75 | + }""".formatted(INLINE_AGENT_TEMPLATE); |
| 76 | + |
| 77 | + Response response = TestHelper |
| 78 | + .makeRequest(client(), "POST", "/_plugins/_ml/agents/_execute", null, TestHelper.toHttpEntity(requestBody), List.of()); |
| 79 | + |
| 80 | + assertEquals(RestStatus.OK.getStatus(), response.getStatusLine().getStatusCode()); |
| 81 | + |
| 82 | + Map<String, Object> responseMap = parseResponseToMap(response); |
| 83 | + validateResponseStructure(responseMap); |
| 84 | + |
| 85 | + List<Map<String, Object>> inferenceResults = (List<Map<String, Object>>) responseMap.get("inference_results"); |
| 86 | + assertNotNull(inferenceResults); |
| 87 | + assertTrue(inferenceResults.size() > 0); |
| 88 | + |
| 89 | + Map<String, Object> result = inferenceResults.get(0); |
| 90 | + List<Map<String, Object>> output = (List<Map<String, Object>>) result.get("output"); |
| 91 | + assertNotNull(output); |
| 92 | + assertTrue(output.size() > 0); |
| 93 | + |
| 94 | + Map<String, Object> outputData = output.get(0); |
| 95 | + assertNotNull(outputData.get("result")); |
| 96 | + |
| 97 | + String resultString = (String) outputData.get("result"); |
| 98 | + assertTrue(resultString.contains(TEST_INDEX_NAME)); |
| 99 | + assertTrue(resultString.contains(TEST_INDEX_NAME2)); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testInlineFlowAgentWithListIndexToolAndUserInput() throws IOException, InterruptedException { |
| 104 | + // Test inline flow agent with listIndexTool and user input parameters |
| 105 | + String requestBody = """ |
| 106 | + { |
| 107 | + "agent": %s, |
| 108 | + "parameters" : { |
| 109 | + "index": "test_index" |
| 110 | + } |
| 111 | + }""".formatted(INLINE_AGENT_TEMPLATE); |
| 112 | + |
| 113 | + Response response = TestHelper |
| 114 | + .makeRequest(client(), "POST", "/_plugins/_ml/agents/_execute", null, TestHelper.toHttpEntity(requestBody), List.of()); |
| 115 | + |
| 116 | + assertEquals(RestStatus.OK.getStatus(), response.getStatusLine().getStatusCode()); |
| 117 | + |
| 118 | + Map<String, Object> responseMap = parseResponseToMap(response); |
| 119 | + validateResponseStructure(responseMap); |
| 120 | + |
| 121 | + List<Map<String, Object>> inferenceResults = (List<Map<String, Object>>) responseMap.get("inference_results"); |
| 122 | + assertNotNull(inferenceResults); |
| 123 | + assertTrue(inferenceResults.size() > 0); |
| 124 | + |
| 125 | + Map<String, Object> result = inferenceResults.get(0); |
| 126 | + List<Map<String, Object>> output = (List<Map<String, Object>>) result.get("output"); |
| 127 | + assertNotNull(output); |
| 128 | + assertTrue(output.size() > 0); |
| 129 | + |
| 130 | + Map<String, Object> outputData = output.get(0); |
| 131 | + assertNotNull(outputData.get("result")); |
| 132 | + |
| 133 | + String resultString = (String) outputData.get("result"); |
| 134 | + assertTrue(resultString.contains("test_index")); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testInlineFlowAgentWithMultipleTools() throws IOException, InterruptedException { |
| 139 | + // Test inline flow agent with multiple tools including listIndexTool |
| 140 | + String inlineAgent = """ |
| 141 | + { |
| 142 | + "name": "agent_with_multi_tools", |
| 143 | + "type": "flow", |
| 144 | + "description": "Inline flow agent with multiple tools", |
| 145 | + "tools": [ |
| 146 | + { |
| 147 | + "type": "ListIndexTool", |
| 148 | + "name": "list_indices", |
| 149 | + "description": "Tool to list all indices", |
| 150 | + "parameters": {} |
| 151 | + }, |
| 152 | + { |
| 153 | + "type": "SearchIndexTool", |
| 154 | + "name": "search_test_index", |
| 155 | + "description": "Tool to search test index", |
| 156 | + "parameters": { |
| 157 | + "index": "%s", |
| 158 | + "query": { |
| 159 | + "query": { |
| 160 | + "match_all": {} |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + ] |
| 166 | + }""".formatted(TEST_INDEX_NAME); |
| 167 | + |
| 168 | + String requestBody = """ |
| 169 | + { |
| 170 | + "agent": %s, |
| 171 | + "parameters": {} |
| 172 | + }""".formatted(inlineAgent); |
| 173 | + |
| 174 | + Response response = TestHelper |
| 175 | + .makeRequest( |
| 176 | + client(), |
| 177 | + "POST", |
| 178 | + "/_plugins/_ml/agents/_execute", |
| 179 | + null, |
| 180 | + TestHelper.toHttpEntity(requestBody), |
| 181 | + ImmutableList.of(new BasicHeader(HttpHeaders.USER_AGENT, "Kibana")) |
| 182 | + ); |
| 183 | + |
| 184 | + assertEquals(RestStatus.OK.getStatus(), response.getStatusLine().getStatusCode()); |
| 185 | + |
| 186 | + Map<String, Object> responseMap = parseResponseToMap(response); |
| 187 | + validateResponseStructure(responseMap); |
| 188 | + |
| 189 | + List<Map<String, Object>> inferenceResults = (List<Map<String, Object>>) responseMap.get("inference_results"); |
| 190 | + assertNotNull(inferenceResults); |
| 191 | + assertTrue(inferenceResults.size() > 0); |
| 192 | + System.out.println(gson.toJson(inferenceResults)); |
| 193 | + // search index tool has been executed |
| 194 | + assertEquals("search_test_index", ((List<Map<String, Object>>) inferenceResults.getFirst().get("output")).getFirst().get("name")); |
| 195 | + } |
| 196 | + |
| 197 | + private void validateResponseStructure(Map<String, Object> responseMap) { |
| 198 | + assertNotNull(responseMap); |
| 199 | + assertTrue(responseMap.containsKey("inference_results")); |
| 200 | + } |
| 201 | + |
| 202 | + private void ingestData(String indexName, String data) throws IOException { |
| 203 | + Response response = TestHelper |
| 204 | + .makeRequest(client(), "POST", "/" + indexName + "/_doc", null, TestHelper.toHttpEntity(data), List.of()); |
| 205 | + assertEquals(RestStatus.CREATED.getStatus(), response.getStatusLine().getStatusCode()); |
| 206 | + } |
| 207 | +} |
0 commit comments