๐ŸŽ“
MASTERCLASS๋‚œ์ด๋„: ์ดˆ๊ธ‰ โž” ์‹ค์ „โฑ๏ธ ์•ฝ 3์‹œ๊ฐ„๐Ÿš€ 5๋‹จ๊ณ„ ์ปค๋ฆฌํ˜๋Ÿผ

Spring AI 2.0+ ์‹ค๋ฌด ๋งˆ์Šคํ„ฐ ํŠœํ† ๋ฆฌ์–ผ

Java & Spring Boot ๊ฐœ๋ฐœ์ž๋ฅผ ์œ„ํ•œ Spring AI 2.0.2(GA) ์ž…๋ฌธ๋ถ€ํ„ฐ ์‹ค์ „๊นŒ์ง€: ChatClient Fluent API, Java Record ์ •ํ˜• ๋ฐ์ดํ„ฐ ์ถ”์ถœ(entity), ChatMemory ๋ฉ€ํ‹ฐํ„ด ๋Œ€ํ™”, PgVector ๊ธฐ๋ฐ˜ RAG(QuestionAnswerAdvisor), @Tool ํ•จ์ˆ˜ ํ˜ธ์ถœ, SSE ์ŠคํŠธ๋ฆฌ๋ฐ ์„œ๋น™ ๋ฐ Ollama ๋กœ์ปฌ ๋ฌด๋ฃŒ ์‹ค์Šต ์™„๋ฒฝ ๊ฐ€์ด๋“œ

--views
#spring-ai#spring-boot#java#llm#rag#pgvector#tool-calling#ollama#openai#gemini#tutorial์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-11-13

๐Ÿ“‹ ๋น ๋ฅธ ๋ฌธ๋ฒ• ๋ฐ API ํ•ต์‹ฌ ์š”์•ฝ(์น˜ํŠธ์‹œํŠธ)์ด ํ•„์š”ํ•˜์‹ ๊ฐ€์š”?
์‹ค๋ฌด API ์กฐํšŒ๋ฅผ ์œ„ํ•œ Spring AI ํ€ต ๋ ˆํผ๋Ÿฐ์Šค ์น˜ํŠธ์‹œํŠธ ๋ฐ”๋กœ๊ฐ€๊ธฐ โž”

0. ๐ŸŒŸ Spring AI๋ž€ ๋ฌด์—‡์ด๊ณ  ์™œ ํ•„์š”ํ•œ๊ฐ€์š”?

๊ธฐ์กด Spring Boot ๋ฐฑ์—”๋“œ์— ChatGPT๋‚˜ Gemini ๊ฐ™์€ LLM์„ ๋ถ™์ด๋ ค๋ฉด, ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ RestClient/WebClient๋กœ ๋ณต์žกํ•œ HTTP JSON์„ ์กฐ๋ฆฝํ•˜๊ณ , ๋ชจ๋ธ(OpenAI, Anthropic, Google, Ollama)์ด ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค API ๊ทœ๊ฒฉ์„ ์ „๋ถ€ ๋œฏ์–ด๊ณ ์ณ์•ผ ํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’ก Spring AI์˜ ์—ญํ• :
Spring์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ ‘๊ทผ์„ JdbcTemplate/JPA๋กœ ์ถ”์ƒํ™”ํ–ˆ๋“ฏ์ด, Spring AI๋Š” ๋ชจ๋“  AI ๋ชจ๋ธ๊ณผ ๋ฒกํ„ฐ DB๋ฅผ ๋‹จ์ผ ChatClient ๋ฐ VectorStore ์ธํ„ฐํŽ˜์ด์Šค๋กœ ํ†ตํ•ฉํ•˜๋Š” ๊ณต์‹ Spring ์ƒํƒœ๊ณ„ ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค. ์„ค์ •(application.yml) ํ•œ ์ค„๋งŒ ๋ฐ”๊พธ๋ฉด ํด๋ผ์šฐ๋“œ GPT/Gemini์—์„œ ๋กœ์ปฌ Ollama๋กœ ์ฝ”๋“œ๋ฅผ ํ•œ ์ค„๋„ ๊ณ ์น˜์ง€ ์•Š๊ณ  ์ „ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!


