|
47 | 47 | import org.opensearch.ml.common.agent.MLMemorySpec; |
48 | 48 | import org.opensearch.ml.common.agent.MLToolSpec; |
49 | 49 | import org.opensearch.ml.common.conversation.Interaction; |
| 50 | +import org.opensearch.ml.common.dataset.remote.RemoteInferenceInputDataSet; |
| 51 | +import org.opensearch.ml.common.input.execute.agent.AgentMLInput; |
50 | 52 | import org.opensearch.ml.common.output.model.ModelTensor; |
51 | 53 | import org.opensearch.ml.common.output.model.ModelTensorOutput; |
52 | 54 | import org.opensearch.ml.common.output.model.ModelTensors; |
@@ -327,6 +329,58 @@ public void testExecutionWithHistory() { |
327 | 329 | assertEquals("final result", responseTensor.getDataAsMap().get("response")); |
328 | 330 | } |
329 | 331 |
|
| 332 | + @Test |
| 333 | + public void testMessageHistoryLimits() { |
| 334 | + MLAgent mlAgent = createMLAgentWithTools(); |
| 335 | + |
| 336 | + doAnswer(invocation -> { |
| 337 | + ActionListener<Object> listener = invocation.getArgument(2); |
| 338 | + ModelTensor modelTensor = ModelTensor |
| 339 | + .builder() |
| 340 | + .dataAsMap(ImmutableMap.of("response", "{\"steps\":[\"step1\"], \"result\":\"\"}")) |
| 341 | + .build(); |
| 342 | + ModelTensors modelTensors = ModelTensors.builder().mlModelTensors(Arrays.asList(modelTensor)).build(); |
| 343 | + ModelTensorOutput mlModelTensorOutput = ModelTensorOutput.builder().mlModelOutputs(Arrays.asList(modelTensors)).build(); |
| 344 | + when(mlTaskResponse.getOutput()).thenReturn(mlModelTensorOutput); |
| 345 | + listener.onResponse(mlTaskResponse); |
| 346 | + return null; |
| 347 | + }).when(client).execute(eq(MLPredictionTaskAction.INSTANCE), any(MLPredictionTaskRequest.class), any()); |
| 348 | + |
| 349 | + doAnswer(invocation -> { |
| 350 | + ActionListener<Object> listener = invocation.getArgument(1); |
| 351 | + ModelTensor modelTensor = ModelTensor.builder().dataAsMap(ImmutableMap.of("response", "tool execution result")).build(); |
| 352 | + ModelTensors modelTensors = ModelTensors.builder().mlModelTensors(Arrays.asList(modelTensor)).build(); |
| 353 | + ModelTensorOutput mlModelTensorOutput = ModelTensorOutput.builder().mlModelOutputs(Arrays.asList(modelTensors)).build(); |
| 354 | + when(mlExecuteTaskResponse.getOutput()).thenReturn(mlModelTensorOutput); |
| 355 | + listener.onResponse(mlExecuteTaskResponse); |
| 356 | + return null; |
| 357 | + }).when(client).execute(eq(MLExecuteTaskAction.INSTANCE), any(MLExecuteTaskRequest.class), any()); |
| 358 | + |
| 359 | + doAnswer(invocation -> { |
| 360 | + ActionListener<UpdateResponse> listener = invocation.getArgument(2); |
| 361 | + listener.onResponse(updateResponse); |
| 362 | + return null; |
| 363 | + }).when(mlMemoryManager).updateInteraction(any(), any(), any()); |
| 364 | + |
| 365 | + Map<String, String> params = new HashMap<>(); |
| 366 | + params.put("question", "test question"); |
| 367 | + params.put("memory_id", "test_memory_id"); |
| 368 | + params.put("parent_interaction_id", "test_parent_interaction_id"); |
| 369 | + params.put("message_history_limit", "5"); |
| 370 | + params.put("executor_message_history_limit", "3"); |
| 371 | + mlPlanExecuteAndReflectAgentRunner.run(mlAgent, params, agentActionListener); |
| 372 | + |
| 373 | + verify(conversationIndexMemory).getMessages(any(), eq(5)); |
| 374 | + |
| 375 | + ArgumentCaptor<MLExecuteTaskRequest> executeCaptor = ArgumentCaptor.forClass(MLExecuteTaskRequest.class); |
| 376 | + verify(client).execute(eq(MLExecuteTaskAction.INSTANCE), executeCaptor.capture(), any()); |
| 377 | + |
| 378 | + AgentMLInput agentInput = (AgentMLInput) executeCaptor.getValue().getInput(); |
| 379 | + RemoteInferenceInputDataSet dataset = (RemoteInferenceInputDataSet) agentInput.getInputDataset(); |
| 380 | + Map<String, String> executorParams = dataset.getParameters(); |
| 381 | + assertEquals("3", executorParams.get("message_history_limit")); |
| 382 | + } |
| 383 | + |
330 | 384 | // ToDo: add test case for when max steps is reached |
331 | 385 |
|
332 | 386 | private MLAgent createMLAgentWithTools() { |
|
0 commit comments