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/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; +} 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."); + } +} 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 { +}