|
| 1 | +package fallguys.fallaylist.adapter.out.external.ai; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import fallguys.fallaylist.adapter.out.external.ai.dto.AiValleyRecommendationResponse; |
| 6 | +import fallguys.fallaylist.application.recommendation.dto.KeywordAiPromt; |
| 7 | +import fallguys.fallaylist.application.recommendation.dto.ValleyAiPromt; |
| 8 | +import fallguys.fallaylist.application.recommendation.required.AiRecommendationPort; |
| 9 | +import java.io.IOException; |
| 10 | +import java.nio.charset.StandardCharsets; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.List; |
| 13 | +import java.util.Map; |
| 14 | +import lombok.RequiredArgsConstructor; |
| 15 | +import lombok.extern.slf4j.Slf4j; |
| 16 | +import org.springframework.ai.chat.client.ChatClient; |
| 17 | +import org.springframework.ai.chat.prompt.PromptTemplate; |
| 18 | +import org.springframework.beans.factory.annotation.Value; |
| 19 | +import org.springframework.core.ParameterizedTypeReference; |
| 20 | +import org.springframework.core.io.Resource; |
| 21 | +import org.springframework.stereotype.Component; |
| 22 | +import org.springframework.util.StreamUtils; |
| 23 | + |
| 24 | +@Slf4j |
| 25 | +@Component |
| 26 | +@RequiredArgsConstructor |
| 27 | +public class AiRecommendationAdapter implements AiRecommendationPort { |
| 28 | + |
| 29 | + @Value("classpath:/prompts/system-message.st") |
| 30 | + private Resource systemMessageResource; |
| 31 | + |
| 32 | + @Value("classpath:/prompts/user-message.st") |
| 33 | + private Resource userMessageResource; |
| 34 | + |
| 35 | + private final ChatClient chatClient; |
| 36 | + private final ObjectMapper objectMapper; |
| 37 | + |
| 38 | + @Override |
| 39 | + public List<AiValleyRecommendationResponse> recommendValleys(List<ValleyAiPromt> valleys, |
| 40 | + List<KeywordAiPromt> keywords) { |
| 41 | + try { |
| 42 | + String systemContent = getContent(systemMessageResource); |
| 43 | + |
| 44 | + String valleysJson = objectMapper.writeValueAsString(valleys); |
| 45 | + String keywordsJson = objectMapper.writeValueAsString(keywords); |
| 46 | + |
| 47 | + PromptTemplate userPromptTemplate = new PromptTemplate(userMessageResource); |
| 48 | + String userContent = userPromptTemplate.render( |
| 49 | + Map.of("valleysJson", valleysJson, "keywordsJson", keywordsJson) |
| 50 | + ); |
| 51 | + |
| 52 | + return chatClient.prompt() |
| 53 | + .system(systemContent) |
| 54 | + .user(userContent) |
| 55 | + .call() |
| 56 | + .entity(new ParameterizedTypeReference<List<AiValleyRecommendationResponse>>() { |
| 57 | + }); |
| 58 | + |
| 59 | + } catch (JsonProcessingException e) { |
| 60 | + log.error("Failed to serialize request data to JSON", e); |
| 61 | + return Collections.emptyList(); |
| 62 | + } catch (IOException e) { |
| 63 | + log.error("Failed to read prompt resource file", e); |
| 64 | + throw new RuntimeException("Failed to read prompt files", e); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private String getContent(Resource resource) throws IOException { |
| 69 | + return StreamUtils.copyToString(resource.getInputStream(), StandardCharsets.UTF_8); |
| 70 | + } |
| 71 | +} |
0 commit comments