๐Ÿค–

Spring AI ์น˜ํŠธ์‹œํŠธ

Spring Boot 4.1.1 ์ตœ์‹  ํ™˜๊ฒฝ ๋ฐ Spring AI 2.0.1 GA ๊ธฐ์ค€: ChatClient 2.0 Fluent API, @Tool ๊ธฐ๋ฐ˜ ToolCallingAdvisor, Virtual Threads I/O ์ตœ์ ํ™”, Streaming SSE, Structured Output, RAG ํŒŒ์ดํ”„๋ผ์ธ ๋ฐ VectorStore ์‹ค์ „ ๊ฐ€์ด๋“œ

--views
๋‹จ์ˆ˜:
#spring-ai#spring-boot-4#spring-ai-2.0#chatclient#tool-calling#rag#vectorstore#virtual-threads#openai#claude#ollama#java#backend์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-10-06

1. Spring AI 2.0 ์•„ํ‚คํ…์ฒ˜ & ์ตœ์‹  ์˜์กด์„ฑ (Spring Boot 4.1.1 + Spring AI 2.0.1)

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

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Linux, macOS, Windows 10/11Ubuntu 22.04/24.04 LTS, macOS (Apple Silicon), Windows 11Java 21 / 25 LTS ์ง€์› OS
CPU2 ์ฝ”์–ด ์ด์ƒ4 ์ฝ”์–ด ~ 8 ์ฝ”์–ด ์ด์ƒ๋ฌธ์„œ ํ† ํฐํ™”, ์ฒญํ‚น, ์ž„๋ฒ ๋”ฉ ๋ฒกํ„ฐ ๋ณ€ํ™˜ ์—ฐ์‚ฐ
๋ฉ”๋ชจ๋ฆฌ (RAM)4 GB ์ด์ƒ8 GB ~ 16 GB ์ด์ƒ๋กœ์ปฌ ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ(ONNX) ๋˜๋Š” ๋ฒกํ„ฐDB ๊ตฌ๋™ ์‹œ 16GB+ ๊ถŒ์žฅ
๋””์Šคํฌ (Storage)5 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„15 GB ์ด์ƒ์˜ ๊ณ ์† SSDGradle ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ฐ ๋กœ์ปฌ ๋ฒกํ„ฐ ์ธ๋ฑ์Šค ์ €์žฅ์†Œ
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„JDK 21 ๋˜๋Š” 25 LTS ํ•„์ˆ˜ (๊ถŒ์žฅ: JDK 25)Spring Boot 4.1.1+ / Spring AI 2.0.1+์™ธ๋ถ€ AI ์ œ๊ณต์ž API ํ‚ค (OpenAI, Claude, Ollama ๋“ฑ)

์ตœ์‹  Spring Boot 4.1.1 ๋ฐ Spring AI 2.0.1 GA ํ™˜๊ฒฝ์€ Java 17~26, Jackson 3, HttpClient 5.6, ๊ฐ€์ƒ ์Šค๋ ˆ๋“œ(Virtual Threads)๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์—ฌ ๋Œ€๊ทœ๋ชจ LLM I/O ํ˜ธ์ถœ์„ ๊ณ ์„ฑ๋Šฅยท์ €๋น„์šฉ์œผ๋กœ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ AI ํ‘œ์ค€ ์กฐํ•ฉ์ž…๋‹ˆ๋‹ค.

[Spring Boot 4.1.1 + Spring AI 2.0 ์•„ํ‚คํ…์ฒ˜]
Application     : Spring Boot 4.1.1 (Virtual Threads ํ™œ์„ฑํ™”, Java 17/21/25/26 ์ง€์›)
Client Layer    : ChatClient (Fluent 2.0 API, Tools & Advisors ํ†ตํ•ฉ)
Advisor Layer   : ToolCallingAdvisor(๋„๊ตฌ ์‹คํ–‰ ๋ฃจํ”„ ์ค‘์•™ํ™”), ChatMemoryAdvisor, RAG Advisor
Model Layer     : ChatModel, EmbeddingModel, ImageModel, AudioModel
Data/RAG Layer  : VectorStore (PGvector, Chroma, Qdrant, Milvus), Document, TextSplitter
Provider Layer  : OpenAI (GPT-4o), Anthropic (Claude 3.5), Ollama (๋กœ์ปฌ LLM), DeepSeek, Gemini

๐Ÿ“Š ๋ฒ„์ „ ํ˜ธํ™˜์„ฑ ๋งคํŠธ๋ฆญ์Šค (Compatibility Matrix)

Spring Boot ๋ฒ„์ „๊ถŒ์žฅ Spring AI ๋ฒ„์ „Java ์š”๊ตฌ ์‚ฌํ•ญ๋น„๊ณ 
Spring Boot 4.1.1 (ํ˜„์žฌ ์ตœ์‹ )Spring AI 2.0.1 GAJava 17, 21, 25, 26๊ณต์‹ ๊ถŒ์žฅ ์ตœ์‹  ์กฐํ•ฉ, Maven Central ์ •์‹ ๋ฐฐํฌ, JSpecify ๋„ ์•ˆ์ „์„ฑ
Spring Boot 4.0.xSpring AI 2.0.0 GAJava 17, 21ToolCallingAdvisor ์ค‘์•™ํ™” ๋„์ž…
Spring Boot 3.3.x / 3.4.xSpring AI 1.0.0-M6Java 17+๊ตฌ๋ฒ„์ „ ๋งˆ์ผ์Šคํ†ค ๋ ˆํฌ์ง€ํ† ๋ฆฌ(repo.spring.io/milestone) ํ•„์š”

๐Ÿ˜ Gradle (build.gradle) ์„ค์ •

Spring Boot 4.1.1๊ณผ Spring AI 2.0.1์€ Maven Central์— ์ •์‹ ๋ฐฐํฌ๋˜๋ฏ€๋กœ ๋ณ„๋„์˜ ๋งˆ์ผ์Šคํ†ค ์ €์žฅ์†Œ ์ถ”๊ฐ€ ์—†์ด ์ฆ‰์‹œ ๋นŒ๋“œ๋ฉ๋‹ˆ๋‹ค.