๐ŸŽจ ํ•ต์‹ฌ ์šฉ์–ด 1๋ถ„ ๋งŒ์— ์ดํ•ดํ•˜๋Š” ์ผ์ƒ ๋น„์œ  ์‚ฌ์ „

Spring AI ํ•ต์‹ฌ ๊ฐœ๋…ํ•œ ์ค„ ์ผ์ƒ ๋น„์œ ์‰ฌ์šด ์„ค๋ช…
ChatClient๋งŒ๋Šฅ AI ๋ฆฌ๋ชจ์ปจ (Fluent API)WebClient์ฒ˜๋Ÿผ .prompt().user(...).call().content() ์ฒด์ด๋‹์œผ๋กœ ๋ชจ๋“  LLM์„ ์ผ๊ด€๋˜๊ฒŒ ํ˜ธ์ถœ
AdvisorSpring Interceptor / AOP ํ•„ํ„ฐAI ์š”์ฒญ ์ „ํ›„์— ๋Œ€ํ™” ๊ธฐ์–ต(ChatMemory), ์‚ฌ๋‚ด ๋ฌธ์„œ ๊ฒ€์ƒ‰(RAG), ๋กœ๊น…์„ ์ž๋™์œผ๋กœ ๋ผ์›Œ ๋„ฃ๋Š” ๋ฏธ๋“ค์›จ์–ด
entity(MyRecord.class)Jackson ์ž๋™ JSON ๋ฒˆ์—ญ๊ธฐAI์˜ ์ค„๊ธ€ ๋‹ต๋ณ€์„ Java record DTO ๊ฐ์ฒด๋กœ ์ž๋™ ํŒŒ์‹ฑยท๋งคํ•‘ํ•ด ์ฃผ๋Š” Structured Output ๊ธฐ๋Šฅ
VectorStore & RAG์˜คํ”ˆ๋ถ(Open Book) ์‹œํ—˜์‚ฌ๋‚ด PDF/๋ฌธ์„œ๋ฅผ ์ž„๋ฒ ๋”ฉํ•ด PgVector ๋“ฑ์— ์ €์žฅํ•ด ๋‘๊ณ , ์งˆ๋ฌธ ์‹œ ๊ด€๋ จ ๊ตฌ์ ˆ๋งŒ AI ํ”„๋กฌํ”„ํŠธ์— ์ž๋™ ์ฒจ๋ถ€
@Tool (Tool Calling)AI์—๊ฒŒ ์ฅ์–ด์ค€ Spring Bean ๋ฒ„ํŠผAI๊ฐ€ ๊ณ„์‚ฐ์ด๋‚˜ DB ์กฐํšŒ๊ฐ€ ํ•„์š”ํ•  ๋•Œ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋งŒ๋“  @Tool Java ๋ฉ”์„œ๋“œ๋ฅผ ์Šค์Šค๋กœ ํ˜ธ์ถœํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์•„๊ฐ
ChatMemoryAI์˜ ๋‹จ๊ธฐยท์žฅ๊ธฐ ๋ฉ”๋ชจ์žฅHTTP๋Š” ๋ฌด์ƒํƒœ(Stateless)์ง€๋งŒ conversationId๋ณ„๋กœ ์ด์ „ ๋Œ€ํ™” ๋‚ด์—ญ์„ ์ž๋™ ๊ธฐ์–ตยท์ฃผ์ž…

