From ff464766706dcb6b3331833385ec79f0d705df5a Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Wed, 18 Nov 2026 16:41:44 +0900 Subject: [PATCH] =?UTF-8?q?feat(rag):=20Ollama=20=ED=86=B5=EC=8B=A0=20?= =?UTF-8?q?=EA=B3=BC=EC=A0=95=20=EB=B0=8F=20LLM=20=EC=9D=91=EB=8B=B5/?= =?UTF-8?q?=ED=86=A0=ED=81=B0/=EC=B6=94=EB=A1=A0=20=EC=83=81=EC=84=B8=20?= =?UTF-8?q?=EB=94=94=EB=B2=84=EA=B7=B8=20=EB=A1=9C=EA=B9=85=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/network/RagService.kt | 77 ++++++++++++++++++++------- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/network/RagService.kt b/src/main/kotlin/network/RagService.kt index 158ce79..92d214b 100644 --- a/src/main/kotlin/network/RagService.kt +++ b/src/main/kotlin/network/RagService.kt @@ -685,6 +685,7 @@ object RagService { private suspend fun callLlamaWithSchema(prompt: String): String { val jsonMediaType = "application/json; charset=utf-8".toMediaType() val activeModel = resolveActiveModelName() + val apiUrl = LLM_API_URL() val requestBodyJson = buildJsonObject { put("model", activeModel) @@ -708,31 +709,66 @@ object RagService { }.toString() val request = Request.Builder() - .url(LLM_API_URL()) + .url(apiUrl) .post(requestBodyJson.toRequestBody(jsonMediaType)) .build() + println("๐Ÿ“ก [Ollama ํ†ต์‹  ์‹œ์ž‘] URL: $apiUrl | Model: $activeModel | Prompt ๊ธธ์ด: ${prompt.length}์ž") + val startTime = System.currentTimeMillis() + return kotlinx.coroutines.Dispatchers.IO.let { kotlinx.coroutines.withContext(it) { - httpClient.newCall(request).execute().use { response -> - if (!response.isSuccessful) throw Exception("LLM API Error: ${response.code} ${response.message}") - - val responseBody = response.body?.string() ?: "{}" - val json = Json.parseToJsonElement(responseBody).jsonObject - val message = json["choices"]?.jsonArray?.getOrNull(0)?.jsonObject?.get("message")?.jsonObject - val rawContent = (message?.get("content") as? JsonPrimitive)?.content?.trim() ?: "" - var content = rawContent - - // Thinking ๋ชจ๋ธ(Nemotron ๋“ฑ)์—์„œ max_tokens ์ดˆ๊ณผ ๋“ฑ์œผ๋กœ content๊ฐ€ ๋น„์–ด์žˆ์„ ๊ฒฝ์šฐ reasoning์—์„œ fallback ์ถ”์ถœ - if (content.isBlank()) { - val reasoning = (message?.get("reasoning") as? JsonPrimitive)?.content ?: "" - val jsonRegex = Regex("""\{[^{}]*"score"[^{}]*\}""") - val match = jsonRegex.find(reasoning) - if (match != null) { - content = match.value + try { + httpClient.newCall(request).execute().use { response -> + val duration = System.currentTimeMillis() - startTime + if (!response.isSuccessful) { + val errBody = response.body?.string() ?: "" + println("โŒ [Ollama ํ†ต์‹  ์‹คํŒจ] HTTP ${response.code} ${response.message} (${duration}ms) | ๋‚ด์šฉ: $errBody") + throw Exception("LLM API Error: ${response.code} ${response.message} - $errBody") } + + val responseBody = response.body?.string() ?: "{}" + println("๐Ÿ“ฅ [Ollama ์‘๋‹ต ์ˆ˜์‹ ] HTTP ${response.code} (${duration}ms) | ์›๋ณธ ์‘๋‹ต ํฌ๊ธฐ: ${responseBody.length}์ž") + + val json = Json.parseToJsonElement(responseBody).jsonObject + val choice = json["choices"]?.jsonArray?.getOrNull(0)?.jsonObject + val finishReason = choice?.get("finish_reason")?.jsonPrimitive?.content ?: "unknown" + val usage = json["usage"]?.jsonObject + val promptTokens = usage?.get("prompt_tokens")?.jsonPrimitive?.content ?: "?" + val completionTokens = usage?.get("completion_tokens")?.jsonPrimitive?.content ?: "?" + + val message = choice?.get("message")?.jsonObject + val rawContent = (message?.get("content") as? JsonPrimitive)?.content?.trim() ?: "" + val reasoning = (message?.get("reasoning") as? JsonPrimitive)?.content ?: "" + + println("๐Ÿ“Š [Ollama ์‘๋‹ต ๋ถ„์„] finish_reason: $finishReason | ํ† ํฐ: (prompt: $promptTokens, completion: $completionTokens)") + + if (reasoning.isNotBlank()) { + val preview = if (reasoning.length > 200) "${reasoning.take(200)}..." else reasoning + println("๐Ÿง  [Ollama ์ถ”๋ก (Reasoning) ${reasoning.length}์ž]: ${preview.replace("\n", " ")}") + } + + var content = rawContent + if (content.isBlank()) { + println("โš ๏ธ [Ollama Content ๋น„์–ด์žˆ์Œ] finish_reason=$finishReason, Reasoning์—์„œ JSON ํŒจํ„ด ๋ณต๊ตฌ ์‹œ๋„ ์ค‘...") + val jsonRegex = Regex("""\{[\s\S]*?"score"[\s\S]*?\}""") + val match = jsonRegex.find(reasoning) + if (match != null) { + content = match.value + println("๐ŸŽฏ [Ollama Reasoning ๋ณต๊ตฌ ์„ฑ๊ณต]: $content") + } else { + println("โŒ [Ollama Reasoning์—์„œ๋„ JSON ์ถ”์ถœ ์‹คํŒจ]. ์›๋ณธ ์‘๋‹ต:\n$responseBody") + } + } else { + println("๐Ÿ’ฌ [Ollama Content ์ˆ˜์‹ ]: $content") + } + + content.ifBlank { "{}" } } - content.ifBlank { "{}" } + } catch (e: Exception) { + val duration = System.currentTimeMillis() - startTime + println("โŒ [Ollama ํ†ต์‹  ์˜ˆ์™ธ ๋ฐœ์ƒ (${duration}ms)]: ${e.message}") + throw e } } } @@ -916,9 +952,9 @@ object RagService { } """.trimIndent() + println("๐Ÿ“ฐ [RagService.getAiNewsScore] ๋Œ€์ƒ ์ข…๋ชฉ: [$stockName] | ๋‰ด์Šค ํ…์ŠคํŠธ ๊ธธ์ด: ${news.length}์ž | ๋งˆ์ผ“ ์ปจํ…์ŠคํŠธ: ${techSummary.length}์ž") return try { val raw = callLlamaWithSchema(prompt) - println("getAiNewsScore: $raw") val startIndex = raw.indexOf("{") val endIndex = raw.lastIndexOf("}") @@ -932,10 +968,11 @@ object RagService { val score = json["score"]?.jsonPrimitive?.double ?: 50.0 val reason = json["reason"]?.jsonPrimitive?.content ?: "๋‰ด์Šค ๋ถ„์„ ์™„๋ฃŒ" + println("๐Ÿท๏ธ [RagService.getAiNewsScore ๊ฒฐ๊ณผ] [$stockName] -> ์ ์ˆ˜: $score | ์‚ฌ์œ : $reason") score to reason } catch (e: Exception) { - println("โš ๏ธ [RagService] ๋‰ด์Šค ๋ถ„์„ ํŒŒ์‹ฑ ์‹คํŒจ (${e.message})") + println("โš ๏ธ [RagService.getAiNewsScore] ํŒŒ์‹ฑ ์˜ˆ์™ธ ๋ฐœ์ƒ (${e.message}) -> 50.0 ์ค‘๋ฆฝ ์ฒ˜๋ฆฌ") 50.0 to "๋‰ด์Šค ๋ถ„์„ ์˜ค๋ฅ˜ ๋ฐœ์ƒ (์ค‘๋ฆฝ ์ฒ˜๋ฆฌ)" } }