...
This commit is contained in:
@@ -618,7 +618,19 @@ object RagService {
|
||||
return try {
|
||||
val raw = callLlamaWithSchema(prompt)
|
||||
println("getAiNewsScore $raw")
|
||||
val json = Json { ignoreUnknownKeys = true }.parseToJsonElement(raw).jsonObject
|
||||
// 💡 [핵심 해결책] 마크다운 블록 및 앞뒤 쓸데없는 텍스트 완벽 차단
|
||||
val startIndex = raw.indexOf("{")
|
||||
val endIndex = raw.lastIndexOf("}")
|
||||
|
||||
// '{' 부터 '}' 까지만 정확하게 잘라냄
|
||||
val sanitizedRaw = if (startIndex != -1 && endIndex != -1) {
|
||||
raw.substring(startIndex, endIndex + 1)
|
||||
} else {
|
||||
raw // 중괄호를 못 찾았을 경우 대비 (기본 fallback)
|
||||
}
|
||||
|
||||
// 정제된 문자열로 JSON 파싱
|
||||
val json = Json { ignoreUnknownKeys = true }.parseToJsonElement(sanitizedRaw).jsonObject
|
||||
|
||||
val score = json["score"]?.jsonPrimitive?.double ?: 50.0
|
||||
val reason = json["reason"]?.jsonPrimitive?.content ?: "뉴스 분석 완료"
|
||||
|
||||
Reference in New Issue
Block a user