diff --git a/spring-ai/src/main/java/com/baeldung/spring/ai/service/PoetryService.java b/spring-ai/src/main/java/com/baeldung/spring/ai/service/PoetryService.java new file mode 100644 index 0000000000..e2760b8950 --- /dev/null +++ b/spring-ai/src/main/java/com/baeldung/spring/ai/service/PoetryService.java @@ -0,0 +1,10 @@ +package com.baeldung.spring.ai.service; + +import com.baeldung.spring.ai.dto.PoetryDto; + +public interface PoetryService { + + String getCatHaiku(); + + PoetryDto getPoetryByGenreAndTheme(String genre, String theme); +} diff --git a/spring-ai/src/main/java/com/baeldung/spring/ai/service/impl/PoetryServiceImpl.java b/spring-ai/src/main/java/com/baeldung/spring/ai/service/impl/PoetryServiceImpl.java new file mode 100644 index 0000000000..26ae407a6e --- /dev/null +++ b/spring-ai/src/main/java/com/baeldung/spring/ai/service/impl/PoetryServiceImpl.java @@ -0,0 +1,48 @@ +package com.baeldung.spring.ai.service.impl; + +import com.baeldung.spring.ai.dto.PoetryDto; +import com.baeldung.spring.ai.service.PoetryService; +import org.springframework.ai.client.AiClient; +import org.springframework.ai.client.AiResponse; +import org.springframework.ai.parser.BeanOutputParser; +import org.springframework.ai.prompt.PromptTemplate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.bind.annotation.GetMapping; + +@Service +public class PoetryServiceImpl implements PoetryService { + + public static final String WRITE_ME_HAIKU_ABOUT_CAT = "Write me Haiku about cat, haiku should contain word cat"; + private final AiClient aiClient; + + @Autowired + public PoetryServiceImpl(AiClient aiClient) { + this.aiClient = aiClient; + } + @Override + public String getCatHaiku() { + return aiClient.generate(WRITE_ME_HAIKU_ABOUT_CAT); + } + + @Override + public PoetryDto getPoetryByGenreAndTheme(String genre, String theme) { + BeanOutputParser poetryDtoBeanOutputParser = new BeanOutputParser<>(PoetryDto.class); + + String promtString = """ + Write me {genre} poetry about {theme} + {format} + """; + + PromptTemplate promptTemplate = new PromptTemplate(promtString); + promptTemplate.add("genre", genre); + promptTemplate.add("theme", theme); + promptTemplate.add("format", poetryDtoBeanOutputParser.getFormat()); + + promptTemplate.setOutputParser(poetryDtoBeanOutputParser); + + AiResponse response = aiClient.generate(promptTemplate.create()); + return poetryDtoBeanOutputParser.parse(response.getGeneration().getText()); + } +} diff --git a/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java b/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java index 7c612e5fee..27c1bad6e2 100644 --- a/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java +++ b/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java @@ -1,6 +1,7 @@ package com.baeldung.spring.ai.web; import com.baeldung.spring.ai.dto.PoetryDto; +import com.baeldung.spring.ai.service.PoetryService; import org.springframework.ai.client.AiClient; import org.springframework.ai.client.AiResponse; import org.springframework.ai.parser.BeanOutputParser; @@ -16,17 +17,16 @@ import org.springframework.web.bind.annotation.RestController; @RequestMapping("ai") public class PoetryController { - public static final String WRITE_ME_HAIKU_ABOUT_CAT = "Write me Haiku about cat, haiku should contain word cat"; - private final AiClient aiClient; + private final PoetryService poetryService; @Autowired - public PoetryController(AiClient aiClient) { - this.aiClient = aiClient; + public PoetryController(PoetryService poetryService) { + this.poetryService = poetryService; } @GetMapping("/cathaiku") public ResponseEntity generateHaiku(){ - return ResponseEntity.ok(aiClient.generate(WRITE_ME_HAIKU_ABOUT_CAT)); + return ResponseEntity.ok(poetryService.getCatHaiku()); } @GetMapping("/poetry") @@ -34,21 +34,6 @@ public class PoetryController { @RequestParam("genre") String genre, @RequestParam("theme") String theme ){ - BeanOutputParser poetryDtoBeanOutputParser = new BeanOutputParser<>(PoetryDto.class); - - String promtString = """ - Write me {genre} poetry about {theme} - {format} - """; - - PromptTemplate promptTemplate = new PromptTemplate(promtString); - promptTemplate.add("genre", genre); - promptTemplate.add("theme", theme); - promptTemplate.add("format", poetryDtoBeanOutputParser.getFormat()); - - promptTemplate.setOutputParser(poetryDtoBeanOutputParser); - - AiResponse response = aiClient.generate(promptTemplate.create()); - return ResponseEntity.ok(poetryDtoBeanOutputParser.parse(response.getGeneration().getText())); + return ResponseEntity.ok(poetryService.getPoetryByGenreAndTheme(genre, theme)); } }