add spring ai controller and sey up project
This commit is contained in:
parent
f2f8e45b96
commit
af543e5861
12
spring-ai/.gitignore
vendored
Normal file
12
spring-ai/.gitignore
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
#folders#
|
||||
.idea
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
61
spring-ai/pom.xml
Normal file
61
spring-ai/pom.xml
Normal file
@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-ai</artifactId>
|
||||
<name>spring-ai</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshots</id>
|
||||
<name>Spring Snapshots</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.experimental.ai</groupId>
|
||||
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
|
||||
<version>0.7.1-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.spring.ai.SpringAIProjectApplication</mainClass>
|
||||
<layout>JAR</layout>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>16</source>
|
||||
<target>16</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@ -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);}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package com.baeldung.spring.ai.dto;
|
||||
|
||||
public record PoetryDto (String title, String poetry){}
|
@ -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<String> generateHaiku(){
|
||||
return ResponseEntity.ok(aiClient.generate(WRITE_ME_HAIKU_ABOUT_CAT));
|
||||
}
|
||||
|
||||
@GetMapping("/poetry")
|
||||
public ResponseEntity<PoetryDto> generatePoetry(
|
||||
@RequestParam("genre") String genre,
|
||||
@RequestParam("theme") String theme
|
||||
){
|
||||
BeanOutputParser<PoetryDto> 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()));
|
||||
}
|
||||
}
|
3
spring-ai/src/main/resources/application.yml
Normal file
3
spring-ai/src/main/resources/application.yml
Normal file
@ -0,0 +1,3 @@
|
||||
spring:
|
||||
ai:
|
||||
openai.api-key: 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
|
Loading…
x
Reference in New Issue
Block a user