feat(rag): 메인 트레이딩 루프 0초 블로킹을 위한 비동기 백그라운드 AI 분석 워커 큐 아키텍처 구축
This commit is contained in:
@@ -16,7 +16,12 @@ import dev.langchain4j.model.openai.OpenAiEmbeddingModel
|
||||
import dev.langchain4j.store.embedding.EmbeddingSearchRequest
|
||||
import dev.langchain4j.store.embedding.EmbeddingSearchResult
|
||||
import dev.langchain4j.store.embedding.filter.MetadataFilterBuilder
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import kotlinx.serialization.json.addJsonObject
|
||||
@@ -53,9 +58,43 @@ import java.util.concurrent.TimeUnit
|
||||
object RagService {
|
||||
val isSafetyBeltStockCodes = ConcurrentHashMap.newKeySet<String>()
|
||||
|
||||
private data class AiAnalysisTask(
|
||||
val currentPrice: Double,
|
||||
val technicalAnalyzer: TechnicalAnalyzer,
|
||||
val stockName: String,
|
||||
val stockCode: String,
|
||||
val isWatering: Boolean,
|
||||
val scores: InvestmentScores,
|
||||
val financialStmt: FinancialStatement,
|
||||
val tradingDecision: TradingDecision,
|
||||
val finDuration: Long,
|
||||
val techDuration: Long,
|
||||
val totalStartTime: Long,
|
||||
val result: TradingDecisionCallback
|
||||
)
|
||||
|
||||
private val aiTaskChannel = Channel<AiAnalysisTask>(Channel.UNLIMITED)
|
||||
private val aiScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
private val analyzingStocks = ConcurrentHashMap.newKeySet<String>()
|
||||
|
||||
init {
|
||||
aiScope.launch {
|
||||
for (task in aiTaskChannel) {
|
||||
try {
|
||||
processAiNewsAnalysis(task)
|
||||
} catch (e: Exception) {
|
||||
println("❌ [RagService AI 워커] ${task.stockName} 분석 실패: ${e.message}")
|
||||
} finally {
|
||||
analyzingStocks.remove(task.stockCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// (매일 아침 8시 30분 시스템 초기화 시 호출)
|
||||
fun clearDailyCache() {
|
||||
isSafetyBeltStockCodes.clear()
|
||||
analyzingStocks.clear()
|
||||
checkAndCleanOldIndices(50L) // 50MB 초과 시 오래된 인덱스 자동 정리
|
||||
println("🧹 [System] 일일 재무 미달 캐시 및 루씬 DB 상태 점검 완료")
|
||||
}
|
||||
@@ -350,71 +389,122 @@ object RagService {
|
||||
return@coroutineScope
|
||||
}
|
||||
|
||||
// [3단계] 뉴스 RAG 및 AI 분석
|
||||
val ragStartTime = System.currentTimeMillis()
|
||||
checkAndFetchRecentNews(stockName, stockCode)
|
||||
// 🌟 [검문소 통과!] 1단계(재무), 2단계(기술)를 모두 통과한 진짜 매수 후보 종목
|
||||
// 중복 분석 중인지 체크 후 백그라운드 AI 분석 큐(Worker)에 위임하여 메인 루프를 블로킹하지 않음
|
||||
if (!analyzingStocks.add(stockCode)) {
|
||||
println("⏳ [$stockName] 이미 백그라운드 AI 분석 대기열에서 진행 중입니다. (스킵)")
|
||||
return@coroutineScope
|
||||
}
|
||||
|
||||
val question = "$stockName 실적 및 향후 전망"
|
||||
val questionEmbedding = currentEmbeddingModel.embed(question).content()
|
||||
var finalSearchResult: EmbeddingSearchResult<TextSegment>? = null
|
||||
try {
|
||||
finalSearchResult = currentEmbeddingStore.search(
|
||||
EmbeddingSearchRequest.builder()
|
||||
.queryEmbedding(questionEmbedding)
|
||||
.filter(MetadataFilterBuilder.metadataKey("stockCode").isEqualTo(stockCode))
|
||||
.maxResults(10)
|
||||
.minScore(0.2)
|
||||
.build()
|
||||
println("🚀 [$stockName] 기술/재무 검문소 통과! 백그라운드 AI 분석 큐에 등록합니다. (메인 루프 즉시 복귀)")
|
||||
|
||||
aiTaskChannel.trySend(
|
||||
AiAnalysisTask(
|
||||
currentPrice = currentPrice,
|
||||
technicalAnalyzer = technicalAnalyzer,
|
||||
stockName = stockName,
|
||||
stockCode = stockCode,
|
||||
isWatering = isWatering,
|
||||
scores = scores,
|
||||
financialStmt = financialStmt,
|
||||
tradingDecision = tradingDecision,
|
||||
finDuration = finDuration,
|
||||
techDuration = techDuration,
|
||||
totalStartTime = totalStartTime,
|
||||
result = result
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
|
||||
val matches = finalSearchResult?.matches() ?: emptyList()
|
||||
println("\n================================================================================")
|
||||
println("🔍 [루씬 RAG 검색] [$stockName($stockCode)] 매칭된 기사 수: ${matches.size}개")
|
||||
matches.take(4).forEachIndexed { idx, match ->
|
||||
val title = match.embedded().metadata().getString("title") ?: "제목 없음"
|
||||
val date = match.embedded().metadata().getString("date") ?: ""
|
||||
val link = match.embedded().metadata().getString("link") ?: ""
|
||||
val simScore = match.score()
|
||||
println(" [$idx] (유사도: ${String.format("%.3f", simScore)}) $title | $date | $link")
|
||||
}
|
||||
println("================================================================================")
|
||||
|
||||
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
|
||||
|
||||
// [4단계] 최종 로그 기록
|
||||
val totalDuration = System.currentTimeMillis() - totalStartTime
|
||||
val detailLog = "재무(${finDuration}ms), 기술(${techDuration}ms), 뉴스/AI(${ragAiDuration}ms), 전체(${totalDuration}ms)"
|
||||
TradingLogStore.addAnalyzer(stockName, stockCode, detailLog, true)
|
||||
println("$stockName[$stockCode] $detailLog")
|
||||
result(finalDecision, true)
|
||||
)
|
||||
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
println("❌ [$stockName] 분석 실패: ${e.message}")
|
||||
println("❌ [$stockName] 1/2단계 분석 실패: ${e.message}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 백그라운드 비동기 AI 뉴스 분석 워커 (메인 루프와 완전 분리되어 30~40초 정밀 분석 수행)
|
||||
*/
|
||||
private suspend fun processAiNewsAnalysis(task: AiAnalysisTask) {
|
||||
val stockName = task.stockName
|
||||
val stockCode = task.stockCode
|
||||
val totalStartTime = task.totalStartTime
|
||||
val finDuration = task.finDuration
|
||||
val techDuration = task.techDuration
|
||||
val tradingDecision = task.tradingDecision
|
||||
val scores = task.scores
|
||||
val financialStmt = task.financialStmt
|
||||
val result = task.result
|
||||
|
||||
try {
|
||||
println("🧠 [AI 워커 가동] [$stockName] 뉴스 수집 및 심층 분석 시작...")
|
||||
|
||||
// [3단계] 뉴스 RAG 및 AI 분석
|
||||
val ragStartTime = System.currentTimeMillis()
|
||||
checkAndFetchRecentNews(stockName, stockCode)
|
||||
|
||||
val question = "$stockName 실적 및 향후 전망"
|
||||
val questionEmbedding = currentEmbeddingModel.embed(question).content()
|
||||
var finalSearchResult: EmbeddingSearchResult<TextSegment>? = null
|
||||
try {
|
||||
finalSearchResult = currentEmbeddingStore.search(
|
||||
EmbeddingSearchRequest.builder()
|
||||
.queryEmbedding(questionEmbedding)
|
||||
.filter(MetadataFilterBuilder.metadataKey("stockCode").isEqualTo(stockCode))
|
||||
.maxResults(10)
|
||||
.minScore(0.2)
|
||||
.build()
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
|
||||
val matches = finalSearchResult?.matches() ?: emptyList()
|
||||
println("\n================================================================================")
|
||||
println("🔍 [루씬 RAG 검색] [$stockName($stockCode)] 매칭된 기사 수: ${matches.size}개")
|
||||
matches.take(4).forEachIndexed { idx, match ->
|
||||
val title = match.embedded().metadata().getString("title") ?: "제목 없음"
|
||||
val date = match.embedded().metadata().getString("date") ?: ""
|
||||
val link = match.embedded().metadata().getString("link") ?: ""
|
||||
val simScore = match.score()
|
||||
println(" [$idx] (유사도: ${String.format("%.3f", simScore)}) $title | $date | $link")
|
||||
}
|
||||
println("================================================================================")
|
||||
|
||||
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
|
||||
|
||||
// [4단계] 최종 로그 기록
|
||||
val totalDuration = System.currentTimeMillis() - totalStartTime
|
||||
val detailLog = "재무(${finDuration}ms), 기술(${techDuration}ms), 뉴스/AI(${ragAiDuration}ms), 전체(${totalDuration}ms)"
|
||||
TradingLogStore.addAnalyzer(stockName, stockCode, detailLog, true)
|
||||
println("$stockName[$stockCode] $detailLog")
|
||||
|
||||
// 콜백 호출 (globalCallback 실행 -> 조건 부합 시 매수 주문 발주)
|
||||
result(finalDecision, true)
|
||||
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
println("❌ [$stockName] 백그라운드 AI 분석 실패: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun checkAndFetchRecentNews(stockName: String, stockCode: String) {
|
||||
val question = "$stockName 실적 전망 및 최근 이슈"
|
||||
val questionEmbedding = currentEmbeddingModel.embed(question).content()
|
||||
|
||||
Reference in New Issue
Block a user