1. ํ•„์ˆ˜ ์‚ฌ์ „ ์š”๊ฑด & ์‹œ์Šคํ…œ ์š”๊ตฌ์‚ฌ์–‘ (Prerequisites)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (ํด๋ผ์šฐ๋“œ API ์œ„์ฃผ)๊ถŒ์žฅ ์‚ฌ์–‘ (๋กœ์ปฌ Ollama & PgVector ๊ตฌ๋™)๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Windows 10/11, macOS 13+, LinuxWindows 11, macOS (Apple Silicon), Ubuntu 22.04+Docker Desktop ์„ค์น˜ ๊ถŒ์žฅ
Java / SpringJava 21 LTS / Spring Boot 3.4+Java 21~25 / Spring Boot 3.5+ ๋˜๋Š” 4.0+Spring AI 2.0.2 (GA) ๊ธฐ์ค€ Java 17 ์ด์ƒ ํ•„์ˆ˜
๋ฉ”๋ชจ๋ฆฌ (RAM)4 GB ์ด์ƒ16 GB ~ 32 GB๋กœ์ปฌ Ollama(8B ๋ชจ๋ธ) + Docker PgVector ๋™์‹œ ๊ตฌ๋™ ์‹œ 16GB+ ๊ถŒ์žฅ
๋””์Šคํฌ (Storage)2 GB ์ด์ƒ20 GB ์ด์ƒ์˜ ๊ณ ์† SSDGradle ์บ์‹œ ๋ฐ ๋กœ์ปฌ ๋ชจ๋ธ ๊ฐ€์ค‘์น˜ ์ €์žฅ
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„Gradle 8.10+ ๋˜๋Š” Maven 3.9+OpenAI/Gemini API ํ‚ค ๋˜๋Š” ๋ฌด๋ฃŒ ๋กœ์ปฌ Ollama์•„๋ž˜ Ollama ๊ฐ€์ด๋“œ๋กœ API ๋น„์šฉ 0์› ์‹ค์Šต ๊ฐ€๋Šฅ!

๐Ÿ“ฆ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ (build.gradle & application.yml)

start.spring.io์—์„œ Web, Spring AI (OpenAI / Ollama) ์˜์กด์„ฑ์„ ์„ ํƒํ•˜๊ฑฐ๋‚˜, ๊ธฐ์กด ํ”„๋กœ์ ํŠธ build.gradle์— Spring AI 2.0.2 BOM์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

