[Spring/AI] Spring AI를 활용하여 가볍게 영작하기

2025. 3. 24. 21:18Backend/Spring

 

📝 AI는 python의 영역이라고 생각했지만, Spring AI를 통해 여러 AI Model를 붙여 사용할 수 있다고 해서 사용해봤습니다. 


Spring AI

먼저, 아래의 링크는 Spring AI를 소개하고 있는 공식 페이지 입니다. 

 

Spring AI

Spring AI is an application framework for AI engineering. Its goal is to apply to the AI domain Spring ecosystem design principles such as portability and modular design and promote using POJOs as the building blocks of an application to the AI domain. At

spring.io

 

 

해당 페이지에서 Spring AI 를 Spring AI란 AI 엔지니어링을 위한 애플리케이션 프레임워크 라고 소개하고 있는데요

 

Spring AI is an application framework for AI engineering. 

Its goal is to apply to the AI domain Spring ecosystem design principles such as portability 

and modular design and promote using POJOs as the building blocks of an application to the AI domain.

- https://docs.spring.io/spring-ai/reference/index.html

 

 

spring initializr

Spring AI를 사용하기 위하여 Spring initializr(https://start.spring.io/)에서 OpenAI를 추가해준 뒤, 새롭게 프로젝트를 생성하였습니다.

 

build.gradle

dependencies {
	implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'

 

이후 프로젝트를 실행하게 되면, build.gradle에 다음과 같은 라이브러리가 추가되어 있음을 확인할 수 있습니다.

 

application.yml

application.yml에 api-key를 추가해주시면 되는데요!

spring:
  ai:
    openai:
      api-key: (발급받은 api-key)

 

api-key는 https://platform.openai.com/api-keys 사이트에 접속하셔서, new secret key를 발급 받으시면 됩니다.

 

TestController

그리고 test를 위해 저는 Controller를 하나 만들었고, postman으로 test를 해보려고 합니다.

@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
public class AITestController {

    private final OpenAiChatModel openAiChatModel;

    @PostMapping("/translate")
    public Mono<String> getWeather(@RequestBody String message) {
        Prompt prompt = new Prompt(new UserMessage(message));
        return Mono.fromSupplier(() -> openAiChatModel.call(prompt))
                .map(response -> response.getResult().getOutput().getText());
    }

}

 

코드는 공식 문서의 sample 코드를 활용했는데, 요금이 나갈 수 있어 1회성 호출을 하는 Mono를 사용했습니다.

 

OpenAI Chat :: Spring AI Reference

Multimodality refers to a model’s ability to simultaneously understand and process information from various sources, including text, images, audio, and other data formats. OpenAI supports text, vision, and audio input modalities. Vision OpenAI models tha

docs.spring.io

 

그런데 Error

org.springframework.ai.retry.NonTransientAiException: 429 - {
    "error": {
        "message": "You exceeded your current quota, please check your plan and billing details. For more information on this error, read the docs: https://platform.openai.com/docs/guides/error-codes/api-errors.",
        "type": "insufficient_quota",
        "param": null,
        "code": "insufficient_quota"
    }
}

 

그런데 요금 사용량을 초과했다는 에러가 발생했습니다. 요금 안내 확인할 수 있는 사이트(https://platform.openai.com/usage)

에서, 제 요금을 확인해보니, 제가 사용할 수 있는 Free Trial도 없는 상황이었습니다. 그래서 Test를 위해 5달러 선결제를 하였습니다😭.

 

Test

이후 한국을 영어로 표현하는 것을 질문했는데, 응답이 온 것을 확인하였습니다.

 

추가로, 사용량이 어느 정도 되었는지 확인하였습니다.

 

 

References

1. Spring AI [official] 

2. [Spring AI] Spring AI를 적용해보자!