Skip to content

Commit

Permalink
feat : adds sentiment analyzer endpoint (#39)
Browse files Browse the repository at this point in the history
* feat : adds sentiment analyzer endpoint

* Update chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* fix : spotless issue

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
rajadilipkolli and coderabbitai[bot] authored Apr 9, 2024
1 parent 9a15c49 commit 4b9f15a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
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

0 comments on commit 4b9f15a

Please sign in to comment.