// build.gradle (Groovy DSL)
plugins {
    id 'java'
    id 'org.springframework.boot' version '3.5.0'
    id 'io.spring.dependency-management' version '1.1.7'
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

ext {
    set('springAiVersion', "2.0.2") // Spring AI 2.0.x ์ตœ์‹  ์•ˆ์ •ํ™”(GA) ๋ฒ„์ „
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.ai:spring-ai-bom:${springAiVersion}"
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    // OpenAI ํ˜ธํ™˜ API (OpenAI, Gemini OpenAI-compat, vLLM ๋“ฑ)
    implementation 'org.springframework.ai:spring-ai-starter-model-openai'
    // (์„ ํƒ) ๋ฌด๋ฃŒ ๋กœ์ปฌ AI ์‹ค์Šต์šฉ Ollama ์Šคํƒ€ํ„ฐ
    implementation 'org.springframework.ai:spring-ai-starter-model-ollama'
}

๐Ÿ”‘ ๋ฌด๋ฃŒ ๋กœ์ปฌ AI(Ollama) ๋˜๋Š” ํด๋ผ์šฐ๋“œ API ํ‚ค ์„ค์ • (application.yml)

์œ ๋ฃŒ API ํ‚ค๊ฐ€ ์—†๋‹ค๋ฉด ํ„ฐ๋ฏธ๋„์—์„œ Ollama๋ฅผ ์ผœ๊ณ  ollama pull llama3.2๋งŒ ์‹คํ–‰ํ•˜๋ฉด ๋น„์šฉ 0์›์œผ๋กœ ๋ชจ๋“  ์˜ˆ์ œ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# src/main/resources/application.yml
spring:
  application:
    name: spring-ai-masterclass
  ai:
    # ์˜ต์…˜ A: OpenAI ์‚ฌ์šฉ ์‹œ (ํ™˜๊ฒฝ๋ณ€์ˆ˜ OPENAI_API_KEY ์ฃผ์ž… ๊ถŒ์žฅ)
    openai:
      api-key: ${OPENAI_API_KEY:demo-key}
      chat:
        options:
          model: gpt-4o-mini
          temperature: 0.3

    # ์˜ต์…˜ B: ๋ฌด๋ฃŒ ๋กœ์ปฌ Ollama ์‚ฌ์šฉ ์‹œ
    ollama:
      base-url: http://localhost:11434
      chat:
        options:
          model: llama3.2

2. ์ „์ฒด ์•„ํ‚คํ…์ฒ˜ & 5๋‹จ๊ณ„ ํ•™์Šต ๋กœ๋“œ๋งต

flowchart TD
    p1["[1๊ฐ•] ChatClient & Structured Output<br/>Fluent API๋กœ LLM ํ˜ธ์ถœํ•˜๊ณ  Java Record๋กœ ํŒŒ์‹ฑ"]
    p2["[2๊ฐ•] ChatMemory & Advisor<br/>conversationId ๊ธฐ๋ฐ˜ ๋ฉ€ํ‹ฐํ„ด ๋Œ€ํ™” ๊ธฐ์–ต ๊ตฌํ˜„"]
    p3["[3๊ฐ•] VectorStore & RAG<br/>์‚ฌ๋‚ด ๋ฌธ์„œ๋ฅผ ์ž„๋ฒ ๋”ฉํ•ด QuestionAnswerAdvisor๋กœ ์˜คํ”ˆ๋ถ ๋‹ต๋ณ€"]
    p4["[4๊ฐ•] @Tool ํ•จ์ˆ˜ ํ˜ธ์ถœ (Tool Calling)<br/>AI๊ฐ€ Spring Service ๋ฉ”์„œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœํ•ด DB/API ์กฐํšŒ"]
    p5["[5๊ฐ•] SSE ์ŠคํŠธ๋ฆฌ๋ฐ & ํ”„๋กœ๋•์…˜ ์„œ๋น™<br/>Flux<String> ์‹ค์‹œ๊ฐ„ ํƒ€์ดํ•‘ ์‘๋‹ต๊ณผ Micrometer ๊ด€์ธก์„ฑ"]

    p1 --> p2 --> p3 --> p4 --> p5

3. [1๊ฐ•] ChatClient ๊ธฐ์ดˆ & Java Record ์ •ํ˜• ๋ฐ์ดํ„ฐ ์ถ”์ถœ

[์‹ค์Šต 1-1] ์ฒซ ๋ฒˆ์งธ AI ์ปจํŠธ๋กค๋Ÿฌ & entity() ๊ตฌ์กฐํ™” ์ถœ๋ ฅ

Spring AI 2.0์—์„œ๋Š” ChatClient.Builder๋ฅผ ์ฃผ์ž…๋ฐ›์•„ ๋นˆ(Bean)์ด๋‚˜ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์ง๊ด€์ ์ธ Fluent API๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

package com.luckytech.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/api/ai")
public class BasicAiController {

    private final ChatClient chatClient;

    // Spring Boot๊ฐ€ ์ž๋™ ๊ตฌ์„ฑํ•œ ChatClient.Builder ์ฃผ์ž…
    public BasicAiController(ChatClient.Builder builder) {
        this.chatClient = builder
                .defaultSystem("๋‹น์‹ ์€ ์นœ์ ˆํ•˜๊ณ  ์ •ํ™•ํ•œ ๋ฐฑ์—”๋“œ ์•„ํ‚คํ…ํŠธ ์–ด์‹œ์Šคํ„ดํŠธ์ž…๋‹ˆ๋‹ค.")
                .build();
    }

    // 1. ๊ธฐ๋ณธ ํ…์ŠคํŠธ ์‘๋‹ต
    @GetMapping("/ask")
    public String ask(@RequestParam(defaultValue = "Spring AI์˜ ์žฅ์ ์„ 2์ค„๋กœ ์š”์•ฝํ•ด์ค˜") String q) {
        return chatClient.prompt()
                .user(q)
                .call()
                .content();
    }

    // 2. Java Record๋กœ JSON ๊ตฌ์กฐํ™” ์ถœ๋ ฅ ์ž๋™ ๋งคํ•‘! (Structured Output)
    public record BookRecommendation(String title, String author, List<String> keyTopics, String reason) {}

    @GetMapping("/recommend")
    public BookRecommendation recommend(@RequestParam(defaultValue = "MSA ์•„ํ‚คํ…์ฒ˜") String topic) {
        return chatClient.prompt()
                .user(u -> u.text("{topic} ๋ถ„์•ผ์˜ ํ•„๋…์„œ 1๊ถŒ์„ ์ถ”์ฒœํ•ด์ค˜.").param("topic", topic))
                .call()
                .entity(BookRecommendation.class); // ๋‚ด๋ถ€์ ์œผ๋กœ JSON Schema๋ฅผ ์ž๋™ ์ƒ์„ฑํ•ด AI์—๊ฒŒ ์ „๋‹ฌ & ํŒŒ์‹ฑ!
    }
}
# ์‹คํ–‰ ํ›„ ํ…Œ์ŠคํŠธ
curl "http://localhost:8080/api/ai/recommend?topic=Kafka"
# ์˜ˆ์ƒ ์ถœ๋ ฅ (๊น”๋”ํ•œ JSON!):
# {
#   "title": "Kafka: The Definitive Guide",
#   "author": "Gwen Shapira ์™ธ",
#   "keyTopics": ["ํŒŒํ‹ฐ์…˜ ์•„ํ‚คํ…์ฒ˜", "์ปจ์Šˆ๋จธ ๊ทธ๋ฃน", "์ •ํ™•ํžˆ ํ•œ ๋ฒˆ(EOS)"],
#   "reason": "์นดํ”„์นด ๋‚ด๋ถ€ ๋™์ž‘ ์›๋ฆฌ์™€ ์‹ค๋ฌด ์šด์˜ ํŒจํ„ด์„ ๊ฐ€์žฅ ์ฒด๊ณ„์ ์œผ๋กœ ๋‹ค๋ฃฌ ํ‘œ์ค€์„œ์ž…๋‹ˆ๋‹ค."
# }

4. [2๊ฐ•] ๋ฉ€ํ‹ฐํ„ด ๋Œ€ํ™” ๊ธฐ์–ต (ChatMemory + Advisor)

LLM์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด์ „ ์งˆ๋ฌธ์„ ๊ธฐ์–ตํ•˜์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค. Spring AI์˜ **MessageChatMemoryAdvisor**๋ฅผ ์—ฐ๊ฒฐํ•˜๋ฉด ์‚ฌ์šฉ์ž๋ณ„(conversationId) ๋Œ€ํ™” ํžˆ์Šคํ† ๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.

package com.luckytech.ai.service;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.stereotype.Service;

@Service
public class StatefulChatService {

    private final ChatClient chatClient;

    public StatefulChatService(ChatClient.Builder builder) {
        // ์ตœ๊ทผ 20๊ฐœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฉ”๋ชจ๋ฆฌ์— ๋ณด๊ด€ํ•˜๋Š” ์œˆ๋„์šฐ ๋ฉ”๋ชจ๋ฆฌ
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .maxMessages(20)
                .build();

        this.chatClient = builder
                .defaultAdvisors(MessageChatMemoryAdvisor.builder(chatMemory).build())
                .build();
    }

    public String chat(String sessionId, String userMessage) {
        return chatClient.prompt()
                .user(userMessage)
                // ์„ธ์…˜ ID๋ฅผ Advisor ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋„˜๊ธฐ๋ฉด ํ•ด๋‹น ์‚ฌ์šฉ์ž์˜ ๋Œ€ํ™”๋งŒ ๋ถ„๋ฆฌ ๊ธฐ์–ต!
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, sessionId))
                .call()
                .content();
    }
}