plugins {
    id 'java'
    id 'org.springframework.boot' version '4.1.1' // ๐Ÿš€ ์ตœ์‹  Spring Boot 4.1.1
    id 'io.spring.dependency-management' version '1.1.7' // ๐Ÿš€ ์ตœ์‹  ์˜์กด์„ฑ ๊ด€๋ฆฌ ํ”Œ๋Ÿฌ๊ทธ์ธ
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25) // Java 25
    }
}

ext {
    // ๐Ÿš€ Spring AI 2.0.1 GA (Spring Boot 4.x ๊ณต์‹ ๋ฒ ์ด์Šค๋ผ์ธ)
    set('springAiVersion', "2.0.1")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    
    // 1. OpenAI ์Šคํƒ€ํ„ฐ (Chat, Embedding, Image, Whisper, Vision ํ†ตํ•ฉ)
    implementation "org.springframework.ai:spring-ai-openai-spring-boot-starter"
    
    // 2. ๋˜๋Š” ์™„์ „ ๋ฌด๋ฃŒ ์˜จํ”„๋ ˆ๋ฏธ์Šค ๋กœ์ปฌ LLM (Ollama)
    // implementation "org.springframework.ai:spring-ai-ollama-spring-boot-starter"
    
    // 3. ๋˜๋Š” Anthropic Claude ์Šคํƒ€ํ„ฐ
    // implementation "org.springframework.ai:spring-ai-anthropic-spring-boot-starter"

    // 4. PostgreSQL PGvector ๋ฒกํ„ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๋™
    // implementation "org.springframework.ai:spring-ai-pgvector-store-spring-boot-starter"
}

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

๐Ÿชถ Maven (pom.xml) ์„ค์ •

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>4.1.1</version>
    <relativePath/>
</parent>

