ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring AI 맛보기
    Spring Framework 2024. 8. 13. 23:31

    최근 Spring AI가 출시되었다는 소식을 듣고 바로 사용해보았다.

     

    다양한 AI 모델을 지원하는데 그중 가장 널리 알려진 ChatGPT를 사용해봤다.

    간단한 테스트만 진행하므로 Spring Web, Spring AI, Lombok만을 의존성으로 추가했다.

     

    build.gradle

    plugins {
        id 'java'
        id 'org.springframework.boot' version '3.3.2'
        id 'io.spring.dependency-management' version '1.1.6'
    }
    
    group = 'project'
    version = '0.0.1-SNAPSHOT'
    
    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(17)
        }
    }
    
    configurations {
        compileOnly {
            extendsFrom annotationProcessor
        }
    }
    
    repositories {
        mavenCentral()
        maven { url 'https://repo.spring.io/milestone' }
    }
    
    ext {
        set('springAiVersion', "1.0.0-M1")
    }
    
    dependencies {
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
        compileOnly 'org.projectlombok:lombok'
        developmentOnly 'org.springframework.boot:spring-boot-devtools'
        annotationProcessor 'org.projectlombok:lombok'
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
        testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
    }
    
    dependencyManagement {
        imports {
            mavenBom "org.springframework.ai:spring-ai-bom:${springAiVersion}"
        }
    }
    
    tasks.named('test') {
        useJUnitPlatform()
    }

     

     

    application.properties

    temperature 값 설정을 통해 생성되는 응답의 무작위성을 제어할 수 있다. 0.0에서 1.0 범위 내의 값으로 설정할 수 있으며 값이 낮아질수록 응답이 결정적이고 예측 가능해지며, 값이 높아질수록 응답이 다양해지고 창의적인 응답이 생성된다.

    spring.application.name=spring-ai
    
    spring.ai.openai.api-key={발급받은 API키}
    spring.ai.openai.chat.options.model=gpt-4o
    spring.ai.openai.chat.options.temperature=0.6

     

     

    ChatClientConfig.java

    @Configuration
    public class ChatClientConfig {
    
        @Bean
        ChatClient chatClient(ChatClient.Builder builder) {
            return builder.build();
        }
    }

     

     

    ChatGptController.java

    위에서 정의한 ChatClient를 빈으로 주입받고 메서드 체이닝을 통해 매우 간결하게 ChatGPT를 사용할 수 있다. system() 메서드를 통해 GPT 모델에게 대화의 상황을 알려주거나 어떤 식으로 대답해야 하는지에 대한 지시사항을 부여할 수 있다.

    @RestController
    @RequiredArgsConstructor
    public class ChatGptController {
    
        private final ChatClient chatClient;
    
        @GetMapping("/chat-gpt-v1")
        public String chatGptV1(@RequestParam String msg) {
            return chatClient
                    .prompt()
                    .user(msg)
                    .call()
                    .content();
        }
    
        @GetMapping("/chat-gpt-v2")
        public String chatGptV2(@RequestParam String msg) {
            return chatClient
                    .prompt()
                    .system("개발자가 되기 위해 공부하는 학생들이 한 질문이야. 이해하기 쉽게 잘 알려줘")
                    .user(msg)
                    .call()
                    .content();
        }
    }

     

     

    Postman 테스트

    응답이 잘 나오는 것을 확인할 수 있다.