5. [3๊ฐ•] ์‚ฌ๋‚ด ๋ฌธ์„œ ๊ธฐ๋ฐ˜ RAG (VectorStore & QuestionAnswerAdvisor)

์‚ฌ๋‚ด ๊ทœ์ •์ด๋‚˜ ์ตœ์‹  ๋งค๋‰ด์–ผ์„ VectorStore์— ์ ์žฌํ•˜๊ณ , **QuestionAnswerAdvisor**๋ฅผ ๋ถ™์ด๋ฉด ๋‹จ 3์ค„์˜ ์ฝ”๋“œ๋กœ ์˜คํ”ˆ๋ถ RAG ์‹œ์Šคํ…œ์ด ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค.

package com.luckytech.ai.rag;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.vectorstore.QuestionAnswerAdvisor;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import java.util.List;

@Configuration
class RagConfig {
    // ์‹ค์Šต์šฉ ์ธ๋ฉ”๋ชจ๋ฆฌ VectorStore (ํ”„๋กœ๋•์…˜์—์„œ๋Š” spring-ai-starter-vector-store-pgvector ์‚ฌ์šฉ)
    @Bean
    VectorStore vectorStore(EmbeddingModel embeddingModel) {
        SimpleVectorStore store = SimpleVectorStore.builder(embeddingModel).build();
        // ์‚ฌ๋‚ด ๊ทœ์ • ๋ฌธ์„œ ์ ์žฌ (์‹ค๋ฌด์—์„œ๋Š” TikaDocumentReader + TokenTextSplitter ์‚ฌ์šฉ)
        store.add(List.of(
            new Document("์‚ฌ๋‚ด ์ถœ์žฅ ์ˆ™๋ฐ•๋น„ ์ƒํ•œ์„ ์€ 1๋ฐ•๋‹น ์„œ์šธ 18๋งŒ์›, ์ง€๋ฐฉ 13๋งŒ์›์ž…๋‹ˆ๋‹ค."),
            new Document("์žฌํƒ๊ทผ๋ฌด ์‹ ์ฒญ์€ ์ „๋‚  ์˜คํ›„ 5์‹œ๊นŒ์ง€ ์‚ฌ๋‚ด ํฌํ„ธ์—์„œ ํŒ€์žฅ ์Šน์ธ์„ ๋ฐ›์•„์•ผ ํ•ฉ๋‹ˆ๋‹ค.")
        ));
        return store;
    }
}

