|
24 | 24 | import org.junit.jupiter.api.DisplayName;
|
25 | 25 | import org.junit.jupiter.api.Test;
|
26 | 26 | import org.junit.jupiter.api.TestInstance;
|
| 27 | +import org.junit.jupiter.api.extension.ExtendWith; |
27 | 28 | import org.junit.jupiter.params.ParameterizedTest;
|
28 | 29 | import org.junit.jupiter.params.provider.Arguments;
|
29 | 30 | import org.junit.jupiter.params.provider.MethodSource;
|
30 | 31 | import org.mockito.Mock;
|
31 | 32 | import org.mockito.Mockito;
|
32 |
| -import org.mockito.MockitoAnnotations; |
| 33 | +import org.mockito.junit.jupiter.MockitoExtension; |
33 | 34 | import uk.co.jemos.podam.api.PodamFactory;
|
34 | 35 |
|
35 | 36 | import java.math.BigDecimal;
|
|
42 | 43 | @Slf4j
|
43 | 44 | @TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
44 | 45 | @DisplayName("LlmAsJudge Message Render")
|
| 46 | +@ExtendWith(MockitoExtension.class) |
45 | 47 | class OnlineScoringEngineTest {
|
46 | 48 |
|
47 | 49 | @Mock
|
@@ -99,14 +101,37 @@ class OnlineScoringEngineTest {
|
99 | 101 | }
|
100 | 102 | """.formatted(outputStr).trim();
|
101 | 103 |
|
| 104 | + String edgeCaseTemplate = "Summary: {{summary}}\\nInstruction: {{ instruction }}\\n\\n"; |
| 105 | + String testEvaluatorEdgeCase = """ |
| 106 | + { |
| 107 | + "model": { "name": "gpt-4o", "temperature": 0.3 }, |
| 108 | + "messages": [ |
| 109 | + { "role": "USER", "content": "%s" }, |
| 110 | + { "role": "SYSTEM", "content": "You're a helpful AI, be cordial." } |
| 111 | + ], |
| 112 | + "variables": { |
| 113 | + "summary": "input.questions.question1", |
| 114 | + "instruction": "output.output", |
| 115 | + "nonUsed": "input.questions.question2", |
| 116 | + "toFail1": "metadata.nonexistent.path" |
| 117 | + }, |
| 118 | + "schema": [ |
| 119 | + { "name": "Relevance", "type": "INTEGER", "description": "Relevance of the summary" }, |
| 120 | + { "name": "Conciseness", "type": "DOUBLE", "description": "Conciseness of the summary" }, |
| 121 | + { "name": "Technical Accuracy", "type": "BOOLEAN", "description": "Technical accuracy of the summary" } |
| 122 | + ] |
| 123 | + } |
| 124 | + """ |
| 125 | + .formatted(edgeCaseTemplate).trim(); |
| 126 | + |
| 127 | + private ObjectMapper mapper = new ObjectMapper(); |
| 128 | + |
102 | 129 | @BeforeEach
|
103 | 130 | void setUp() throws JsonProcessingException {
|
104 |
| - MockitoAnnotations.openMocks(this); |
105 | 131 | Mockito.doNothing().when(eventBus).register(Mockito.any());
|
106 | 132 | onlineScoringEventListener = new OnlineScoringEventListener(eventBus, ruleEvaluatorService,
|
107 | 133 | aiProxyService, feedbackScoreService);
|
108 | 134 |
|
109 |
| - var mapper = new ObjectMapper(); |
110 | 135 | evaluatorCode = mapper.readValue(testEvaluator, AutomationRuleEvaluatorLlmAsJudge.LlmAsJudgeCode.class);
|
111 | 136 | trace = Trace.builder().input(mapper.readTree(input)).output(mapper.readTree(output)).build();
|
112 | 137 | }
|
@@ -239,4 +264,25 @@ void testParseResponseIntoFeedbacks(String aiMessage, Integer expectedResults) {
|
239 | 264 |
|
240 | 265 | }
|
241 | 266 | }
|
| 267 | + |
| 268 | + @Test |
| 269 | + @DisplayName("render a message template with edge cases") |
| 270 | + void testRenderEdgeCaseTemplate() throws JsonProcessingException { |
| 271 | + |
| 272 | + var evaluatorEdgeCase = mapper.readValue(testEvaluatorEdgeCase, |
| 273 | + AutomationRuleEvaluatorLlmAsJudge.LlmAsJudgeCode.class); |
| 274 | + |
| 275 | + var renderedMessages = OnlineScoringEngine.renderMessages(evaluatorEdgeCase.messages(), |
| 276 | + evaluatorEdgeCase.variables(), trace); |
| 277 | + |
| 278 | + assertThat(renderedMessages).hasSize(2); |
| 279 | + |
| 280 | + var userMessage = renderedMessages.get(0); |
| 281 | + assertThat(userMessage.getClass()).isEqualTo(UserMessage.class); |
| 282 | + assertThat(((UserMessage) userMessage).singleText()).contains(summaryStr); |
| 283 | + assertThat(((UserMessage) userMessage).singleText()).contains(outputStr); |
| 284 | + |
| 285 | + var systemMessage = renderedMessages.get(1); |
| 286 | + assertThat(systemMessage.getClass()).isEqualTo(SystemMessage.class); |
| 287 | + } |
242 | 288 | }
|
0 commit comments