BAEL-7142, move the logic to Service layer
This commit is contained in:
parent
4c56ffb754
commit
07b7278310
@ -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,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<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 poetryDtoBeanOutputParser.parse(response.getGeneration().getText());
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package com.baeldung.spring.ai.web;
|
package com.baeldung.spring.ai.web;
|
||||||
|
|
||||||
import com.baeldung.spring.ai.dto.PoetryDto;
|
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.AiClient;
|
||||||
import org.springframework.ai.client.AiResponse;
|
import org.springframework.ai.client.AiResponse;
|
||||||
import org.springframework.ai.parser.BeanOutputParser;
|
import org.springframework.ai.parser.BeanOutputParser;
|
||||||
@ -16,17 +17,16 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
@RequestMapping("ai")
|
@RequestMapping("ai")
|
||||||
public class PoetryController {
|
public class PoetryController {
|
||||||
|
|
||||||
public static final String WRITE_ME_HAIKU_ABOUT_CAT = "Write me Haiku about cat, haiku should contain word cat";
|
private final PoetryService poetryService;
|
||||||
private final AiClient aiClient;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public PoetryController(AiClient aiClient) {
|
public PoetryController(PoetryService poetryService) {
|
||||||
this.aiClient = aiClient;
|
this.poetryService = poetryService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/cathaiku")
|
@GetMapping("/cathaiku")
|
||||||
public ResponseEntity<String> generateHaiku(){
|
public ResponseEntity<String> generateHaiku(){
|
||||||
return ResponseEntity.ok(aiClient.generate(WRITE_ME_HAIKU_ABOUT_CAT));
|
return ResponseEntity.ok(poetryService.getCatHaiku());
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/poetry")
|
@GetMapping("/poetry")
|
||||||
@ -34,21 +34,6 @@ public class PoetryController {
|
|||||||
@RequestParam("genre") String genre,
|
@RequestParam("genre") String genre,
|
||||||
@RequestParam("theme") String theme
|
@RequestParam("theme") String theme
|
||||||
){
|
){
|
||||||
BeanOutputParser<PoetryDto> poetryDtoBeanOutputParser = new BeanOutputParser<>(PoetryDto.class);
|
return ResponseEntity.ok(poetryService.getPoetryByGenreAndTheme(genre, theme));
|
||||||
|
|
||||||
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()));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user