@Service
public class PolicyRagService {

    private final ChatClient ragClient;

    public PolicyRagService(ChatClient.Builder builder, VectorStore vectorStore) {
        this.ragClient = builder
                .defaultAdvisors(
                    QuestionAnswerAdvisor.builder(vectorStore)
                        .searchRequest(SearchRequest.builder().topK(3).similarityThreshold(0.7).build())
                        .build()
                )
                .build();
    }

    public String askCompanyPolicy(String question) {
        return ragClient.prompt()
                .user(question)
                .call()
                .content();
    }
}

6. [4๊ฐ•] AI์—๊ฒŒ ์†๋ฐœ ๋‹ฌ์•„์ฃผ๊ธฐ (@Tool Tool Calling)

Spring AI์—์„œ๋Š” ์ผ๋ฐ˜ Spring Bean ๋ฉ”์„œ๋“œ์— @Tool ์–ด๋…ธํ…Œ์ด์…˜๋งŒ ๋ถ™์ด๋ฉด AI๊ฐ€ ํ•„์š”ํ•  ๋•Œ ์Šค์Šค๋กœ ์ธ์ž๋ฅผ ์ฑ„์›Œ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

package com.luckytech.ai.tools;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

@Component
class OrderTools {
    @Tool(description = "์ฃผ๋ฌธ ๋ฒˆํ˜ธ(orderId)๋กœ ํ˜„์žฌ ๋ฐฐ์†ก ์ƒํƒœ์™€ ์˜ˆ์ƒ ๋„์ฐฉ์ผ์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
    public String getOrderStatus(
            @ToolParam(description = "์กฐํšŒํ•  ์ฃผ๋ฌธ ๋ฒˆํ˜ธ (์˜ˆ: ORD-2026-001)") String orderId) {
        // ์‹ค๋ฌด์—์„œ๋Š” ์—ฌ๊ธฐ์„œ OrderRepository.findById(orderId) ํ˜ธ์ถœ
        return switch (orderId) {
            case "ORD-2026-001" -> "๋ฐฐ์†ก ์ค‘ (๋‚ด์ผ ์˜คํ›„ ๋„์ฐฉ ์˜ˆ์ •)";
            case "ORD-2026-002" -> "๊ฒฐ์ œ ์™„๋ฃŒ (์ถœ๊ณ  ์ค€๋น„ ์ค‘)";
            default -> "ํ•ด๋‹น ์ฃผ๋ฌธ ๋ฒˆํ˜ธ๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.";
        };
    }
}

@Service
public class SupportAgentService {

    private final ChatClient agentClient;
    private final OrderTools orderTools;

    public SupportAgentService(ChatClient.Builder builder, OrderTools orderTools) {
        this.agentClient = builder.build();
        this.orderTools = orderTools;
    }