<properties>
    <java.version>25</java.version>
    <spring-ai.version>2.0.1</spring-ai.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-bom</artifactId>
            <version>${spring-ai.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- Maven Central์—์„œ ์ง์ ‘ ๋‹ค์šด๋กœ๋“œ๋ฉ๋‹ˆ๋‹ค -->
    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
    </dependency>
</dependencies>

2. application.yml ํ”„๋กœ๋ฐ”์ด๋”๋ณ„ ์„ค์ • (OpenAI ยท Claude ยท Ollama ยท DeepSeek)

Spring Boot 4.1.1์˜ application.yml์— API ํ‚ค์™€ ๋ชจ๋ธ ์˜ต์…˜์„ ์ง€์ •ํ•˜๋ฉด ์ตœ์ ํ™”๋œ ChatModel๊ณผ EmbeddingModel์ด ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ์ž๋™ ๋“ฑ๋ก๋ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ๊ธด ๋„คํŠธ์›Œํฌ I/O๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” AI ์„œ๋น„์Šค ํŠน์„ฑ์ƒ ๊ฐ€์ƒ ์Šค๋ ˆ๋“œ(Virtual Threads) ํ™œ์„ฑํ™”๋ฅผ ์ ๊ทน ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

๐ŸŒ OpenAI ๋ฐ ํ˜ธํ™˜ API (vLLM, Groq, DeepSeek)

spring:
  # ๐Ÿš€ Spring Boot 4.1.1 ๊ถŒ์žฅ: AI I/O ๋Œ€๊ธฐ ์‹œ ํ†ฐ์บฃ ์Šค๋ ˆ๋“œ ๊ณ ๊ฐˆ ๋ฐฉ์ง€ (์ดˆ๊ณ ์„ฑ๋Šฅ ์ฒ˜๋ฆฌ)
  threads:
    virtual:
      enabled: true

  ai:
    openai:
      api-key: ${OPENAI_API_KEY}
      base-url: https://api.openai.com # vLLM์ด๋‚˜ Groq ๋“ฑ OpenAI ํ˜ธํ™˜ ์—”๋“œํฌ์ธํŠธ ์ง€์›
      chat:
        options:
          model: gpt-4o-mini # gpt-4o, o1-mini, gpt-4-turbo ๋“ฑ
          temperature: 0.7   # 0.0(์ •๋ฐ€/๋…ผ๋ฆฌ) ~ 1.0(์ฐฝ์˜์ )
          max-tokens: 4096
      embedding:
        options:
          model: text-embedding-3-small
    # 2.0.1 ๋„๊ตฌ ๋ณด์•ˆ ์ •์ฑ… (๋ช…์‹œ์  ๋“ฑ๋ก ๋„๊ตฌ๋งŒ ๊ฒฉ๋ฆฌ ์‹คํ–‰)
    tools:
      resolution:
        fallback:
          enabled: false # ๋ณด์•ˆ ๊ถŒ์žฅ: ๋ฏธ๋“ฑ๋ก ๋„๊ตฌ์˜ ๊ธ€๋กœ๋ฒŒ ๋””์ŠคํŒจ์น˜ ๋ฐฉ์ง€ (CVE-2026-59318 ๋ฐฉ์–ด)

๐Ÿฆ™ Ollama (๋กœ์ปฌ ์˜คํ”ˆ์†Œ์Šค LLM: Llama 3.1, DeepSeek-Coder, Qwen)

GPU/CPU ๋กœ์ปฌ ์„œ๋ฒ„์—์„œ API ๋น„์šฉ ์—†์ด ์™„์ „ ๋ฌด๋ฃŒ๋กœ ์‹คํ–‰:

spring:
  ai:
    ollama:
      base-url: http://localhost:11434
      chat:
        options:
          model: llama3.1:8b # deepseek-r1:8b, qwen2.5:7b ๋“ฑ
          temperature: 0.3
      embedding:
        options:
          model: nomic-embed-text

๐ŸŸฃ Anthropic Claude & Google Gemini

spring:
  ai:
    anthropic:
      api-key: ${ANTHROPIC_API_KEY}
      chat:
        options:
          model: claude-3-5-sonnet-20241022
          max-tokens: 8192
    vertex:
      ai:
        gemini:
          project-id: my-gcp-project
          location: us-central1
          chat:
            options:
              model: gemini-1.5-pro

3. ChatClient 2.0 Fluent API (ํ˜„๋Œ€์  ๋Œ€ํ™”ํ˜• ๋ชจ๋ธ ์ธํ„ฐํŽ˜์ด์Šค)

Spring AI 2.0์˜ ChatClient๋Š” ๋ชจ๋ธ ๋…๋ฆฝ์ (Model-agnostic)์ธ ๊ณ ์ˆ˜์ค€ Fluent API์ž…๋‹ˆ๋‹ค. ๋นŒ๋”๋ฅผ ํ†ตํ•ด ์ „์—ญ ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ, ๋„๊ตฌ, ์–ด๋“œ๋ฐ”์ด์ € ์ฒด์ธ์„ ๋ชจ๋“ˆ์‹์œผ๋กœ ์žฅ์ฐฉํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ› ๏ธ ChatClient ๋นˆ ๋“ฑ๋ก (@Configuration)

package com.lucky.ai.config;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AiConfig {

    @Bean
    public ChatClient chatClient(ChatClient.Builder builder) {
        return builder
                // ๋ชจ๋“  ๋Œ€ํ™”์— ๊ธฐ๋ณธ ์ ์šฉ๋  ์‹œ์Šคํ…œ ํ”„๋กฌํ”„ํŠธ
                .defaultSystem("๋‹น์‹ ์€ ์นœ์ ˆํ•˜๊ณ  ์ „๋ฌธ์ ์ธ IT ๊ธฐ์ˆ  ์ปจ์„คํ„ดํŠธ์ž…๋‹ˆ๋‹ค. ํ•ญ์ƒ ๋ช…ํ™•ํ•œ ํ•œ๊ตญ์–ด๋กœ ๋‹ต๋ณ€ํ•˜์„ธ์š”.")
                // ๋ชจ๋ธ ๊ธฐ๋ณธ ํŒŒ๋ผ๋ฏธํ„ฐ (์˜จ๋„, ํ† ํฐ ๋“ฑ)
                .defaultOptions(options -> options.temperature(0.3))
                .build();
    }
}

๐Ÿ’ป Controller์—์„œ ChatClient ํ˜ธ์ถœ ์‹ค์ „

@RestController
@RequestMapping("/api/chat")
@RequiredArgsConstructor
public class ChatController {

    private final ChatClient chatClient;

    // 1. ๋‹จ์ผ ํ”„๋กฌํ”„ํŠธ ๋ฌธ์ž์—ด ์‘๋‹ต
    @GetMapping("/simple")
    public String simpleChat(@RequestParam String message) {
        return chatClient.prompt()
                .user(message)
                .call()
                .content(); // String ํ…์ŠคํŠธ ๋ฐ˜ํ™˜
    }

    // 2. ๋™์  ํŒŒ๋ผ๋ฏธํ„ฐ ์น˜ํ™˜ & ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ(ํ† ํฐ, ๋น„์šฉ) ํš๋“
    @GetMapping("/expert")
    public ChatResponse expertChat(@RequestParam String topic, @RequestParam String lang) {
        return chatClient.prompt()
                .system(s -> s.text("์ „๋ฌธ ๋ถ„์•ผ: {field}").param("field", "์†Œํ”„ํŠธ์›จ์–ด ์•„ํ‚คํ…์ฒ˜"))
                .user(u -> u.text("{topic}์— ๋Œ€ํ•ด {lang} ์–ธ์–ด ๊ด€์ ์—์„œ ํ•ต์‹ฌ ์ฃผ์˜์‚ฌํ•ญ์„ 3์ค„ ์š”์•ฝํ•ด์ค˜.")
                            .param("topic", topic)
                            .param("lang", lang))
                .call()
                .chatResponse(); // ChatResponse: ํ† ํฐ ์‚ฌ์šฉ๋Ÿ‰, FinishReason ๋“ฑ ์ „์ฒด ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
    }
}

4. ์ŠคํŠธ๋ฆฌ๋ฐ(Streaming) & Server-Sent Events (SSE) ์‹ค์‹œ๊ฐ„ ์‘๋‹ต

๋ชจ๋ธ์˜ ๊ธด ์ƒ์„ฑ ์‹œ๊ฐ„์„ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š๊ณ  ์‹ค์‹œ๊ฐ„์œผ๋กœ ์ƒ์„ฑ๋˜๋Š” ํ† ํฐ์„ ํ™”๋ฉด์— ์ฆ‰์‹œ ์ŠคํŠธ๋ฆฌ๋ฐํ•ฉ๋‹ˆ๋‹ค. Spring AI 2.0์˜ ChatClient.stream()์€ WebFlux Flux<String>๊ณผ ์™„๋ฒฝํ•˜๊ฒŒ ํ˜ธํ™˜๋ฉ๋‹ˆ๋‹ค.

โšก Reactive ์ŠคํŠธ๋ฆฌ๋ฐ ์ปจํŠธ๋กค๋Ÿฌ (Flux<String>)

package com.lucky.ai.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

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

    private final ChatClient chatClient;

    // text/event-stream ์œผ๋กœ ์‹ค์‹œ๊ฐ„ ๊ธ€์ž ๋‹จ์œ„ ๋ฐฉ์ถœ
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> streamChat(@RequestParam String prompt) {
        return chatClient.prompt()
                .user(prompt)
                .stream()     // call() ๋Œ€์‹  stream() ํ˜ธ์ถœ!
                .content();   // Flux<String> ์‹ค์‹œ๊ฐ„ ์ŠคํŠธ๋ฆผ
    }
}

๐ŸŒ ํ”„๋ก ํŠธ์—”๋“œ ์‹ค์‹œ๊ฐ„ SSE ์ˆ˜์‹  ์ฝ”๋“œ

// ๋ธŒ๋ผ์šฐ์ €์—์„œ Server-Sent Events ์ˆ˜์‹ 
const eventSource = new EventSource('/api/ai/stream?prompt=' + encodeURIComponent('Spring AI 2.0 ์žฅ์ '));

eventSource.onmessage = (event) => {
  // ์‹ค์‹œ๊ฐ„ ํ† ํฐ ๋ˆ„์  ๋ Œ๋”๋ง
  document.getElementById('chat-box').textContent += event.data;
};

