실무에 바로 적용하는 Spring AI: Spring 서비스에 챗봇·RAG·MCP 도입하기
practice-building-chat-api-with-local-ollama
✅ 1. build.gradle 수정
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webmvc'
implementation 'org.springframework.ai:spring-ai-starter-model-ollama'
// implementation 'org.springframework.ai:spring-ai-starter-model-openai'
testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}
✅ 2. application.yml 수정
#[2] Ollama 설정
ollama:
init:
pull-model-strategy: when_missing
chat:
model: hf.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF
✅ 3. 모델 다운로드
ollama pull hf.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF
✅ 4. Controller 구현
- 우리가 AI와 쉽게 소통할 수 있도록 Spring AI에서 제공하는 객체인 ChatClient
ChatController.java
package com.jscode.chat.controller;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ChatController {
private final ChatClient chatClient;
public ChatController(ChatClient.Builder chatClientBuilder){
this.chatClient = chatClientBuilder.build();
}
@GetMapping("/ai")
public String generation(String userPrompt){
return this.chatClient.prompt()
.user(userPrompt)
.call()
.content(); // 받아온 응답 중 메타데이터는 버리고, 순수 content만 추출!
}
}