BAEL-7142, move the logic to Service layer

This commit is contained in:
Ekatereana 2023-11-27 11:01:39 +02:00
parent 4c56ffb754
commit 07b7278310
3 changed files with 64 additions and 21 deletions

View File

@ -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);
}

View File

@ -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());
}
}

View File

@ -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<String> 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<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()));
return ResponseEntity.ok(poetryService.getPoetryByGenreAndTheme(genre, theme));
}
}