From 3119d9d43f2b955e248f3364456c1b2c5c19134a Mon Sep 17 00:00:00 2001 From: lunaticbum Date: Wed, 18 Nov 2026 16:34:32 +0900 Subject: [PATCH] =?UTF-8?q?fix(rag):=20LLM=20=EB=89=B4=EC=8A=A4=20?= =?UTF-8?q?=EC=A0=90=EC=88=98=20=EB=B6=84=EC=84=9D=20=EC=8B=9C=20thinking?= =?UTF-8?q?=20=EB=AA=A8=EB=8D=B8=20=ED=86=A0=ED=81=B0=20=EB=B6=80=EC=A1=B1?= =?UTF-8?q?=20=EB=B0=8F=20content=20=EA=B3=B5=EB=B0=B1=20=EC=9D=B4?= =?UTF-8?q?=EC=8A=88=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/network/RagService.kt | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/network/RagService.kt b/src/main/kotlin/network/RagService.kt index 4e5a3bb..158ce79 100644 --- a/src/main/kotlin/network/RagService.kt +++ b/src/main/kotlin/network/RagService.kt @@ -18,6 +18,7 @@ import dev.langchain4j.store.embedding.EmbeddingSearchResult import dev.langchain4j.store.embedding.filter.MetadataFilterBuilder import kotlinx.coroutines.coroutineScope import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.addJsonObject import kotlinx.serialization.json.buildJsonObject import kotlinx.serialization.json.double @@ -690,11 +691,11 @@ object RagService { put("temperature", 0.1) put("top_p", 0.85) put("top_k", 40) - put("max_tokens", 200) + put("max_tokens", 1500) putJsonArray("messages") { addJsonObject { put("role", "system") - put("content", "You are a helpful AI financial analyst. You must output responses ONLY in valid JSON format.") + put("content", "You are a helpful AI financial analyst. Keep your internal thinking concise (under 50 words), then output strictly in valid JSON format.") } addJsonObject { put("role", "user") @@ -718,7 +719,20 @@ object RagService { val responseBody = response.body?.string() ?: "{}" val json = Json.parseToJsonElement(responseBody).jsonObject - json["choices"]?.jsonArray?.get(0)?.jsonObject?.get("message")?.jsonObject?.get("content")?.jsonPrimitive?.content ?: "{}" + 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 + } + } + content.ifBlank { "{}" } } } } @@ -904,7 +918,7 @@ object RagService { return try { val raw = callLlamaWithSchema(prompt) - println("getAiNewsScore $raw") + println("getAiNewsScore: $raw") val startIndex = raw.indexOf("{") val endIndex = raw.lastIndexOf("}") @@ -921,6 +935,7 @@ object RagService { score to reason } catch (e: Exception) { + println("⚠️ [RagService] 뉴스 분석 파싱 실패 (${e.message})") 50.0 to "뉴스 분석 오류 발생 (중립 처리)" } }