Skip to content

Commit 21c0c78

Browse files
committed
add: test cases for fallback
Signed-off-by: Jiaru Jiang <[email protected]>
1 parent b1dea0c commit 21c0c78

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

ml-algorithms/src/main/java/org/opensearch/ml/engine/algorithms/agent/MLChatAgentRunner.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,6 @@ private void runReAct(
423423
lastThought,
424424
maxIterations,
425425
tools,
426-
tmpParameters,
427426
llm,
428427
tenantId
429428
);
@@ -527,7 +526,6 @@ private void runReAct(
527526
lastThought,
528527
maxIterations,
529528
tools,
530-
tmpParameters,
531529
llm,
532530
tenantId
533531
);
@@ -903,7 +901,6 @@ private void handleMaxIterationsReached(
903901
AtomicReference<String> lastThought,
904902
int maxIterations,
905903
Map<String, Tool> tools,
906-
Map<String, String> parameters,
907904
LLMSpec llmSpec,
908905
String tenantId
909906
) {

ml-algorithms/src/test/java/org/opensearch/ml/engine/algorithms/agent/MLChatAgentRunnerTest.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,4 +1368,51 @@ public void testExtractSummaryFromResponse_WithEmptyDataMap() {
13681368
String result = mlChatAgentRunner.extractSummaryFromResponse(response);
13691369
assertEquals(null, result);
13701370
}
1371+
1372+
@Test
1373+
public void testExtractSummaryFromResponse_ThrowsException_FallbackStrategyUsed() {
1374+
LLMSpec llmSpec = LLMSpec.builder().modelId("MODEL_ID").parameters(Map.of("max_iteration", "1")).build();
1375+
MLToolSpec firstToolSpec = MLToolSpec.builder().name(FIRST_TOOL).type(FIRST_TOOL).build();
1376+
final MLAgent mlAgent = MLAgent
1377+
.builder()
1378+
.name("TestAgent")
1379+
.type(MLAgentType.CONVERSATIONAL.name())
1380+
.llm(llmSpec)
1381+
.memory(mlMemorySpec)
1382+
.tools(Arrays.asList(firstToolSpec))
1383+
.build();
1384+
1385+
Mockito.reset(client);
1386+
Mockito.reset(firstTool);
1387+
when(firstTool.getName()).thenReturn(FIRST_TOOL);
1388+
when(firstTool.validate(Mockito.anyMap())).thenReturn(true);
1389+
Mockito.doAnswer(generateToolResponse("First tool response")).when(firstTool).run(Mockito.anyMap(), any());
1390+
1391+
Mockito.doAnswer(getLLMAnswer(ImmutableMap.of("thought", "Analyzing the problem", "action", FIRST_TOOL))).doAnswer(invocation -> {
1392+
ActionListener<Object> listener = invocation.getArgument(2);
1393+
Map<String, Object> invalidDataMap = new HashMap<>();
1394+
invalidDataMap.put("output", new HashMap<>());
1395+
ModelTensor modelTensor = ModelTensor.builder().dataAsMap(invalidDataMap).build();
1396+
ModelTensors modelTensors = ModelTensors.builder().mlModelTensors(Arrays.asList(modelTensor)).build();
1397+
ModelTensorOutput mlModelTensorOutput = ModelTensorOutput.builder().mlModelOutputs(Arrays.asList(modelTensors)).build();
1398+
MLTaskResponse mlTaskResponse = MLTaskResponse.builder().output(mlModelTensorOutput).build();
1399+
listener.onResponse(mlTaskResponse);
1400+
return null;
1401+
}).when(client).execute(any(ActionType.class), any(ActionRequest.class), isA(ActionListener.class));
1402+
1403+
Map<String, String> params = new HashMap<>();
1404+
params.put(MLAgentExecutor.PARENT_INTERACTION_ID, "parent_interaction_id");
1405+
mlChatAgentRunner.run(mlAgent, params, agentActionListener, null);
1406+
1407+
verify(agentActionListener).onResponse(objectCaptor.capture());
1408+
Object capturedResponse = objectCaptor.getValue();
1409+
assertTrue(capturedResponse instanceof ModelTensorOutput);
1410+
1411+
ModelTensorOutput modelTensorOutput = (ModelTensorOutput) capturedResponse;
1412+
List<ModelTensor> agentOutput = modelTensorOutput.getMlModelOutputs().get(1).getMlModelTensors();
1413+
assertEquals(1, agentOutput.size());
1414+
1415+
String response = (String) agentOutput.get(0).getDataAsMap().get("response");
1416+
assertEquals("Agent reached maximum iterations (1) without completing the task. Last thought: Analyzing the problem", response);
1417+
}
13711418
}

0 commit comments

Comments
 (0)