Skip to content
Open
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
@@ -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);

}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package br.com.bravox.eval;

public enum Sentiment {
POSITIVE, NEGATIVE, NEUTRAL;
}
Original file line number Diff line number Diff line change
@@ -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.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package br.com.bravox.eval;

public class StructuredOutputTest {
}