Created
September 25, 2025 13:33
-
-
Save hikaMaeng/36404c0d31775c05785c1a6dc0e0f493 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.demo.service; | |
| import java.util.HashMap; | |
| import java.util.Map; | |
| import org.springframework.ai.chat.client.ChatClient; | |
| import org.springframework.ai.chat.prompt.Prompt; | |
| import org.springframework.ai.chat.prompt.PromptTemplate; | |
| import org.springframework.ai.converter.BeanOutputConverter; | |
| import org.springframework.ai.openai.OpenAiChatOptions; | |
| import org.springframework.ai.openai.api.ResponseFormat; | |
| import org.springframework.stereotype.Service; | |
| import com.example.demo.dto.Hotel; | |
| import lombok.extern.slf4j.Slf4j; | |
| @Service | |
| @Slf4j | |
| public class AiServiceBeanOutputConverter { | |
| // ##### 필드 ##### | |
| private ChatClient chatClient; | |
| // ##### 생성자 ##### | |
| public AiServiceBeanOutputConverter(ChatClient.Builder chatClientBuilder) { | |
| chatClient = chatClientBuilder.build(); | |
| } | |
| // ##### 메소드 ##### | |
| public Hotel beanOutputConverterLowLevel(String city) { | |
| // 구조화된 출력 변환기 생성 | |
| BeanOutputConverter<Hotel> beanOutputConverter = new BeanOutputConverter<>(Hotel.class); | |
| // 프롬프트 템플릿 생성 | |
| PromptTemplate promptTemplate = PromptTemplate.builder() | |
| .template("{city}에서 유명한 호텔 목록 5개를 출력하세요. {format}") | |
| .build(); | |
| // 프롬프트 생성 | |
| Prompt prompt = promptTemplate.create(Map.of( | |
| "city", city, | |
| "format", beanOutputConverter.getFormat())); | |
| // LLM의 JSON 출력 얻기 | |
| String json = chatClient.prompt(prompt) | |
| .call() | |
| .content(); | |
| // JSON을 Hotel로 매핑해서 변환 | |
| Hotel hotel = beanOutputConverter.convert(json); | |
| return hotel; | |
| } | |
| public Hotel LowLevel(){ | |
| ResponseFormat.JsonSchema jsonSchema = ResponseFormat.JsonSchema.builder() | |
| .name("hotel") | |
| .schema(""" | |
| { | |
| "name":"hotel", | |
| "strict":"true", | |
| "schema":{ | |
| "type":"object", | |
| "properties":{ | |
| "city":{"type":"string"}, | |
| "names":{"type":"array", "items":{"type":"string"}} | |
| } | |
| }, | |
| "required":["city", "names"] | |
| } | |
| """) | |
| .strict(true) | |
| .build(); | |
| ResponseFormat responseFormat = ResponseFormat.builder() | |
| .type(ResponseFormat.Type.JSON_SCHEMA) | |
| .jsonSchema(jsonSchema) | |
| .build(); | |
| OpenAiChatOptions options = OpenAiChatOptions.builder() | |
| .model("gpt-4o-mini") | |
| .responseFormat(responseFormat) | |
| .build(); | |
| BeanOutputConverter<Hotel> beanOutputConverter = new BeanOutputConverter<>(Hotel.class); | |
| Prompt prompt = new Prompt("서울에서 유명한 호텔 목록 5개를 출력하세요.", options); | |
| String json = chatClient.prompt(prompt) | |
| .call() | |
| .content(); | |
| Hotel hotel = beanOutputConverter.convert(json); | |
| return hotel; | |
| } | |
| public Hotel beanOutputConverterHighLevel(String city) { | |
| Hotel hotel = chatClient.prompt() | |
| .user("%s에서 유명한 호텔 목록 5개를 출력하세요.".formatted(city)) | |
| .call() | |
| .entity(Hotel.class); | |
| return hotel; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment