From 6d2c0953fd85e7a8ec59418a3a13c3248a12ecc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edd=C3=BA=20Mel=C3=A9ndez?= Date: Wed, 8 May 2024 10:06:00 +0100 Subject: [PATCH] Use output parser format --- .../TestcontainersHelpControllerTest.java | 13 ++++++++----- .../{system-prompt.txt => system-prompt.st} | 16 +--------------- 2 files changed, 9 insertions(+), 20 deletions(-) rename src/test/resources/validator-agent/{system-prompt.txt => system-prompt.st} (57%) diff --git a/src/test/java/com/example/demogenai/TestcontainersHelpControllerTest.java b/src/test/java/com/example/demogenai/TestcontainersHelpControllerTest.java index e6d5cf8..fa5dcf2 100644 --- a/src/test/java/com/example/demogenai/TestcontainersHelpControllerTest.java +++ b/src/test/java/com/example/demogenai/TestcontainersHelpControllerTest.java @@ -19,13 +19,13 @@ import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest(classes = { ContainersConfiguration.class, IngestionConfiguration.class, }, +@SpringBootTest(classes = { ContainersConfiguration.class, IngestionConfiguration.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) class TestcontainersHelpControllerTest { private static final Logger logger = LoggerFactory.getLogger(TestcontainersHelpControllerTest.class); - @Value("classpath:/validator-agent/system-prompt.txt") + @Value("classpath:/validator-agent/system-prompt.st") private Resource systemPrompt; @Value("classpath:/validator-agent/user-prompt.st") @@ -37,7 +37,8 @@ class TestcontainersHelpControllerTest { @Autowired private OllamaChatClient chatClient; - BeanOutputParser outputParser = new BeanOutputParser<>(ValidatorAgentResponse.class); + private final BeanOutputParser outputParser = new BeanOutputParser<>( + ValidatorAgentResponse.class); @Test void contextLoads() { @@ -73,13 +74,15 @@ record ValidatorAgentResponse(String response, String reason) { } private void evaluation(String question, String answer, String reference, String expected) { - var systemMessage = new SystemMessage(this.systemPrompt); + var systemPrompt = new PromptTemplate(this.systemPrompt) + .create(Map.of("format", this.outputParser.getFormat())); + var systemMessage = new SystemMessage(systemPrompt.getContents()); var promptTemplate = new PromptTemplate(this.userPrompt); var userMessage = promptTemplate .createMessage(Map.of("question", question, "answer", answer, "reference", reference)); var prompt = new Prompt(List.of(systemMessage, userMessage)); String content = this.chatClient.call(prompt).getResult().getOutput().getContent(); - ValidatorAgentResponse validation = outputParser.parse(content); + ValidatorAgentResponse validation = this.outputParser.parse(content); logger.info("Validation: {}", validation); assertThat(validation.response()).isEqualTo(expected); } diff --git a/src/test/resources/validator-agent/system-prompt.txt b/src/test/resources/validator-agent/system-prompt.st similarity index 57% rename from src/test/resources/validator-agent/system-prompt.txt rename to src/test/resources/validator-agent/system-prompt.st index 119645d..e2cd961 100644 --- a/src/test/resources/validator-agent/system-prompt.txt +++ b/src/test/resources/validator-agent/system-prompt.st @@ -11,18 +11,4 @@ Follow these instructions: - Respond with 'no' if the answer is not clear or concise - Respond with 'no' if the answer is not based on the reference -Your response must be a json object with the following structure: -{ - "response": "yes", - "reason": "The answer is correct because it is based on the reference provided." -} - -### Example -Question: Is Madrid the capital of Spain? -Answer: No, it's Barcelona. -Reference: The capital of Spain is Madrid -### -Response: { - "response": "no", - "reason": "The answer is incorrect because the reference states that the capital of Spain is Madrid." -} \ No newline at end of file +{format}