fix(rag): LLM 뉴스 점수 분석 시 thinking 모델 토큰 부족 및 content 공백 이슈 해결
This commit is contained in:
@@ -18,6 +18,7 @@ import dev.langchain4j.store.embedding.EmbeddingSearchResult
|
|||||||
import dev.langchain4j.store.embedding.filter.MetadataFilterBuilder
|
import dev.langchain4j.store.embedding.filter.MetadataFilterBuilder
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
|
import kotlinx.serialization.json.JsonPrimitive
|
||||||
import kotlinx.serialization.json.addJsonObject
|
import kotlinx.serialization.json.addJsonObject
|
||||||
import kotlinx.serialization.json.buildJsonObject
|
import kotlinx.serialization.json.buildJsonObject
|
||||||
import kotlinx.serialization.json.double
|
import kotlinx.serialization.json.double
|
||||||
@@ -690,11 +691,11 @@ object RagService {
|
|||||||
put("temperature", 0.1)
|
put("temperature", 0.1)
|
||||||
put("top_p", 0.85)
|
put("top_p", 0.85)
|
||||||
put("top_k", 40)
|
put("top_k", 40)
|
||||||
put("max_tokens", 200)
|
put("max_tokens", 1500)
|
||||||
putJsonArray("messages") {
|
putJsonArray("messages") {
|
||||||
addJsonObject {
|
addJsonObject {
|
||||||
put("role", "system")
|
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 {
|
addJsonObject {
|
||||||
put("role", "user")
|
put("role", "user")
|
||||||
@@ -718,7 +719,20 @@ object RagService {
|
|||||||
|
|
||||||
val responseBody = response.body?.string() ?: "{}"
|
val responseBody = response.body?.string() ?: "{}"
|
||||||
val json = Json.parseToJsonElement(responseBody).jsonObject
|
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 {
|
return try {
|
||||||
val raw = callLlamaWithSchema(prompt)
|
val raw = callLlamaWithSchema(prompt)
|
||||||
println("getAiNewsScore $raw")
|
println("getAiNewsScore: $raw")
|
||||||
val startIndex = raw.indexOf("{")
|
val startIndex = raw.indexOf("{")
|
||||||
val endIndex = raw.lastIndexOf("}")
|
val endIndex = raw.lastIndexOf("}")
|
||||||
|
|
||||||
@@ -921,6 +935,7 @@ object RagService {
|
|||||||
|
|
||||||
score to reason
|
score to reason
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
println("⚠️ [RagService] 뉴스 분석 파싱 실패 (${e.message})")
|
||||||
50.0 to "뉴스 분석 오류 발생 (중립 처리)"
|
50.0 to "뉴스 분석 오류 발생 (중립 처리)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user