https://spring.io/projects/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. Fea
spring.io
Spring AI란 Spring에서 만든, 여러가지 Generative AI(GPT, gemini 등등..)을 Spring Framework에 쉽게 사용할 수 있도록 추상화시켜준 라이브러리입니다.
Spring boot에서 지원하는 Google Gemini API 라이브러리를 이용해서,
이미지 input을 주고, 그것을 설명하라는 간단한 AI Controller를 작성해 보도록 하겠습니다.
prerequisite
implementation platform("org.springframework.ai:spring-ai-bom:0.8.1-SNAPSHOT")
implementation 'org.springframework.ai:spring-ai-vertex-ai-gemini-spring-boot-starter'
빌드 관리 도구 (위 코드는 gradle 기준)를 이용하여 위 두가지 코드를 추가합니다.
또한, OpenAI API 처럼 API Key를 직접적으로 코드에 사용하는것이아닌, ADC(Application Default Credentials) 라는 것을 사용해, 구글에 한번 로그인을 진행하면, 해당 머신에서는 API Key를 이용하지않고도 인증이 가능합니다.
(API Key를 직접적으로 사용하는 방식도 존재합니다)
ADC(Application Default Credentials) 란 ?
구글 클라우드에서 사용하는 인증 방식으로,
필요한 인증정보를 자동으로 검색해 가져오는 편리한 방식입니다.
ADC를 설치후, (https://cloud.google.com/sdk/docs/install?hl=ko)
다음과같은 명령어를 입력하면, 구글 로그인 화면이 띄워지게되고, 구글에 로그인을 진행합니다.
(참고 : https://cloud.google.com/docs/authentication/provide-credentials-adc?hl=ko#how-to)
gcloud auth application-default login
로그인이 성공하면, 나중에 필요한 인증정보를 자동으로 로컬 환경에서 찾게 됩니다.
구글 클라우드에 접속하신 후 , 새 프로젝트를 열어 내가 사용할 프로젝트를 만듭니다.
생성하게되면, 오른쪽 Id 필드가 나중에 코드에 ADC가 인식하게된 프로젝트 이름입니다.
그후, Spring boot 프로젝트로 가셔서, application.properties에 다음과같은 설정을 추가합니다.
spring.ai.vertex.ai.gemini.project-id=구글 클라우드의 프로젝트 이름
spring.ai.vertex.ai.gemini.location=asia-northeast3
spring.ai.vertex.ai.gemini.chat.options.model=vertex-pro-vision
spring.ai.vertex.ai.gemini.chat.options.temperature=0.5
project는 아까 생성헀던 프로젝트의 ID를 기입하시면 됩니다.
최종적으로, 내 프로젝트에서 Vertex AI API를 활성화 시켜 주시면 모든 준비가 끝납니다.
이제부터는 Spring boot의 코드입니다.
API를 시험만 해볼것이기떄문에 Controller하나로 구성되어 있습니다.
간단하게 post로 png파일을 건네받고, 그 response를 출력하는 예제입니다.
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.ChatResponse;
import org.springframework.ai.chat.messages.Media;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatClient;
import org.springframework.ai.vertexai.gemini.VertexAiGeminiChatOptions;
import org.springframework.stereotype.Service;
import org.springframework.util.MimeTypeUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
@RestController
@RequiredArgsConstructor
public class GeminiService {
private final VertexAiGeminiChatClient geminiClient;
public String getDescription(@RequestParam("png_file") MultipartFile png_file) throws IOException {
byte[] imgBytes = png_file.getBytes();
UserMessage multiModalUserMessage = new UserMessage("please explain given image.",
List.of(new Media(MimeTypeUtils.IMAGE_PNG, imgBytes)));
ChatResponse multiModalResponse = geminiClient.call(new Prompt(List.of(multiModalUserMessage),
VertexAiGeminiChatOptions.builder().withModel("gemini-pro-vision").build()));
return multiModalResponse.getResult().getOutput().getContent();
}
}
'Backend > Spring Boot' 카테고리의 다른 글
[Spring boot] Spring AOP - CGLIB Proxy vs Dynamic Proxy (0) | 2024.12.31 |
---|---|
[SpringBoot] QueryDsl의 Pagenation + PageableExecutionUtils을 이용해 count 쿼리 최적화 (1) | 2024.11.19 |
[Spring boot] JPA에서 Slice와 Page의 차이 (1) | 2024.10.07 |
[Spring Boot] Pageable을 이용해 페이지네이션구현(+JPA) (0) | 2024.09.23 |