feat(rag): Ollama 통신 과정 및 LLM 응답/토큰/추론 상세 디버그 로깅 추가
This commit is contained in:
@@ -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,32 +709,67 @@ 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) {
|
||||
try {
|
||||
httpClient.newCall(request).execute().use { response ->
|
||||
if (!response.isSuccessful) throw Exception("LLM API Error: ${response.code} ${response.message}")
|
||||
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() ?: "{}"
|
||||
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
|
||||
println("📥 [Ollama 응답 수신] HTTP ${response.code} (${duration}ms) | 원본 응답 크기: ${responseBody.length}자")
|
||||
|
||||
// Thinking 모델(Nemotron 등)에서 max_tokens 초과 등으로 content가 비어있을 경우 reasoning에서 fallback 추출
|
||||
if (content.isBlank()) {
|
||||
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 ?: ""
|
||||
val jsonRegex = Regex("""\{[^{}]*"score"[^{}]*\}""")
|
||||
|
||||
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 { "{}" }
|
||||
}
|
||||
} 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 "뉴스 분석 오류 발생 (중립 처리)"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user