From 75b136573123cd8d30e7053446a2996f2a945b4c Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Sun, 7 Apr 2024 07:28:36 +0000 Subject: [PATCH 1/2] feat : example of output parser --- .../example/ai/controller/ChatController.java | 21 +++++++++ .../ai/model/response/ActorsFilms.java | 5 +++ .../ai/controller/ChatControllerTest.java | 43 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 chatmodel-springai/src/main/java/com/example/ai/model/response/ActorsFilms.java 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 8e80cd5..77b131f 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 @@ -2,6 +2,7 @@ import com.example.ai.model.request.AIChatRequest; import com.example.ai.model.response.AIChatResponse; +import com.example.ai.model.response.ActorsFilms; import java.util.List; import java.util.Map; import org.springframework.ai.chat.ChatClient; @@ -12,9 +13,12 @@ import org.springframework.ai.chat.prompt.Prompt; import org.springframework.ai.chat.prompt.PromptTemplate; import org.springframework.ai.embedding.EmbeddingClient; +import org.springframework.ai.parser.BeanOutputParser; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -61,4 +65,21 @@ AIChatResponse chatWithEmbeddingClient(@RequestBody AIChatRequest aiChatRequest) List embed = embeddingClient.embed(aiChatRequest.query()); return new AIChatResponse(embed.toString()); } + + @GetMapping("/output") + public ActorsFilms generate(@RequestParam(value = "actor", defaultValue = "Jr NTR") String actor) { + BeanOutputParser outputParser = new BeanOutputParser<>(ActorsFilms.class); + + String format = outputParser.getFormat(); + String template = """ + Generate the filmography for the actor {actor}. + {format} + """; + PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("actor", actor, "format", format)); + Prompt prompt = new Prompt(promptTemplate.createMessage()); + ChatResponse response = chatClient.call(prompt); + Generation generation = response.getResult(); + + return outputParser.parse(generation.getOutput().getContent()); + } } diff --git a/chatmodel-springai/src/main/java/com/example/ai/model/response/ActorsFilms.java b/chatmodel-springai/src/main/java/com/example/ai/model/response/ActorsFilms.java new file mode 100644 index 0000000..f9e7d7d --- /dev/null +++ b/chatmodel-springai/src/main/java/com/example/ai/model/response/ActorsFilms.java @@ -0,0 +1,5 @@ +package com.example.ai.model.response; + +import java.util.List; + +public record ActorsFilms(String actor, List movies) {} 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 973f72f..7fd9dc2 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 @@ -1,11 +1,14 @@ package com.example.ai.controller; import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; import com.example.ai.model.request.AIChatRequest; import io.restassured.RestAssured; import io.restassured.http.ContentType; +import org.apache.http.HttpStatus; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInstance; @@ -56,4 +59,44 @@ void chatWithSystemPrompt() { .statusCode(200) .body("answer", containsString("cricket")); } + + @Test + void outputParser() { + given().param("actor", "Jr NTR") + .when() + .get("/api/ai/output") + .then() + .statusCode(HttpStatus.SC_OK) + .contentType(ContentType.JSON) + .body("actor", is("Jr NTR")) + .body( + "movies", + containsInAnyOrder( + "Ninnu Choodalani", + "Student No.1", + "Subbu", + "Aadi", + "Allari Ramudu", + "Naaga", + "Simhadri", + "Samba", + "Andhrawala", + "Rakhi", + "Yamadonga", + "Kantri", + "Adhurs", + "Brindavanam", + "Shakti", + "Oosaravelli", + "Dammu", + "Baadshah", + "Ramayya Vasthavayya", + "Rabhasa", + "Temper", + "Nannaku Prematho", + "Janatha Garage", + "Jai Lava Kusa", + "Aravinda Sametha Veera Raghava", + "RRR")); + } } From 668983f3394ba175c4946c29d480a2445faa5177 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Sun, 7 Apr 2024 07:43:39 +0000 Subject: [PATCH 2/2] feat : change assertion --- .../ai/controller/ChatControllerTest.java | 33 ++----------------- 1 file changed, 3 insertions(+), 30 deletions(-) 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 7fd9dc2..f1afeca 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 @@ -1,8 +1,9 @@ package com.example.ai.controller; import static io.restassured.RestAssured.given; -import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.greaterThanOrEqualTo; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import com.example.ai.model.request.AIChatRequest; @@ -69,34 +70,6 @@ void outputParser() { .statusCode(HttpStatus.SC_OK) .contentType(ContentType.JSON) .body("actor", is("Jr NTR")) - .body( - "movies", - containsInAnyOrder( - "Ninnu Choodalani", - "Student No.1", - "Subbu", - "Aadi", - "Allari Ramudu", - "Naaga", - "Simhadri", - "Samba", - "Andhrawala", - "Rakhi", - "Yamadonga", - "Kantri", - "Adhurs", - "Brindavanam", - "Shakti", - "Oosaravelli", - "Dammu", - "Baadshah", - "Ramayya Vasthavayya", - "Rabhasa", - "Temper", - "Nannaku Prematho", - "Janatha Garage", - "Jai Lava Kusa", - "Aravinda Sametha Veera Raghava", - "RRR")); + .body("movies", hasSize(greaterThanOrEqualTo(25))); } }