eventSource.onerror = (err) => {
  console.log('์‘๋‹ต ์ŠคํŠธ๋ฆผ ์ข…๋ฃŒ ๋˜๋Š” ๋„คํŠธ์›Œํฌ ํ•ด์ œ');
  eventSource.close();
};

5. ๊ตฌ์กฐํ™”๋œ ์ถœ๋ ฅ (Structured Output & Record ๋งคํ•‘)

LLM์˜ ์ž์œ ๋กœ์šด ์ž์—ฐ์–ด ์‘๋‹ต์„ Java 17+ Record๋‚˜ DTO ๊ฐ์ฒด๋กœ ์—„๊ฒฉํ•˜๊ณ  ์•ˆ์ „ํ•˜๊ฒŒ(Type-Safe) ์ž๋™ ํŒŒ์‹ฑํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“‹ ์‘๋‹ต ๋ฐ›์„ Java Record ์„ ์–ธ

public record TechArticle(
    String title,
    String author,
    List<String> tags,
    int difficultyLevel,
    String summary
) {}

public record CurationResponse(
    String category,
    List<TechArticle> articles
) {}

๐ŸŽฏ entity(...)๋ฅผ ํ™œ์šฉํ•œ ๊ฐ์ฒด ์ž๋™ ๋งคํ•‘

@Service
@RequiredArgsConstructor
public class CurationService {

    private final ChatClient chatClient;

    // ๋‹จ์ผ Record ์ž๋™ ์—ญ์ง๋ ฌํ™”
    public TechArticle getRecommendedArticle(String keyword) {
        return chatClient.prompt()
                .user(u -> u.text("{keyword} ๋ถ„์•ผ์˜ ๋ฒ ์ŠคํŠธ ์•„ํ‹ฐํด ์ •๋ณด๋ฅผ ์ถ”์ฒœํ•ด์ค˜.")
                            .param("keyword", keyword))
                .call()
                .entity(TechArticle.class); // ๋‚ด๋ถ€ JSON ์Šคํ‚ค๋งˆ ์ฃผ์ž… ๋ฐ ์ž๋™ ๋งคํ•‘!
    }

    // List ์ปฌ๋ ‰์…˜ ์ง์ ‘ ๋งคํ•‘ (ParameterizedTypeReference)
    public List<TechArticle> getArticleList(String keyword) {
        return chatClient.prompt()
                .user(keyword + " ๊ด€๋ จ ์ตœ์‹  ๊ธ€ 3ํŽธ์„ ์ถ”์ฒœํ•ด์ค˜.")
                .call()
                .entity(new ParameterizedTypeReference<List<TechArticle>>() {});
    }
}

6. ํ”„๋กฌํ”„ํŠธ ํ…œํ”Œ๋ฆฟ & ๋ฉ”์‹œ์ง€ ์—ญํ•  ๊ณ„์ธต (PromptTemplate & Roles)

๋ฐ˜๋ณต๋˜๋Š” ํ”„๋กฌํ”„ํŠธ ํŒจํ„ด์„ ํŒŒ์ผ(.st StringTemplate)๋กœ ๋ถ„๋ฆฌ ๊ด€๋ฆฌํ•˜๊ณ , ์—ญํ• (System, User, Assistant)๋ณ„ ์ปจํ…์ŠคํŠธ๋ฅผ ๊ตฌ์กฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ“ PromptTemplate๊ณผ ๋ณ€์ˆ˜ ์ฃผ์ž…

import org.springframework.ai.chat.prompt.PromptTemplate;

@GetMapping("/review-diff")
public String reviewCodeDiff(@RequestParam String diffText) {
    String templateString = """
        ๋‹น์‹ ์€ ์ฝ”๋“œ ํ’ˆ์งˆ ๊ฐ์‚ฌ ์ „๋ฌธ๊ฐ€์ž…๋‹ˆ๋‹ค. ์•„๋ž˜ Git Diff๋ฅผ ๊ฒ€ํ† ํ•˜๊ณ :
        1. ์ž ์žฌ์  ๋ฒ„๊ทธ
        2. ์„ฑ๋Šฅ ๋ณ‘๋ชฉ ์ง€์ 
        3. ๋ฆฌํŒฉํ† ๋ง ๊ถŒ์žฅ ์‚ฌํ•ญ์„ ํ•œ๊ตญ์–ด๋กœ ์ž‘์„ฑํ•˜์„ธ์š”.
        
        [Git Diff]:
        {diff}
        """;

    PromptTemplate template = new PromptTemplate(templateString);
    Prompt prompt = template.create(Map.of("diff", diffText));

    return chatClient.prompt(prompt).call().content();
}

๐Ÿ“‚ ์™ธ๋ถ€ ๋ฆฌ์†Œ์Šค ํŒŒ์ผ์—์„œ ํ”„๋กฌํ”„ํŠธ ๋กœ๋”ฉ

src/main/resources/prompts/sql-generator.st ํŒŒ์ผ ๊ด€๋ฆฌ:

@RestController
public class SqlPromptController {

    @Value("classpath:prompts/sql-generator.st")
    private Resource sqlPromptResource;

    private final ChatClient chatClient;

    @PostMapping("/generate-sql")
    public String generateSql(@RequestBody String userQuery) {
        return chatClient.prompt()
                .user(u -> u.text(sqlPromptResource)
                            .param("query", userQuery)
                            .param("dialect", "PostgreSQL 16"))
                .call()
                .content();
    }
}

7. Spring AI 2.0 ๋„๊ตฌ ํ˜ธ์ถœ (@Tool & ToolCallingAdvisor)

Spring AI 2.0์˜ ์ตœ๋Œ€ ๋ณ€๊ฒฝ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค! ์ด์ „์˜ ๋ณต์žกํ•œ ํ•จ์ˆ˜ ๋“ฑ๋ก ๋Œ€์‹ , @Tool ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ฉ”์„œ๋“œ์— ๋ถ€์—ฌํ•˜๊ณ  ChatClient.prompt().tools(...)๋กœ ๋„˜๊ธฐ๋ฉด ToolCallingAdvisor๊ฐ€ ๋ชจ๋ธ๊ณผ์˜ ๋„๊ตฌ ํ˜ธ์ถœ ๋ฃจํ”„๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ์ž๋™ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