    public String handleInquiry(String customerMessage) {
        return agentClient.prompt()
                .user(customerMessage)
                .tools(orderTools) // AI ์†์— OrderTools ๋ฆฌ๋ชจ์ปจ ์ฅ์–ด์ฃผ๊ธฐ!
                .call()
                .content();
    }
}

๋™์ž‘ ์›๋ฆฌ: ๊ณ ๊ฐ์ด "๋‚ด ์ฃผ๋ฌธ ORD-2026-001 ์–ธ์ œ ์™€?"๋ผ๊ณ  ๋ฌผ์œผ๋ฉด, AI๊ฐ€ ๋จผ์ € OrderTools.getOrderStatus("ORD-2026-001") ์‹คํ–‰์„ ์š”์ฒญํ•˜๊ณ  Spring AI๊ฐ€ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•ด ์ค€ ๋’ค, ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์นœ์ ˆํ•œ ์ตœ์ข… ๋‹ต๋ณ€์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค!


7. [5๊ฐ•] ์‹ค์‹œ๊ฐ„ SSE ์ŠคํŠธ๋ฆฌ๋ฐ ์„œ๋น™ & ํ”„๋กœ๋•์…˜ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

โ‘  Flux<String> ๊ธฐ๋ฐ˜ ์‹ค์‹œ๊ฐ„ ํƒ€์ดํ•‘(Streaming) API

@GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<String> streamAnswer(@RequestParam String q) {
    return chatClient.prompt()
            .user(q)
            .stream()   // .call() ๋Œ€์‹  .stream() ์‚ฌ์šฉ!
            .content(); // ํ† ํฐ์ด ์ƒ์„ฑ๋˜๋Š” ์ฆ‰์‹œ ๋ธŒ๋ผ์šฐ์ €๋กœ SSE ํ‘ธ์‹œ
}

๐Ÿ›ก๏ธ ํ”„๋กœ๋•์…˜ ์‹ค์ „ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

  1. VectorStore๋Š” ๋ฐ˜๋“œ์‹œ ์˜๊ตฌ DB ์‚ฌ์šฉ:
    • SimpleVectorStore๋Š” ์žฌ์‹œ์ž‘ ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ํ”„๋กœ๋•์…˜์—์„œ๋Š” PostgreSQL + pgvector(spring-ai-starter-vector-store-pgvector) ๋˜๋Š” Qdrant/Milvus๋ฅผ ์‚ฌ์šฉํ•˜์„ธ์š”.
  2. ํ”„๋กฌํ”„ํŠธ ์ธ์ ์…˜ ๋ฐ ๋น„์šฉ ๋ฐฉ์–ด (SafeGuardAdvisor):
    • SafeGuardAdvisor๋ฅผ ๋“ฑ๋กํ•ด ๊ธˆ์ง€์–ด/๋ฏผ๊ฐ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์ „ ์ฐจ๋‹จํ•˜๊ณ , maxTokens์™€ temperature๋ฅผ ๋ช…์‹œํ•ด ๋ฌดํ•œ ํ† ํฐ ๊ณผ๊ธˆ์„ ๋ฐฉ์ง€ํ•˜์„ธ์š”.
  3. ๊ด€์ธก์„ฑ(Observability) ํ™œ์„ฑํ™”:
    • spring-boot-starter-actuator์™€ Micrometer๋ฅผ ํ•จ๊ป˜ ์“ฐ๋ฉด gen_ai.client.token.usage ๋“ฑ LLM ํ† ํฐ ์‚ฌ์šฉ๋Ÿ‰๊ณผ ์‘๋‹ต ์ง€์—ฐ์„ Grafana/Prometheus์—์„œ ์ž๋™์œผ๋กœ ์ถ”์ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  4. ์™ธ๋ถ€ ๋„๊ตฌ ์—ฐ๋™์€ MCP(Model Context Protocol) ๊ณ ๋ ค:
    • ์—ฌ๋Ÿฌ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋‚˜ GitHub/Slack/DB ๋„๊ตฌ๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๋ ค๋ฉด Spring AI์˜ MCP Server/Client Starter๋ฅผ ํ†ตํ•ด ํ‘œ์ค€ ํ”„๋กœํ† ์ฝœ๋กœ ๋„๊ตฌ๋ฅผ ๋…ธ์ถœํ•˜์„ธ์š”.
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500