Merge pull request #15307 from Ekatereana/BAEL-7142-spring-ai
Bael 7142 spring ai module
This commit is contained in:
commit
11c122fda5
2
pom.xml
2
pom.xml
|
@ -841,6 +841,7 @@
|
|||
<module>spring-5</module>
|
||||
<module>spring-activiti</module>
|
||||
<module>spring-actuator</module>
|
||||
<module>spring-ai</module>
|
||||
<module>spring-aop-2</module>
|
||||
<!--<module>spring-aop</module>-->
|
||||
<module>spring-batch-2</module>
|
||||
|
@ -1093,6 +1094,7 @@
|
|||
<module>spring-5</module>
|
||||
<module>spring-activiti</module>
|
||||
<module>spring-actuator</module>
|
||||
<module>spring-ai</module>
|
||||
<module>spring-aop-2</module>
|
||||
<!--<module>spring-aop</module>-->
|
||||
<module>spring-batch-2</module>
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
#folders#
|
||||
.idea
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
|
@ -0,0 +1,63 @@
|
|||
<?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-3</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-3</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>
|
||||
<excludes>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"id": "df61838b-eb6f-4243-87ee-ca02c77e8646",
|
||||
"name": "Spring_AI_Poetry",
|
||||
"values": [
|
||||
{
|
||||
"key": "baseUrl",
|
||||
"value": "localhost:8080",
|
||||
"type": "default",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "genre",
|
||||
"value": "liric",
|
||||
"enabled": true
|
||||
},
|
||||
{
|
||||
"key": "theme",
|
||||
"value": "flames",
|
||||
"enabled": true
|
||||
}
|
||||
],
|
||||
"_postman_variable_scope": "environment",
|
||||
"_postman_exported_at": "2023-11-26T19:49:21.755Z",
|
||||
"_postman_exported_using": "Postman/10.20.3"
|
||||
}
|
|
@ -0,0 +1,56 @@
|
|||
{
|
||||
"info": {
|
||||
"_postman_id": "f4282fac-bfe5-45b9-aae6-5ea7c43528ee",
|
||||
"name": "spring-ai",
|
||||
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
|
||||
"_exporter_id": "9576856"
|
||||
},
|
||||
"item": [
|
||||
{
|
||||
"name": "Generate poetry with genre and theme",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "{{baseUrl}}/ai/poetry?genre={{genre}}&theme={{theme}}",
|
||||
"host": [
|
||||
"{{baseUrl}}"
|
||||
],
|
||||
"path": [
|
||||
"ai",
|
||||
"poetry"
|
||||
],
|
||||
"query": [
|
||||
{
|
||||
"key": "genre",
|
||||
"value": "{{genre}}"
|
||||
},
|
||||
{
|
||||
"key": "theme",
|
||||
"value": "{{theme}}"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
},
|
||||
{
|
||||
"name": "Generate haiku about cats",
|
||||
"request": {
|
||||
"method": "GET",
|
||||
"header": [],
|
||||
"url": {
|
||||
"raw": "{{baseUrl}}/ai/cathaiku",
|
||||
"host": [
|
||||
"{{baseUrl}}"
|
||||
],
|
||||
"path": [
|
||||
"ai",
|
||||
"cathaiku"
|
||||
]
|
||||
}
|
||||
},
|
||||
"response": []
|
||||
}
|
||||
]
|
||||
}
|
|
@ -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, String genre, String theme) {}
|
|
@ -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);
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
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 start with the word cat obligatory""";
|
||||
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<PoetryDto> poetryDtoBeanOutputParser = new BeanOutputParser<>(PoetryDto.class);
|
||||
|
||||
String promptString = """
|
||||
Write me {genre} poetry about {theme}
|
||||
{format}
|
||||
""";
|
||||
|
||||
PromptTemplate promptTemplate = new PromptTemplate(promptString);
|
||||
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());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.baeldung.spring.ai.web;
|
||||
|
||||
import com.theokanning.openai.OpenAiHttpException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ProblemDetail;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ExceptionTranslator extends ResponseEntityExceptionHandler {
|
||||
|
||||
public static final String OPEN_AI_CLIENT_RAISED_EXCEPTION = "Open AI client raised exception";
|
||||
|
||||
@ExceptionHandler(OpenAiHttpException.class)
|
||||
ProblemDetail handleOpenAiHttpException(OpenAiHttpException ex) {
|
||||
HttpStatus status = Optional
|
||||
.ofNullable(HttpStatus.resolve(ex.statusCode))
|
||||
.orElse(HttpStatus.BAD_REQUEST);
|
||||
|
||||
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(status, ex.getMessage());
|
||||
problemDetail.setTitle(OPEN_AI_CLIENT_RAISED_EXCEPTION);
|
||||
return problemDetail;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
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;
|
||||
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 {
|
||||
|
||||
private final PoetryService poetryService;
|
||||
|
||||
@Autowired
|
||||
public PoetryController(PoetryService poetryService) {
|
||||
this.poetryService = poetryService;
|
||||
}
|
||||
|
||||
@GetMapping("/cathaiku")
|
||||
public ResponseEntity<String> generateHaiku(){
|
||||
return ResponseEntity.ok(poetryService.getCatHaiku());
|
||||
}
|
||||
|
||||
@GetMapping("/poetry")
|
||||
public ResponseEntity<PoetryDto> generatePoetry(
|
||||
@RequestParam("genre") String genre,
|
||||
@RequestParam("theme") String theme
|
||||
){
|
||||
return ResponseEntity.ok(poetryService.getPoetryByGenreAndTheme(genre, theme));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
spring:
|
||||
ai:
|
||||
openai.api-key: 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
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.spring.ai.web;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.ai.client.AiClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@AutoConfigureMockMvc
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class PoetryControllerManualTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
@Autowired
|
||||
private AiClient aiClient;
|
||||
|
||||
@Test
|
||||
public void givenGetCatHaiku_whenCallingAiClient_thenCorrect() throws Exception {
|
||||
mockMvc.perform(get("/ai/cathaiku"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(containsStringIgnoringCase("cat")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGetPoetryWithGenreAndTheme_whenCallingAiClient_thenCorrect() throws Exception {
|
||||
String genre = "lyric";
|
||||
String theme = "coffee";
|
||||
mockMvc.perform(get("/ai/poetry?genre={genre}&theme={theme}", genre, theme))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$.genre").value(containsStringIgnoringCase(genre)))
|
||||
.andExpect(jsonPath("$.theme").value(containsStringIgnoringCase(theme)))
|
||||
.andExpect(jsonPath("$.poetry").isNotEmpty())
|
||||
.andExpect(jsonPath("$.title").exists());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
spring:
|
||||
ai:
|
||||
openai.api-key: "mock-key-token"
|
Loading…
Reference in New Issue