fix(rag): 네이버 뉴스 기사 제목 기반 타겟 종목명 엄격 필터링 및 루씬 잡음 기사 차단/리셋 처리
This commit is contained in:
@@ -59,21 +59,23 @@ object NewsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
suspend fun fetchAndIngestNews(corpInfo: CorpInfo) {
|
suspend fun fetchAndIngestNews(corpInfo: CorpInfo) {
|
||||||
val clientId = KisSession.config.nAppKey // 설정에서 가져오도록 수정 필요
|
val clientId = KisSession.config.nAppKey
|
||||||
val clientSecret = KisSession.config.nSecretKey
|
val clientSecret = KisSession.config.nSecretKey
|
||||||
|
val cleanStockName = corpInfo.stockName.replace("(주)", "").trim()
|
||||||
|
val cleanCorpName = corpInfo.cName.replace("(주)", "").trim()
|
||||||
|
|
||||||
val qlistNews = listOf(
|
val qlistNews = listOf(
|
||||||
"${corpInfo.stockName} 주가",
|
"\"$cleanStockName\"",
|
||||||
"${corpInfo.stockName} 실적",
|
"\"$cleanStockName\" 실적",
|
||||||
"${corpInfo.stockName} 공시",
|
"\"$cleanStockName\" 공시"
|
||||||
// "${corpInfo.stockName} 이벤트"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
val qlistCorpTrend = listOf(
|
val qlistCorpTrend = if (cleanCorpName.isNotBlank() && cleanCorpName != cleanStockName) {
|
||||||
"${corpInfo.cName} 최근 동향",
|
listOf("\"$cleanCorpName\"")
|
||||||
"${corpInfo.cName} 이슈",
|
} else {
|
||||||
// "${corpInfo.cName} 투자",
|
emptyList()
|
||||||
// "${corpInfo.cName} 실적"
|
}
|
||||||
)
|
|
||||||
(qlistNews + qlistCorpTrend).forEach { query ->
|
(qlistNews + qlistCorpTrend).forEach { query ->
|
||||||
try {
|
try {
|
||||||
val formatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss Z", Locale.ENGLISH)
|
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") {
|
val response: NaverNewsResponse = client.get("https://openapi.naver.com/v1/search/news.json") {
|
||||||
parameter("query", query)
|
parameter("query", query)
|
||||||
parameter("display", 4) // 최근 10개 뉴스
|
parameter("display", 10) // 10건 검색 후 엄격 필터링
|
||||||
parameter("sort", "date") // 유사도 순 (또는 date 발간순)
|
parameter("sort", "date") // 발간일 순
|
||||||
header("X-Naver-Client-Id", clientId)
|
header("X-Naver-Client-Id", clientId)
|
||||||
header("X-Naver-Client-Secret", clientSecret)
|
header("X-Naver-Client-Secret", clientSecret)
|
||||||
}.body()
|
}.body()
|
||||||
|
|
||||||
val todayItems = response.items.filter { item ->
|
val todayItems = response.items.filter { item ->
|
||||||
try {
|
try {
|
||||||
val pubDate = ZonedDateTime.parse(item.pubDate, formatter)
|
val pubDate = ZonedDateTime.parse(item.pubDate, formatter)
|
||||||
pubDate.toLocalDate() == today // 날짜가 오늘과 일치하는지 확인
|
val isToday = pubDate.toLocalDate() == today
|
||||||
|
val cleanTitle = item.title
|
||||||
|
.replace("<b>", "")
|
||||||
|
.replace("</b>", "")
|
||||||
|
.replace(""", "\"")
|
||||||
|
.replace("&", "&")
|
||||||
|
.replace("<", "<")
|
||||||
|
.replace(">", ">")
|
||||||
|
|
||||||
|
// 기사 제목에 타겟 종목명 또는 회사명이 반드시 포함되어 있는지 엄격 검증
|
||||||
|
val mentionsTarget = cleanTitle.contains(cleanStockName) ||
|
||||||
|
(cleanCorpName.isNotBlank() && cleanTitle.contains(cleanCorpName))
|
||||||
|
|
||||||
|
isToday && mentionsTarget
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
@@ -101,7 +117,7 @@ object NewsService {
|
|||||||
.take(2)
|
.take(2)
|
||||||
|
|
||||||
if (finalItems.isNotEmpty()) {
|
if (finalItems.isNotEmpty()) {
|
||||||
println("📰 [NaverNews] '${corpInfo.stockName}' 검색어: '$query' -> 스크래핑 대상 ${finalItems.size}건:")
|
println("📰 [NaverNews] '${corpInfo.stockName}' 검색어: '$query' -> 필터 통과 기사 ${finalItems.size}건:")
|
||||||
finalItems.forEach {
|
finalItems.forEach {
|
||||||
val cleanTitle = it.title.replace("<b>", "").replace("</b>", "").replace(""", "\"")
|
val cleanTitle = it.title.replace("<b>", "").replace("</b>", "").replace(""", "\"")
|
||||||
println(" - $cleanTitle (${it.originallink})")
|
println(" - $cleanTitle (${it.originallink})")
|
||||||
|
|||||||
@@ -381,11 +381,22 @@ object RagService {
|
|||||||
}
|
}
|
||||||
println("================================================================================")
|
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)
|
.take(4)
|
||||||
.joinToString("\n\n") {
|
.joinToString("\n\n") {
|
||||||
it.embedded().text()
|
it.embedded().text()
|
||||||
}
|
}.ifBlank { null }
|
||||||
|
|
||||||
val finalDecision = decideTrading(stockName, scores, financialStmt, tradingDecision)
|
val finalDecision = decideTrading(stockName, scores, financialStmt, tradingDecision)
|
||||||
val ragAiDuration = System.currentTimeMillis() - ragStartTime
|
val ragAiDuration = System.currentTimeMillis() - ragStartTime
|
||||||
@@ -635,11 +646,11 @@ object RagService {
|
|||||||
*/
|
*/
|
||||||
private fun getCurrentEmbeddingTag(): String {
|
private fun getCurrentEmbeddingTag(): String {
|
||||||
return if (LlamaServerManager.isOllamaEmbeddingActive) {
|
return if (LlamaServerManager.isOllamaEmbeddingActive) {
|
||||||
"ollama:${LlamaServerManager.ollamaEmbeddingModel}"
|
"ollama:${LlamaServerManager.ollamaEmbeddingModel}:v2_filtered"
|
||||||
} else if (KisSession.config.embedModelPath.isNotBlank()) {
|
} 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 {
|
} else {
|
||||||
"local:default"
|
"local:default:v2_filtered"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user