diff --git a/src/main/kotlin/network/NewsService.kt b/src/main/kotlin/network/NewsService.kt index 76407fe..ab8e0fb 100644 --- a/src/main/kotlin/network/NewsService.kt +++ b/src/main/kotlin/network/NewsService.kt @@ -59,21 +59,23 @@ object NewsService { } suspend fun fetchAndIngestNews(corpInfo: CorpInfo) { - val clientId = KisSession.config.nAppKey // 설정에서 가져오도록 수정 필요 + val clientId = KisSession.config.nAppKey val clientSecret = KisSession.config.nSecretKey + val cleanStockName = corpInfo.stockName.replace("(주)", "").trim() + val cleanCorpName = corpInfo.cName.replace("(주)", "").trim() + val qlistNews = listOf( - "${corpInfo.stockName} 주가", - "${corpInfo.stockName} 실적", - "${corpInfo.stockName} 공시", -// "${corpInfo.stockName} 이벤트" + "\"$cleanStockName\"", + "\"$cleanStockName\" 실적", + "\"$cleanStockName\" 공시" ) - val qlistCorpTrend = listOf( - "${corpInfo.cName} 최근 동향", - "${corpInfo.cName} 이슈", -// "${corpInfo.cName} 투자", -// "${corpInfo.cName} 실적" - ) + val qlistCorpTrend = if (cleanCorpName.isNotBlank() && cleanCorpName != cleanStockName) { + listOf("\"$cleanCorpName\"") + } else { + emptyList() + } + (qlistNews + qlistCorpTrend).forEach { query -> try { val formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH) @@ -81,15 +83,29 @@ object NewsService { val response: NaverNewsResponse = client.get("https://openapi.naver.com/v1/search/news.json") { parameter("query", query) - parameter("display", 4) // 최근 10개 뉴스 - parameter("sort", "date") // 유사도 순 (또는 date 발간순) + parameter("display", 10) // 10건 검색 후 엄격 필터링 + parameter("sort", "date") // 발간일 순 header("X-Naver-Client-Id", clientId) header("X-Naver-Client-Secret", clientSecret) }.body() + val todayItems = response.items.filter { item -> try { val pubDate = ZonedDateTime.parse(item.pubDate, formatter) - pubDate.toLocalDate() == today // 날짜가 오늘과 일치하는지 확인 + val isToday = pubDate.toLocalDate() == today + val cleanTitle = item.title + .replace("", "") + .replace("", "") + .replace(""", "\"") + .replace("&", "&") + .replace("<", "<") + .replace(">", ">") + + // 기사 제목에 타겟 종목명 또는 회사명이 반드시 포함되어 있는지 엄격 검증 + val mentionsTarget = cleanTitle.contains(cleanStockName) || + (cleanCorpName.isNotBlank() && cleanTitle.contains(cleanCorpName)) + + isToday && mentionsTarget } catch (e: Exception) { false } @@ -101,7 +117,7 @@ object NewsService { .take(2) if (finalItems.isNotEmpty()) { - println("📰 [NaverNews] '${corpInfo.stockName}' 검색어: '$query' -> 스크래핑 대상 ${finalItems.size}건:") + println("📰 [NaverNews] '${corpInfo.stockName}' 검색어: '$query' -> 필터 통과 기사 ${finalItems.size}건:") finalItems.forEach { val cleanTitle = it.title.replace("", "").replace("", "").replace(""", "\"") println(" - $cleanTitle (${it.originallink})") diff --git a/src/main/kotlin/network/RagService.kt b/src/main/kotlin/network/RagService.kt index 55aed38..83b93ee 100644 --- a/src/main/kotlin/network/RagService.kt +++ b/src/main/kotlin/network/RagService.kt @@ -381,11 +381,22 @@ object RagService { } println("================================================================================") - tradingDecision.newsContext = matches.distinct() + val cleanName = stockName.replace("(주)", "").trim() + val targetMatches = matches.filter { match -> + val title = match.embedded().metadata().getString("title") ?: "" + val text = match.embedded().text() + title.contains(cleanName) || text.contains(cleanName) + } + + if (targetMatches.isEmpty() && matches.isNotEmpty()) { + println("⚠️ [RagService] 검색된 ${matches.size}개 기사 중 [$stockName]과 직접 관련된 기사가 없어 LLM 분석을 건너뜁니다.") + } + + tradingDecision.newsContext = targetMatches.distinct() .take(4) .joinToString("\n\n") { it.embedded().text() - } + }.ifBlank { null } val finalDecision = decideTrading(stockName, scores, financialStmt, tradingDecision) val ragAiDuration = System.currentTimeMillis() - ragStartTime @@ -635,11 +646,11 @@ object RagService { */ private fun getCurrentEmbeddingTag(): String { return if (LlamaServerManager.isOllamaEmbeddingActive) { - "ollama:${LlamaServerManager.ollamaEmbeddingModel}" + "ollama:${LlamaServerManager.ollamaEmbeddingModel}:v2_filtered" } else if (KisSession.config.embedModelPath.isNotBlank()) { - "local:${java.io.File(KisSession.config.embedModelPath).name}" + "local:${java.io.File(KisSession.config.embedModelPath).name}:v2_filtered" } else { - "local:default" + "local:default:v2_filtered" } }