|
| 1 | +package com.example.spring_ai_tutorial.controller |
| 2 | + |
| 3 | +import com.example.spring_ai_tutorial.service.ChatService |
| 4 | +import io.github.oshai.kotlinlogging.KotlinLogging |
| 5 | +import io.swagger.v3.oas.annotations.Operation |
| 6 | +import io.swagger.v3.oas.annotations.Parameter |
| 7 | +import io.swagger.v3.oas.annotations.media.Content |
| 8 | +import io.swagger.v3.oas.annotations.media.Schema |
| 9 | +import io.swagger.v3.oas.annotations.responses.ApiResponse as SwaggerResponse |
| 10 | +import io.swagger.v3.oas.annotations.tags.Tag |
| 11 | +import org.springframework.http.HttpStatus |
| 12 | +import org.springframework.http.ResponseEntity |
| 13 | +import org.springframework.web.bind.annotation.* |
| 14 | + |
| 15 | +/** |
| 16 | + * Chat API 컨트롤러 |
| 17 | + * |
| 18 | + * LLM API를 통해 채팅 기능을 제공합니다. |
| 19 | + */ |
| 20 | +@RestController |
| 21 | +@RequestMapping("/api/v1/chat") |
| 22 | +@Tag(name = "Chat API", description = "OpenAI API를 통한 채팅 기능") |
| 23 | +class ChatController( |
| 24 | + private val chatService: ChatService |
| 25 | +) { |
| 26 | + private val logger = KotlinLogging.logger {} |
| 27 | + |
| 28 | + /** |
| 29 | + * 사용자의 메시지를 받아 OpenAI API로 응답 생성 |
| 30 | + */ |
| 31 | + @Operation( |
| 32 | + summary = "LLM 채팅 메시지 전송", |
| 33 | + description = "사용자의 메시지를 받아 OpenAI API를 통해 응답을 생성합니다." |
| 34 | + ) |
| 35 | + @SwaggerResponse( |
| 36 | + responseCode = "200", |
| 37 | + description = "LLM 응답 성공", |
| 38 | + content = [Content(schema = Schema(implementation = ApiResponse::class))] |
| 39 | + ) |
| 40 | + @SwaggerResponse(responseCode = "400", description = "잘못된 요청") |
| 41 | + @SwaggerResponse(responseCode = "500", description = "서버 오류") |
| 42 | + @PostMapping("/query") |
| 43 | + suspend fun sendMessage( |
| 44 | + @Parameter(description = "채팅 요청 객체", required = true) |
| 45 | + @RequestBody request: ChatRequest |
| 46 | + ): ResponseEntity<ApiResponse<Map<String, Any>>> { |
| 47 | + logger.info { "Chat API 요청 받음: model=${request.model}" } |
| 48 | + |
| 49 | + // 유효성 검사 |
| 50 | + if (request.query.isBlank()) { |
| 51 | + logger.warn { "빈 질의가 요청됨" } |
| 52 | + return ResponseEntity.badRequest().body( |
| 53 | + ApiResponse(success = false, error = "질의가 비어있습니다.") |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + return try { |
| 58 | + // 시스템 프롬프트 지정 |
| 59 | + val systemMessage = "You are a helpful AI assistant." |
| 60 | + |
| 61 | + // AI 응답 생성 |
| 62 | + val response = chatService.openAiChat( |
| 63 | + userInput = request.query, |
| 64 | + systemMessage = systemMessage, |
| 65 | + model = request.model |
| 66 | + ) |
| 67 | + logger.debug { "LLM 응답 생성: $response" } |
| 68 | + |
| 69 | + response?.let { chatResponse -> |
| 70 | + ResponseEntity.ok( |
| 71 | + ApiResponse( |
| 72 | + success = true, |
| 73 | + data = mapOf("answer" to chatResponse.result.output.text) |
| 74 | + ) |
| 75 | + ) |
| 76 | + } ?: run { |
| 77 | + logger.error { "LLM 응답 생성 실패" } |
| 78 | + ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body( |
| 79 | + ApiResponse( |
| 80 | + success = false, |
| 81 | + error = "LLM 응답 생성 중 오류 발생" |
| 82 | + ) |
| 83 | + ) |
| 84 | + } |
| 85 | + } catch (e: Exception) { |
| 86 | + logger.error(e) { "Chat API 처리 중 오류 발생" } |
| 87 | + ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body( |
| 88 | + ApiResponse( |
| 89 | + success = false, |
| 90 | + error = e.message ?: "알 수 없는 오류 발생" |
| 91 | + ) |
| 92 | + ) |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +@Schema(description = "채팅 요청 데이터 모델") |
| 98 | +data class ChatRequest( |
| 99 | + @Schema(description = "사용자 질문", example = "안녕하세요") |
| 100 | + val query: String, |
| 101 | + |
| 102 | + @Schema(description = "사용할 LLM 모델", example = "gpt-3.5-turbo", defaultValue = "gpt-3.5-turbo") |
| 103 | + val model: String = "gpt-3.5-turbo" |
| 104 | +) |
| 105 | + |
| 106 | +@Schema(description = "API 응답 포맷") |
| 107 | +data class ApiResponse<T>( |
| 108 | + @Schema(description = "요청 처리 성공 여부") |
| 109 | + val success: Boolean, |
| 110 | + |
| 111 | + @Schema(description = "성공 응답 데이터") |
| 112 | + val data: T? = null, |
| 113 | + |
| 114 | + @Schema(description = "실패 오류 메시지") |
| 115 | + val error: String? = null |
| 116 | +) |
0 commit comments