Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use output parser format #2

Merged
merged 1 commit into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -37,7 +37,8 @@ class TestcontainersHelpControllerTest {
@Autowired
private OllamaChatClient chatClient;

BeanOutputParser<ValidatorAgentResponse> outputParser = new BeanOutputParser<>(ValidatorAgentResponse.class);
private final BeanOutputParser<ValidatorAgentResponse> outputParser = new BeanOutputParser<>(
ValidatorAgentResponse.class);

@Test
void contextLoads() {
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
{format}