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

feat : adds sentiment analyzer endpoint #39

Merged
merged 3 commits into from
Apr 9, 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 @@ -37,6 +37,11 @@ AIChatResponse chatWithSystemPrompt(@RequestBody AIChatRequest aiChatRequest) {
return chatService.chatWithSystemPrompt(aiChatRequest.query());
}

@PostMapping("/sentiment/analyze")
AIChatResponse sentimentAnalyzer(@RequestBody AIChatRequest aiChatRequest) {
return chatService.analyzeSentiment(aiChatRequest.query());
}

@PostMapping("/emebedding-client-conversion")
AIChatResponse chatWithEmbeddingClient(@RequestBody AIChatRequest aiChatRequest) {
return chatService.getEmbeddings(aiChatRequest.query());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.ai.chat.Generation;
import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.AssistantPromptTemplate;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.ai.document.Document;
Expand All @@ -30,6 +31,8 @@
public class ChatService {

private static final Logger logger = LoggerFactory.getLogger(ChatService.class);
private static final String SENTIMENT_ANALYSIS_TEMPLATE =
"{query}, You must answer strictly in the following format: one of [POSITIVE, NEGATIVE, SARCASTIC]";

@Value("classpath:/data/restaurants.json")
private Resource restaurantsResource;
Expand Down Expand Up @@ -68,6 +71,15 @@ public AIChatResponse chatWithSystemPrompt(String query) {
return new AIChatResponse(answer);
}

public AIChatResponse analyzeSentiment(String query) {
AssistantPromptTemplate promptTemplate = new AssistantPromptTemplate(SENTIMENT_ANALYSIS_TEMPLATE);
Prompt prompt = promptTemplate.create(Map.of("query", query));
ChatResponse response = chatClient.call(prompt);
Generation generation = response.getResult();
String answer = (generation != null) ? generation.getOutput().getContent() : "";
return new AIChatResponse(answer);
}

public AIChatResponse getEmbeddings(String query) {
List<Double> embed = embeddingClient.embed(query);
return new AIChatResponse(embed.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ void chatWithSystemPrompt() {
.body("answer", containsString("cricket"));
}

@Test
void sentimentAnalyzer() {
given().contentType(ContentType.JSON)
.body(new AIChatRequest("Why did the Python programmer go broke? Because he couldn't C#"))
.when()
.post("/api/ai/sentiment/analyze")
.then()
.statusCode(HttpStatus.SC_OK)
.contentType(ContentType.JSON)
.body("answer", is("SARCASTIC"));
}

@Test
void outputParser() {
given().param("actor", "Jr NTR")
Expand Down