[Spring AI 2.0 ๋„๊ตฌ ์‹คํ–‰ ๋ฃจํ”„]
1. ์‚ฌ์šฉ์ž ์งˆ๋ฌธ  : "์„œ์šธ ๋‚ ์”จ์™€ ํ™˜์œจ ์•Œ๋ ค์ค˜"
2. ChatClient   : ๋ชจ๋ธ์— ๋“ฑ๋ก๋œ ๋„๊ตฌ ์Šคํ‚ค๋งˆ(@Tool) ์ „์†ก
3. LLM ๊ฒฐ์ •     : getWeatherTool(), getExchangeRateTool() ํ˜ธ์ถœ ์š”์ฒญ
4. ToolCallingAdvisor : ์Šคํ”„๋ง ๋‚ด Java ๋ฉ”์„œ๋“œ ์ง์ ‘ ์‹คํ–‰ ๋ฐ ๊ฒฐ๊ณผ ์ทจํ•ฉ
5. LLM ์ตœ์ข… ์‘๋‹ต: ์ทจํ•ฉ๋œ ๋„๊ตฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ ์ž์—ฐ์Šค๋Ÿฌ์šด ํ•œ๊ตญ์–ด ๋‹ต๋ณ€ ์™„์„ฑ

๐Ÿ› ๏ธ 1. @Tool ์ปดํฌ๋„ŒํŠธ ์„ ์–ธ

package com.lucky.ai.tools;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

@Component
public class ExternalApiTools {

    public record WeatherInfo(String city, double temperature, String condition) {}

    // @Tool: ๋ชจ๋ธ์ด ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ๋„๊ตฌ ์„ค๋ช… ๋ช…์‹œ
    @Tool(description = "์ฃผ์–ด์ง„ ๋„์‹œ์˜ ์‹ค์‹œ๊ฐ„ ๋‚ ์”จ์™€ ์˜จ๋„๋ฅผ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
    public WeatherInfo getWeather(
            @ToolParam(description = "์กฐํšŒํ•  ๋„์‹œ ์ด๋ฆ„ (์˜ˆ: Seoul, Tokyo)") String city) {
        System.out.println("๋„๊ตฌ ์‹คํ–‰๋จ: city = " + city);
        // ์‹ค์ œ ๊ธฐ์ƒ์ฒญ API ์—ฐ๋™ ๋˜๋Š” DB ์ฟผ๋ฆฌ
        return new WeatherInfo(city, 23.5, "๋ง‘์Œ");
    }

    @Tool(description = "ํ†ตํ™” ์ฝ”๋“œ(USD, JPY ๋“ฑ)์— ๋Œ€ํ•œ ํ˜„์žฌ ์›ํ™”(KRW) ํ™˜์œจ์„ ์กฐํšŒํ•ฉ๋‹ˆ๋‹ค.")
    public double getExchangeRate(@ToolParam(description = "ํ†ตํ™” ์ฝ”๋“œ") String currency) {
        if ("USD".equalsIgnoreCase(currency)) return 1380.50;
        if ("JPY".equalsIgnoreCase(currency)) return 920.30;
        return 1000.0;
    }
}

๐Ÿš€ 2. ChatClient์—์„œ ๋„๊ตฌ ์žฅ์ฐฉ ๋ฐ ์‹คํ–‰

@RestController
@RequiredArgsConstructor
public class AssistantController {

    private final ChatClient chatClient;
    private final ExternalApiTools apiTools; // @Tool ์ปดํฌ๋„ŒํŠธ ์ฃผ์ž…

    @GetMapping("/ask-assistant")
    public String askAssistant(@RequestParam String query) {
        return chatClient.prompt()
                .user(query) // ์˜ˆ: "๋„์ฟ„ ๋‚ ์”จ ์•Œ๋ ค์ฃผ๊ณ  100๋‹ฌ๋Ÿฌ๋Š” ์›ํ™”๋กœ ์–ผ๋งˆ์•ผ?"
                // 2.0 ํ‘œ์ค€: .tools(...)์— ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ „๋‹ฌํ•˜๋ฉด @Tool ๋ฉ”์„œ๋“œ๊ฐ€ ์ž๋™ ๋ฐ”์ธ๋”ฉ!
                .tools(apiTools)
                .call()
                .content();
    }
}

8. RAG ์™„๋ฒฝ ๊ฐ€์ด๋“œ (Ingestion ยท Embedding ยท VectorStore ยท Advisor)

์‚ฌ๋‚ด ๋น„๊ณต๊ฐœ ๋ฌธ์„œ(์‚ฌ๊ทœ, ๋งค๋‰ด์–ผ, ์ •์ฑ… PDF)๋ฅผ ๋ฒกํ„ฐ DB์— ์ ์žฌํ•œ ๋’ค, ์‚ฌ์šฉ์ž์˜ ์งˆ๋ฌธ๊ณผ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„ LLM์˜ ์ง€์‹์œผ๋กœ ๋ณด๊ฐ•ํ•˜๋Š” RAG(๊ฒ€์ƒ‰ ์ฆ๊ฐ• ์ƒ์„ฑ) ํŒŒ์ดํ”„๋ผ์ธ์ž…๋‹ˆ๋‹ค.

[RAG ๋ฐ์ดํ„ฐ ํŒŒ์ดํ”„๋ผ์ธ]
1. Document Ingestion : PDF/Text ํŒŒ์ผ ์ฝ๊ธฐ (PagePdfDocumentReader, TextReader)
2. Text Splitting     : ์ฒญํ‚น (TokenTextSplitter: 500ํ† ํฐ ๋‹จ์œ„, 100ํ† ํฐ ์˜ค๋ฒ„๋žฉ)
3. Embedding          : ๋ฒกํ„ฐ ์ˆ˜์น˜ํ™” (EmbeddingModel: 1536์ฐจ์› ๋ฒกํ„ฐ ๋ณ€ํ™˜)
4. VectorStore Save   : pgvector, Chroma, Qdrant ๋“ฑ์— ์ €์žฅ
5. Similarity Search  : ์‚ฌ์šฉ์ž ์งˆ๋ฌธ ๋ฒกํ„ฐ์™€ ์ฝ”์‚ฌ์ธ ์œ ์‚ฌ๋„ ์ƒ์œ„ K๊ฑด ์ถ”์ถœ
6. QA Advisor         : ์งˆ๋ฌธ๊ณผ ๊ฒ€์ƒ‰๋œ ์ง€์‹์„ ๊ฒฐํ•ฉํ•˜์—ฌ ํ™˜๊ฐ(Hallucination) ์—†๋Š” ๋‹ต๋ณ€ ์ƒ์„ฑ

๐Ÿ—„๏ธ 1. VectorStore ์„ค์ • (application.yml - PGvector)

spring:
  ai:
    vectorstore:
      pgvector:
        index-type: HNSW               # HNSW (์ดˆ๊ณ ์† ๊ทผ์‚ฌ ์ตœ๊ทผ์ ‘ ํƒ์ƒ‰)
        distance-type: COSINE_DISTANCE # ์ฝ”์‚ฌ์ธ ๊ฑฐ๋ฆฌ ์ธก์ •
        dimensions: 1536               # OpenAI text-embedding-3-small ๊ธฐ์ค€ ์ฐจ์›

๐Ÿ“ฅ 2. ๋ฌธ์„œ ์ˆ˜์ง‘ & ์ฒญํ‚น & ๋ฒกํ„ฐ DB ์ ์žฌ (ETL ์„œ๋น„์Šค)

@Service
@RequiredArgsConstructor
public class DocumentIngestionService {

