From 560b9c39102a9bf9b4c203289f85832d502de10a Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Tue, 9 Apr 2024 06:09:38 +0000 Subject: [PATCH 1/3] feat : adds sentiment analyzer endpoint --- .../com/example/ai/controller/ChatController.java | 5 +++++ .../java/com/example/ai/service/ChatService.java | 14 ++++++++++++++ .../example/ai/controller/ChatControllerTest.java | 12 ++++++++++++ 3 files changed, 31 insertions(+) diff --git a/chatmodel-springai/src/main/java/com/example/ai/controller/ChatController.java b/chatmodel-springai/src/main/java/com/example/ai/controller/ChatController.java index a9ef21d..8812787 100644 --- a/chatmodel-springai/src/main/java/com/example/ai/controller/ChatController.java +++ b/chatmodel-springai/src/main/java/com/example/ai/controller/ChatController.java @@ -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()); diff --git a/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java b/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java index 7691f52..f9295c5 100644 --- a/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java +++ b/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java @@ -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; @@ -68,6 +69,19 @@ public AIChatResponse chatWithSystemPrompt(String query) { return new AIChatResponse(answer); } + public AIChatResponse analyzeSentiment(String query) { + String template = + """ + {query}, You must answer strictly in the following format: one of [POSITIVE, NEGATIVE, SARCASTIC] + """; + AssistantPromptTemplate promptTemplate = new AssistantPromptTemplate(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 embed = embeddingClient.embed(query); return new AIChatResponse(embed.toString()); diff --git a/chatmodel-springai/src/test/java/com/example/ai/controller/ChatControllerTest.java b/chatmodel-springai/src/test/java/com/example/ai/controller/ChatControllerTest.java index a58e818..194f9c7 100644 --- a/chatmodel-springai/src/test/java/com/example/ai/controller/ChatControllerTest.java +++ b/chatmodel-springai/src/test/java/com/example/ai/controller/ChatControllerTest.java @@ -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") From 8a9295fbfed3f443b9626016efe174be3285c64a Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Tue, 9 Apr 2024 11:43:15 +0530 Subject: [PATCH 2/3] Update chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- .../src/main/java/com/example/ai/service/ChatService.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java b/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java index f9295c5..d755631 100644 --- a/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java +++ b/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java @@ -69,12 +69,9 @@ public AIChatResponse chatWithSystemPrompt(String query) { return new AIChatResponse(answer); } + private static final String SENTIMENT_ANALYSIS_TEMPLATE = "{query}, You must answer strictly in the following format: one of [POSITIVE, NEGATIVE, SARCASTIC]"; public AIChatResponse analyzeSentiment(String query) { - String template = - """ - {query}, You must answer strictly in the following format: one of [POSITIVE, NEGATIVE, SARCASTIC] - """; - AssistantPromptTemplate promptTemplate = new AssistantPromptTemplate(template); + AssistantPromptTemplate promptTemplate = new AssistantPromptTemplate(SENTIMENT_ANALYSIS_TEMPLATE); Prompt prompt = promptTemplate.create(Map.of("query", query)); ChatResponse response = chatClient.call(prompt); Generation generation = response.getResult(); From 13a8c4315563520d6cd110cccb53bbc3d353535b Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Tue, 9 Apr 2024 06:15:59 +0000 Subject: [PATCH 3/3] fix : spotless issue --- .../src/main/java/com/example/ai/service/ChatService.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java b/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java index d755631..d659a8a 100644 --- a/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java +++ b/chatmodel-springai/src/main/java/com/example/ai/service/ChatService.java @@ -31,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; @@ -69,7 +71,6 @@ public AIChatResponse chatWithSystemPrompt(String query) { return new AIChatResponse(answer); } - private static final String SENTIMENT_ANALYSIS_TEMPLATE = "{query}, You must answer strictly in the following format: one of [POSITIVE, NEGATIVE, SARCASTIC]"; public AIChatResponse analyzeSentiment(String query) { AssistantPromptTemplate promptTemplate = new AssistantPromptTemplate(SENTIMENT_ANALYSIS_TEMPLATE); Prompt prompt = promptTemplate.create(Map.of("query", query));