|
7 | 7 |
|
8 | 8 | import static org.junit.Assert.*; |
9 | 9 | import static org.mockito.Mockito.*; |
| 10 | +import static org.opensearch.ml.common.agui.AGUIConstants.AGUI_PARAM_CONTEXT; |
10 | 11 | import static org.opensearch.ml.engine.algorithms.agent.MLAgentExecutor.QUESTION; |
11 | 12 |
|
12 | 13 | import java.io.IOException; |
@@ -595,33 +596,97 @@ public void test_ProcessAgentInput_AGUIAgent_WithoutContext() { |
595 | 596 |
|
596 | 597 | @Test |
597 | 598 | public void test_ProcessAgentInput_AGUIAgent_WithContext_LegacyInterface() { |
598 | | - // AGUI agent with legacy LLM interface |
599 | | - // Context has already been appended by AGUIInputConverter before reaching MLAgentExecutor |
| 599 | + // AGUI agent with legacy LLM interface (no model field) |
| 600 | + // Context is passed via AGUI_PARAM_CONTEXT and should be prepended to question |
600 | 601 | MLAgent agent = MLAgent |
601 | 602 | .builder() |
602 | 603 | .name("agui_agent_legacy_context") |
603 | 604 | .type(MLAgentType.AG_UI.name()) |
604 | 605 | .llm(LLMSpec.builder().modelId("gpt-4").build()) |
605 | 606 | .build(); |
606 | 607 |
|
607 | | - // Simulate message with context already appended (as done by AGUIInputConverter) |
608 | 608 | ContentBlock textBlock = new ContentBlock(); |
609 | 609 | textBlock.setType(ContentType.TEXT); |
610 | | - textBlock.setText("Context:\n- Location: San Francisco\n\nWhat is the weather?"); |
| 610 | + textBlock.setText("What is the weather?"); |
611 | 611 | Message message = new Message("user", Collections.singletonList(textBlock)); |
612 | 612 | AgentInput agentInput = new AgentInput(); |
613 | 613 | agentInput.setInput(Collections.singletonList(message)); |
614 | 614 | AgentMLInput agentMLInput = new AgentMLInput("test", null, FunctionName.AGENT, agentInput, null, false); |
615 | 615 |
|
| 616 | + // Set context via params (as AGUIInputConverter stores it) |
| 617 | + Map<String, String> params = new HashMap<>(); |
| 618 | + params.put(AGUI_PARAM_CONTEXT, "[{\"description\":\"Location\",\"value\":\"San Francisco\"}]"); |
| 619 | + agentMLInput.setInputDataset(new RemoteInferenceInputDataSet(params)); |
| 620 | + |
616 | 621 | mlAgentExecutor.processAgentInput(agentMLInput, agent); |
617 | 622 |
|
618 | | - // Verify question contains context that was already appended |
| 623 | + // Verify question contains context prepended by the new code path |
619 | 624 | RemoteInferenceInputDataSet dataset = (RemoteInferenceInputDataSet) agentMLInput.getInputDataset(); |
620 | 625 | String question = dataset.getParameters().get(QUESTION); |
621 | 626 | Assert.assertNotNull(question); |
622 | | - Assert.assertTrue(question.contains("Context:")); |
| 627 | + Assert.assertTrue(question.startsWith("Context: ")); |
623 | 628 | Assert.assertTrue(question.contains("San Francisco")); |
| 629 | + Assert.assertTrue(question.contains("Question: ")); |
| 630 | + Assert.assertTrue(question.contains("What is the weather?")); |
| 631 | + } |
| 632 | + |
| 633 | + @Test |
| 634 | + public void test_ProcessAgentInput_AGUIAgent_NoContext_LegacyInterface() { |
| 635 | + // AGUI agent with legacy LLM interface, no context param |
| 636 | + MLAgent agent = MLAgent |
| 637 | + .builder() |
| 638 | + .name("agui_agent_legacy_no_context") |
| 639 | + .type(MLAgentType.AG_UI.name()) |
| 640 | + .llm(LLMSpec.builder().modelId("gpt-4").build()) |
| 641 | + .build(); |
| 642 | + |
| 643 | + ContentBlock textBlock = new ContentBlock(); |
| 644 | + textBlock.setType(ContentType.TEXT); |
| 645 | + textBlock.setText("What is the weather?"); |
| 646 | + Message message = new Message("user", Collections.singletonList(textBlock)); |
| 647 | + AgentInput agentInput = new AgentInput(); |
| 648 | + agentInput.setInput(Collections.singletonList(message)); |
| 649 | + AgentMLInput agentMLInput = new AgentMLInput("test", null, FunctionName.AGENT, agentInput, null, false); |
| 650 | + |
| 651 | + mlAgentExecutor.processAgentInput(agentMLInput, agent); |
| 652 | + |
| 653 | + RemoteInferenceInputDataSet dataset = (RemoteInferenceInputDataSet) agentMLInput.getInputDataset(); |
| 654 | + String question = dataset.getParameters().get(QUESTION); |
| 655 | + Assert.assertNotNull(question); |
624 | 656 | Assert.assertTrue(question.contains("What is the weather?")); |
| 657 | + Assert.assertFalse(question.contains("Context:")); |
| 658 | + } |
| 659 | + |
| 660 | + @Test |
| 661 | + public void test_ProcessAgentInput_AGUIAgent_EmptyContext_LegacyInterface() { |
| 662 | + // AGUI agent with legacy LLM interface, empty context string |
| 663 | + MLAgent agent = MLAgent |
| 664 | + .builder() |
| 665 | + .name("agui_agent_legacy_bad_context") |
| 666 | + .type(MLAgentType.AG_UI.name()) |
| 667 | + .llm(LLMSpec.builder().modelId("gpt-4").build()) |
| 668 | + .build(); |
| 669 | + |
| 670 | + ContentBlock textBlock = new ContentBlock(); |
| 671 | + textBlock.setType(ContentType.TEXT); |
| 672 | + textBlock.setText("What is the weather?"); |
| 673 | + Message message = new Message("user", Collections.singletonList(textBlock)); |
| 674 | + AgentInput agentInput = new AgentInput(); |
| 675 | + agentInput.setInput(Collections.singletonList(message)); |
| 676 | + AgentMLInput agentMLInput = new AgentMLInput("test", null, FunctionName.AGENT, agentInput, null, false); |
| 677 | + |
| 678 | + // Empty context should not be prepended |
| 679 | + Map<String, String> params = new HashMap<>(); |
| 680 | + params.put(AGUI_PARAM_CONTEXT, ""); |
| 681 | + agentMLInput.setInputDataset(new RemoteInferenceInputDataSet(params)); |
| 682 | + |
| 683 | + mlAgentExecutor.processAgentInput(agentMLInput, agent); |
| 684 | + |
| 685 | + RemoteInferenceInputDataSet dataset = (RemoteInferenceInputDataSet) agentMLInput.getInputDataset(); |
| 686 | + String question = dataset.getParameters().get(QUESTION); |
| 687 | + Assert.assertNotNull(question); |
| 688 | + Assert.assertTrue(question.contains("What is the weather?")); |
| 689 | + Assert.assertFalse(question.contains("Context:")); |
625 | 690 | } |
626 | 691 |
|
627 | 692 | @Test |
|
0 commit comments