    private final VectorStore vectorStore;

    public void ingestManual(Resource pdfFile) {
        // 1. PDF ๋ฌธ์„œ ํŒŒ์‹ฑ
        PagePdfDocumentReader reader = new PagePdfDocumentReader(pdfFile);
        List<Document> documents = reader.get();

        // 2. ํ† ํฐ ๋‹จ์œ„ ํ…์ŠคํŠธ ๋ถ„ํ•  (์ฒญํ‚น)
        TokenTextSplitter splitter = new TokenTextSplitter(500, 100, 5, 10000, true);
        List<Document> chunks = splitter.apply(documents);

        // 3. ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๋ถ€์ฐฉ (์‚ฌํ›„ ํ•„ํ„ฐ๋ง์— ์‚ฌ์šฉ)
        chunks.forEach(c -> c.getMetadata().put("department", "HR"));

        // 4. ๋ฒกํ„ฐ ์Šคํ† ์–ด์— ์ ์žฌ (์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ ์ž๋™ ํ˜ธ์ถœ)
        vectorStore.accept(chunks);
    }
}

๐Ÿ” 3. QuestionAnswerAdvisor๋ฅผ ํ†ตํ•œ RAG ์›์Šคํ†ฑ ์งˆ๋ฌธ

@RestController
@RequiredArgsConstructor
public class RagController {

    private final ChatClient chatClient;
    private final VectorStore vectorStore;

    @GetMapping("/ask-policy")
    public String askPolicy(@RequestParam String question) {
        return chatClient.prompt()
                .user(question)
                // Spring AI๊ฐ€ ์งˆ๋ฌธ๊ณผ ์œ ์‚ฌํ•œ ์ƒ์œ„ ๋ฌธ์„œ๋ฅผ ์ฐพ์•„ ํ”„๋กฌํ”„ํŠธ์— ์ž๋™ ์ฃผ์ž…
                .advisors(new QuestionAnswerAdvisor(
                        vectorStore,
                        SearchRequest.builder()
                                .topK(4)
                                .similarityThreshold(0.75) // ์œ ์‚ฌ๋„ 75% ์ด์ƒ๋งŒ ์ฑ„ํƒ
                                .build()
                ))
                .call()
                .content();
    }
}

9. ๋Œ€ํ™” ๋ฉ”๋ชจ๋ฆฌ (Chat Memory) & ์–ด๋“œ๋ฐ”์ด์ € (Advisor) ์ฒด์ธ

Statelessํ•œ HTTP ํ™˜๊ฒฝ์—์„œ ์‚ฌ์šฉ์ž๋ณ„ ์ด์ „ ๋Œ€ํ™” ๋ฌธ๋งฅ(Multi-turn Chat)์„ ์•ˆ์ „ํ•˜๊ฒŒ ์œ ์ง€ํ•˜๊ณ  ๊ธฐ์–ตํ•ฉ๋‹ˆ๋‹ค.

๐Ÿง  1. ChatMemory ๋นˆ ๋“ฑ๋ก (์ธ๋ฉ”๋ชจ๋ฆฌ ๋˜๋Š” Redis)

@Configuration
public class ChatMemoryConfig {

    @Bean
    public ChatMemory chatMemory() {
        // ๋กœ์ปฌ/ํ…Œ์ŠคํŠธ ํ™˜๊ฒฝ: ์ธ๋ฉ”๋ชจ๋ฆฌ ์ €์žฅ์†Œ
        // ์šด์˜ ํ™˜๊ฒฝ: Redis(spring-ai-autoconfigure-model-chat-memory-repository-redis) ๋˜๋Š” JdbcChatMemory
        return new InMemoryChatMemory();
    }

