diff --git a/spring-ai/.gitignore b/spring-ai/.gitignore new file mode 100644 index 0000000000..dec013dfa4 --- /dev/null +++ b/spring-ai/.gitignore @@ -0,0 +1,12 @@ +#folders# +.idea +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-ai/pom.xml b/spring-ai/pom.xml new file mode 100644 index 0000000000..52eba30508 --- /dev/null +++ b/spring-ai/pom.xml @@ -0,0 +1,61 @@ + + + 4.0.0 + spring-ai + spring-ai + war + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + false + + + + + + + org.springframework.experimental.ai + spring-ai-openai-spring-boot-starter + 0.7.1-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + com.baeldung.spring.ai.SpringAIProjectApplication + JAR + + + + org.apache.maven.plugins + maven-compiler-plugin + + 16 + 16 + + + + + + \ No newline at end of file diff --git a/spring-ai/src/main/java/com/baeldung/spring/ai/SpringAIProjectApplication.java b/spring-ai/src/main/java/com/baeldung/spring/ai/SpringAIProjectApplication.java new file mode 100644 index 0000000000..3dfa800d26 --- /dev/null +++ b/spring-ai/src/main/java/com/baeldung/spring/ai/SpringAIProjectApplication.java @@ -0,0 +1,9 @@ +package com.baeldung.spring.ai; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringAIProjectApplication { + public static void main(String[] args) { SpringApplication.run(SpringAIProjectApplication.class, args);} +} diff --git a/spring-ai/src/main/java/com/baeldung/spring/ai/dto/PoetryDto.java b/spring-ai/src/main/java/com/baeldung/spring/ai/dto/PoetryDto.java new file mode 100644 index 0000000000..b51c511659 --- /dev/null +++ b/spring-ai/src/main/java/com/baeldung/spring/ai/dto/PoetryDto.java @@ -0,0 +1,3 @@ +package com.baeldung.spring.ai.dto; + +public record PoetryDto (String title, String poetry){} 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 new file mode 100644 index 0000000000..b6e510c65b --- /dev/null +++ b/spring-ai/src/main/java/com/baeldung/spring/ai/web/PoetryController.java @@ -0,0 +1,54 @@ +package com.baeldung.spring.ai.web; + +import com.baeldung.spring.ai.dto.PoetryDto; +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.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("ai") +public class PoetryController { + + public static final String WRITE_ME_HAIKU_ABOUT_CAT = "Write me Haiku about cat"; + private final AiClient aiClient; + + @Autowired + public PoetryController(AiClient aiClient) { + this.aiClient = aiClient; + } + + @GetMapping("/cathaiku") + public ResponseEntity generateHaiku(){ + return ResponseEntity.ok(aiClient.generate(WRITE_ME_HAIKU_ABOUT_CAT)); + } + + @GetMapping("/poetry") + public ResponseEntity generatePoetry( + @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())); + } +} diff --git a/spring-ai/src/main/resources/application.yml b/spring-ai/src/main/resources/application.yml new file mode 100644 index 0000000000..b9d9ee1ce8 --- /dev/null +++ b/spring-ai/src/main/resources/application.yml @@ -0,0 +1,3 @@ +spring: + ai: + openai.api-key: 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 \ No newline at end of file