From e6ece9ce5e7fce6a7d9493e411fee7c36e3c0046 Mon Sep 17 00:00:00 2001 From: robsonoduarte Date: Sat, 18 Jul 2026 01:32:52 -0300 Subject: [PATCH 1/3] spring ai - evaluator --- .../src/main/java/br/com/bravox/eval/Sentiment.java | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 spring-ia-course/src/main/java/br/com/bravox/eval/Sentiment.java diff --git a/spring-ia-course/src/main/java/br/com/bravox/eval/Sentiment.java b/spring-ia-course/src/main/java/br/com/bravox/eval/Sentiment.java new file mode 100644 index 0000000..7e623ca --- /dev/null +++ b/spring-ia-course/src/main/java/br/com/bravox/eval/Sentiment.java @@ -0,0 +1,5 @@ +package br.com.bravox.eval; + +public enum Sentiment { + POSITIVE, NEGATIVE, NEUTRAL; +} From 25005a4f354e164a717ba1016a0c5480c2306bb6 Mon Sep 17 00:00:00 2001 From: robsonoduarte Date: Sat, 8 Aug 2026 01:27:25 -0300 Subject: [PATCH 2/3] spring ai - evaluator --- .../br/com/bravox/eval/ReviewService.java | 29 +++++++++++++++ .../bravox/eval/SentimentAnalysisTest.java | 35 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 spring-ia-course/src/main/java/br/com/bravox/eval/ReviewService.java create mode 100644 spring-ia-course/src/test/java/br/com/bravox/eval/SentimentAnalysisTest.java diff --git a/spring-ia-course/src/main/java/br/com/bravox/eval/ReviewService.java b/spring-ia-course/src/main/java/br/com/bravox/eval/ReviewService.java new file mode 100644 index 0000000..a8b3e12 --- /dev/null +++ b/spring-ia-course/src/main/java/br/com/bravox/eval/ReviewService.java @@ -0,0 +1,29 @@ +package br.com.bravox.eval; + +import org.springframework.ai.chat.client.ChatClient; +import org.springframework.ai.openai.OpenAiChatOptions; +import org.springframework.stereotype.Service; + +@Service +public class ReviewService { + private final ChatClient chatClient; + + public ReviewService(ChatClient.Builder builder) { + this.chatClient = builder + .defaultOptions(OpenAiChatOptions.builder().temperature(0.1d).build()) + .build(); + } + + public Sentiment classifySentiment(String review) { + String systemPrompt = """ + Classify the sentiment of the following text as POSITIVE, NEGATIVE, or NEUTRAL. \ + Your response must be only one of these three words."""; + var sentiment = chatClient.prompt() + .system(systemPrompt) + .user(review) + .call().content(); + return Sentiment.valueOf(sentiment); + + } +} + diff --git a/spring-ia-course/src/test/java/br/com/bravox/eval/SentimentAnalysisTest.java b/spring-ia-course/src/test/java/br/com/bravox/eval/SentimentAnalysisTest.java new file mode 100644 index 0000000..3286f98 --- /dev/null +++ b/spring-ia-course/src/test/java/br/com/bravox/eval/SentimentAnalysisTest.java @@ -0,0 +1,35 @@ +package br.com.bravox.eval; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@SpringBootTest +public class SentimentAnalysisTest { + + @Autowired + private ReviewService reviewService; + + @Test + void testPositiveSentiment() { + var positiveReview = "I absolutely love the hotel, it was amazing"; + var sentiment = reviewService.classifySentiment(positiveReview); + assertEquals(Sentiment.POSITIVE, sentiment, "the sentiment should be classified as positive."); + } + + @Test + void testNegativeSentiment() { + var negativeReview = "This is the worst experience I've ever had. The product is terrible and broke immediately."; + var result = reviewService.classifySentiment(negativeReview); + assertEquals(Sentiment.NEGATIVE, result, "The sentiment should be classified as NEGATIVE."); + } + + @Test + void testNeutralSentiment() { + var neutralReview = "The product is okay. It does what it says but nothing more."; + var result = reviewService.classifySentiment(neutralReview); + assertEquals(Sentiment.NEUTRAL, result, "The sentiment should be classified as NEUTRAL."); + } +} From 5c787bcb836bc685446f55ac97203de160c29db4 Mon Sep 17 00:00:00 2001 From: robsonoduarte Date: Sat, 8 Aug 2026 01:28:23 -0300 Subject: [PATCH 3/3] spring ai - evaluator --- .../test/java/br/com/bravox/eval/StructuredOutputTest.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 spring-ia-course/src/test/java/br/com/bravox/eval/StructuredOutputTest.java diff --git a/spring-ia-course/src/test/java/br/com/bravox/eval/StructuredOutputTest.java b/spring-ia-course/src/test/java/br/com/bravox/eval/StructuredOutputTest.java new file mode 100644 index 0000000..4befb17 --- /dev/null +++ b/spring-ia-course/src/test/java/br/com/bravox/eval/StructuredOutputTest.java @@ -0,0 +1,4 @@ +package br.com.bravox.eval; + +public class StructuredOutputTest { +}