    @Bean
    public ChatClient memoryChatClient(ChatClient.Builder builder, ChatMemory chatMemory) {
        return builder
                .defaultAdvisors(
                    // ์‚ฌ์šฉ์ž ์„ธ์…˜๋ณ„ ์ตœ๊ทผ 10๊ฐœ ๋ฉ”์‹œ์ง€๋ฅผ ๊ธฐ์–ตํ•˜์—ฌ ์ž๋™ ์ฃผ์ž…
                    new MessageChatMemoryAdvisor(chatMemory, "default", 10)
                )
                .build();
    }
}

๐Ÿ’ฌ 2. ์‚ฌ์šฉ์ž ID(conversationId)๋ณ„ ์„ธ์…˜ ๊ฒฉ๋ฆฌ ๋Œ€ํ™”

@GetMapping("/chat")
public String chatWithUserMemory(
        @RequestParam String userId,
        @RequestParam String message) {

    return chatClient.prompt()
            .user(message)
            // ์‚ฌ์šฉ์ž๋ณ„(userId)๋กœ ๋…๋ฆฝ๋œ ๋Œ€ํ™” ๋งฅ๋ฝ ์œ ์ง€
            .advisors(a -> a.param(ChatMemory.CONVERSATION_ID_ATTRIBUTE, userId))
            .call()
            .content();
}

๐Ÿ›ก๏ธ 3. ์ปค์Šคํ…€ ์–ด๋“œ๋ฐ”์ด์ € (ํ† ํฐ ๊ฐ์‚ฌ ๋ฐ ๋กœ๊น…)

public class AuditLoggingAdvisor implements RequestResponseAdvisor {

    @Override
    public AdvisedRequest adviseRequest(AdvisedRequest request, Map<String, Object> context) {
        System.out.println("๐Ÿค– [AI ์š”์ฒญ] " + request.userText());
        return request;
    }

    @Override
    public ChatResponse adviseResponse(ChatResponse response, Map<String, Object> context) {
        var usage = response.getMetadata().getUsage();
        System.out.println("๐Ÿ“Š [ํ† ํฐ ์†Œ๋น„] ์ž…๋ ฅ: " + usage.getPromptTokens() + 
                           " | ์ƒ์„ฑ: " + usage.getGenerationTokens());
        return response;
    }

    @Override
    public int getOrder() { return 0; }
}

10. ๋ฉ€ํ‹ฐ๋ชจ๋‹ฌ (Multimodal: Vision ์ด๋ฏธ์ง€ ๋ถ„์„ & OCR)

GPT-4o, Claude 3.5 Sonnet, Gemini ๋“ฑ ๋น„์ „ ์ง€์› ๋ชจ๋ธ์— ์ด๋ฏธ์ง€, ์˜์ˆ˜์ฆ, ๋„๋ฉด ๋“ฑ์„ ์ „์†กํ•˜์—ฌ ๋ถ„์„ ๋ฐ ํ…์ŠคํŠธ ์ถ”์ถœ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ–ผ๏ธ ์ด๋ฏธ์ง€ ๋ถ„์„ ๋ฐ ๋ฐ์ดํ„ฐ ์ถ”์ถœ

@RestController
@RequiredArgsConstructor
public class VisionController {

    private final ChatClient chatClient;

    // 1. URL ๊ธฐ๋ฐ˜ ์ด๋ฏธ์ง€ ์งˆ์˜
    @GetMapping("/analyze-chart")
    public String analyzeChartUrl() throws MalformedURLException {
        var imageUrl = new URI("https://luckytechworld.pe.kr/samples/sales-chart.png").toURL();
        var media = new Media(MimeTypeUtils.IMAGE_PNG, imageUrl);

        return chatClient.prompt()
                .user(u -> u.text("์ด ๋งค์ถœ ๊ทธ๋ž˜ํ”„์—์„œ ๊ฐ€์žฅ ์„ฑ์žฅ์ด ๋‘๋“œ๋Ÿฌ์ง„ ๋ถ„๊ธฐ๋ฅผ ๋ถ„์„ํ•˜๊ณ  ์ธ์‚ฌ์ดํŠธ๋ฅผ ๋„์ถœํ•ด์ค˜.")
                            .media(media))
                .call()
                .content();
    }

    // 2. ์‚ฌ์šฉ์ž๊ฐ€ ์—…๋กœ๋“œํ•œ ์˜์ˆ˜์ฆ/์ธ๋ณด์ด์Šค OCR ๋ถ„์„ (Record ๋งคํ•‘)
    public record InvoiceData(String vendor, String date, int totalAmount, List<String> items) {}

    @PostMapping(value = "/invoice", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public InvoiceData parseInvoice(@RequestParam("file") MultipartFile file) {
        var media = new Media(
            MimeTypeUtils.parseMimeType(file.getContentType()), 
            file.getResource()
        );

        return chatClient.prompt()
                .user(u -> u.text("์˜์ˆ˜์ฆ ์ด๋ฏธ์ง€์—์„œ ๊ณต๊ธ‰์ž, ์ผ์ž, ์ด์•ก, ํ’ˆ๋ชฉ์„ JSON ์Šคํ‚ค๋งˆ์— ๋งž์ถฐ ์ถ”์ถœํ•˜์„ธ์š”.")
                            .media(media))
                .call()
                .entity(InvoiceData.class); // Java Record๋กœ ์ž๋™ ์—ญ์ง๋ ฌํ™”!
    }
}

11. ์˜ค๋””์˜ค (Whisper STT / Speech TTS) & ์ด๋ฏธ์ง€ ์ƒ์„ฑ (DALL-E)

Spring AI๋Š” ํ…์ŠคํŠธ ๋Œ€ํ™” ๋ชจ๋ธ๋ฟ ์•„๋‹ˆ๋ผ ์Œ์„ฑ ์ „์‚ฌ(STT), ์Œ์„ฑ ํ•ฉ์„ฑ(TTS), ์ด๋ฏธ์ง€ ์ƒ์„ฑ ๋ชจ๋ธ๋„ ํ‘œ์ค€ ์ธํ„ฐํŽ˜์ด์Šค๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

๐ŸŽ™๏ธ 1. Whisper ์Œ์„ฑ ์ „์‚ฌ (STT - Speech to Text)

@Autowired
private OpenAiAudioTranscriptionModel transcriptionModel;

public String convertAudioToText(Resource audioFile) {
    var options = OpenAiAudioTranscriptionOptions.builder()
            .language("ko") // ํ•œ๊ตญ์–ด ์ „์‚ฌ ์ตœ์ ํ™”
            .temperature(0.2f)
            .responseFormat(OpenAiAudioApi.TranscriptResponseFormat.TEXT)
            .build();

    AudioTranscriptionPrompt prompt = new AudioTranscriptionPrompt(audioFile, options);
    AudioTranscriptionResponse response = transcriptionModel.call(prompt);
    return response.getResult().getOutput();
}

๐Ÿ”Š 2. TTS ํ…์ŠคํŠธ ์Œ์„ฑ ํ•ฉ์„ฑ (Text to Speech)

@Autowired
private OpenAiAudioSpeechModel speechModel;

public byte[] synthesizeSpeech(String text) {
    var options = OpenAiAudioSpeechOptions.builder()
            .voice(OpenAiAudioApi.SpeechRequest.Voice.ALLOY) // alloy, echo, fable, onyx, nova, shimmer
            .speed(1.0f)
            .responseFormat(OpenAiAudioApi.SpeechRequest.AudioResponseFormat.MP3)
            .build();

    TextToSpeechPrompt prompt = new TextToSpeechPrompt(text, options);
    return speechModel.call(prompt).getResult().getOutput(); // MP3 ๋ฐ”์ด๋„ˆ๋ฆฌ ๋ฐ”์ดํŠธ ๋ฐ˜ํ™˜
}

๐ŸŽจ 3. DALL-E 3 ์ด๋ฏธ์ง€ ์ƒ์„ฑ

@Autowired
private ImageModel imageModel;

public String generateImage(String promptText) {
    ImageResponse response = imageModel.call(
        new ImagePrompt(
            promptText,
            OpenAiImageOptions.builder()
                .quality("hd")
                .model("dall-e-3")
                .width(1024)
                .height(1024)
                .build()
        )
    );
    return response.getResult().getOutput().getUrl(); // ์ƒ์„ฑ๋œ ์ด๋ฏธ์ง€ CDN URL ๋ฐ˜ํ™˜
}

12. ํ‰๊ฐ€(Evaluation), ๋ชจ๋‹ˆํ„ฐ๋ง(Observability) & ํ”„๋กœ๋•์…˜ ๋ณด์•ˆ

Spring AI 2.0.1 ๊ธฐ๋ฐ˜ ์—”ํ„ฐํ”„๋ผ์ด์ฆˆ ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ ์š”๊ตฌ๋˜๋Š” ๊ด€์ธก์„ฑ(Micrometer) ๋ฐ ๋ณด์•ˆ ๋ชจ๋ฒ” ์‚ฌ๋ก€์ž…๋‹ˆ๋‹ค.

๐Ÿ“Š Micrometer & Prometheus ๊ด€์ธก์„ฑ (Observability)

Spring AI๋Š” ๋ณ„๋„ ์ฝ”๋“œ ์ž‘์„ฑ ์—†์ด ๋ชจ๋ธ ํ˜ธ์ถœ ์‹œ๊ฐ„, ์„ฑ๊ณต๋ฅ , ํ† ํฐ ์‚ฌ์šฉ๋Ÿ‰์„ ํ‘œ์ค€ ๋ฉ”ํŠธ๋ฆญ์œผ๋กœ ๋…ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

management:
  endpoints:
    web:
      exposure:
        include: health, info, metrics, prometheus
  metrics:
    export:
      prometheus:
        enabled: true
  • gen_ai.client.token.usage : ๋ชจ๋ธ๋ณ„ ํ”„๋กฌํ”„ํŠธ/์‘๋‹ต ํ† ํฐ ์†Œ๋น„ ์ˆ˜๋Ÿ‰ ์ถ”์ 
  • gen_ai.client.operation.duration : LLM API ํ˜ธ์ถœ ๋ ˆ์ดํ„ด์‹œ ๋ฐฑ๋ถ„์œ„์ˆ˜(p95, p99)

๐Ÿ›ก๏ธ Spring AI 2.0.1 ๋ณด์•ˆ & ์šด์˜ ํ•ต์‹ฌ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

1. ๋„๊ตฌ ์‹คํ–‰ ๋ณด์•ˆ   : 2.0.1๋ถ€ํ„ฐ spring.ai.tools.resolution.fallback.enabled=false ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ 
                     ๋ช…์‹œ์ ์œผ๋กœ .tools()์— ๋„˜๊ธด ๋„๊ตฌ๋งŒ ์‹คํ–‰ํ•˜์—ฌ ์›๊ฒฉ ๋„๊ตฌ ํƒˆ์ทจ ๋ฐฉ์ง€ (CVE-2026-59318)
2. API ํ‚ค ๋ณดํ˜ธ     : Git ์ปค๋ฐ‹ ์ ˆ๋Œ€ ๊ธˆ์ง€. Vault, AWS Secrets Manager ๋˜๋Š” ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ฃผ์ž…
3. ํ”„๋กฌํ”„ํŠธ ์ธ์ ์…˜ : ์‚ฌ์šฉ์ž ์ž…๋ ฅ์„ SystemMessage์™€ ๋ถ„๋ฆฌํ•˜๊ณ  UserMessage ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์ž…
4. ์„œํ‚ท ๋ธŒ๋ ˆ์ด์ปค   : Resilience4j์™€ ์—ฐ๋™ํ•˜์—ฌ LLM ์žฅ์•  ์‹œ Fallback ๊ณ ์ • ์‘๋‹ต ๋ฐ˜ํ™˜
5. ํƒ€์ž„์•„์›ƒ ๋ฐฉ์–ด   : RestClient์˜ readTimeout์„ ๋ฐ˜๋“œ์‹œ 30~60์ดˆ๋กœ ๋ช…์‹œ (๋ฌดํ•œ ๋ธ”๋กœํ‚น ๋ฐฉ์ง€)

โฑ๏ธ RestClient ํƒ€์ž„์•„์›ƒ ์ปค์Šคํ…€ ๋นˆ

@Bean
public RestClientCustomizer restClientCustomizer() {
    return restClientBuilder -> restClientBuilder
            .requestFactory(new SimpleClientHttpRequestFactory() {{
                setConnectTimeout(Duration.ofSeconds(10)); // ์—ฐ๊ฒฐ ์ œํ•œ 10์ดˆ
                setReadTimeout(Duration.ofSeconds(60));    // ์‘๋‹ต ๋Œ€๊ธฐ ์ œํ•œ 60์ดˆ
            }});
}
๐Ÿ’ฌ

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

0
0 / 1500