feat(rag): support hybrid Ollama/local embedding routing with Lucene DB maintenance

- Added automatic Ollama embedding model discovery in LlamaServerManager (bge, nomic, embed, minilm)
- Implemented dynamic routing in RagService for embeddings (Ollama 11434 vs local llama-server 8081)
- Added embedding model consistency check with embed_model.tag to prevent Lucene vector dimension mismatches
- Added Lucene store maintenance functions: getLuceneStats, resetLuceneStore, and automatic 50MB retention cleanup
- Added real-time Lucene status display and one-click '벡터 DB 초기화' button to TradeConfigPanel
- Verified with compileKotlin and full project build
This commit is contained in:
2026-11-17 16:08:23 +09:00
parent e2c2dca278
commit 5514356c53
4 changed files with 350 additions and 36 deletions
+199 -23
View File
@@ -52,18 +52,43 @@ import java.util.concurrent.TimeUnit
object RagService {
val isSafetyBeltStockCodes = ConcurrentHashMap.newKeySet<String>()
// (매일 아침 8시 30분 시스템 초기화 시 호출해주어야 함)
// (매일 아침 8시 30분 시스템 초기화 시 호출)
fun clearDailyCache() {
isSafetyBeltStockCodes.clear()
println("🧹 [System] 일일 재무 미달 캐시 초기화 완료")
checkAndCleanOldIndices(50L) // 50MB 초과 시 오래된 인덱스 자동 정리
println("🧹 [System] 일일 재무 미달 캐시 및 루씬 DB 상태 점검 완료")
}
// 임베딩 모델은 포트 할당 완료 후 지연 초기화되도록 by lazy 적용
private val embeddingModel by lazy {
OpenAiEmbeddingModel.builder()
.baseUrl("http://127.0.0.1:${EMBEDDING_PORT}/v1")
.apiKey("unused")
.build()
@Volatile
private var _embeddingModel: dev.langchain4j.model.embedding.EmbeddingModel? = null
// 임베딩 모델: Ollama 임베딩 활성 시 Ollama(11434)로, 아니면 내장 llama-server(8081)로 자동 분기
val currentEmbeddingModel: dev.langchain4j.model.embedding.EmbeddingModel
get() {
_embeddingModel?.let { return it }
return synchronized(this) {
_embeddingModel ?: run {
val port = LlamaServerManager.activeEmbeddingPort
val modelName = if (LlamaServerManager.isOllamaEmbeddingActive) {
LlamaServerManager.ollamaEmbeddingModel.ifBlank { "bge-m3:latest" }
} else {
"local-model"
}
val model = OpenAiEmbeddingModel.builder()
.baseUrl("http://127.0.0.1:$port/v1")
.modelName(modelName)
.apiKey("unused")
.timeout(Duration.ofSeconds(30))
.build()
_embeddingModel = model
println("🤖 [RagService] 임베딩 모델 바인딩 완료 (Port: $port, Model: $modelName)")
model
}
}
}
fun invalidateEmbeddingModel() {
_embeddingModel = null
}
// 채팅 모델은 LlamaServerManager의 활성 포트(Ollama 11434 또는 기존 LLM_PORT)를 지연 바인딩
@@ -78,18 +103,30 @@ object RagService {
.build()
}
private val embeddingStore: LuceneEmbeddingStore by lazy {
val path = Paths.get("db/lucene_idx")
val directory = MMapDirectory(path)
@Volatile
private var _embeddingStore: LuceneEmbeddingStore? = null
LuceneEmbeddingStore.builder()
.directory(directory)
.build()
}
val currentEmbeddingStore: LuceneEmbeddingStore
get() {
_embeddingStore?.let { return it }
return synchronized(this) {
_embeddingStore ?: run {
val path = Paths.get("db/lucene_idx")
java.nio.file.Files.createDirectories(path)
checkEmbeddingModelConsistency()
val directory = MMapDirectory(path)
val store = LuceneEmbeddingStore.builder()
.directory(directory)
.build()
_embeddingStore = store
store
}
}
}
fun active() {
if (UrlCacheManager.isInitialized()) return
UrlCacheManager.initialize(embeddingStore, embeddingModel)
UrlCacheManager.initialize(currentEmbeddingStore, currentEmbeddingModel)
}
/**
@@ -140,8 +177,8 @@ object RagService {
metadata.put("stockCode", stockCode)
val segment = TextSegment.from(chunk, metadata)
val embedding = embeddingModel.embed(segment).content()
embeddingStore.add(embedding, segment)
val embedding = currentEmbeddingModel.embed(segment).content()
currentEmbeddingStore.add(embedding, segment)
}
}
}
@@ -265,10 +302,10 @@ object RagService {
checkAndFetchRecentNews(stockName, stockCode)
val question = "$stockName 실적 및 향후 전망"
val questionEmbedding = embeddingModel.embed(question).content()
val questionEmbedding = currentEmbeddingModel.embed(question).content()
var finalSearchResult: EmbeddingSearchResult<TextSegment>? = null
try {
finalSearchResult = embeddingStore.search(
finalSearchResult = currentEmbeddingStore.search(
EmbeddingSearchRequest.builder()
.queryEmbedding(questionEmbedding)
.filter(MetadataFilterBuilder.metadataKey("stockCode").isEqualTo(stockCode))
@@ -304,11 +341,11 @@ object RagService {
private suspend fun checkAndFetchRecentNews(stockName: String, stockCode: String) {
val question = "$stockName 실적 전망 및 최근 이슈"
val questionEmbedding = embeddingModel.embed(question).content()
val questionEmbedding = currentEmbeddingModel.embed(question).content()
var searchResult: EmbeddingSearchResult<TextSegment>? = null
try {
searchResult = embeddingStore.search(
searchResult = currentEmbeddingStore.search(
EmbeddingSearchRequest.builder()
.queryEmbedding(questionEmbedding)
.filter(MetadataFilterBuilder.metadataKey("stockCode").isEqualTo(stockCode))
@@ -350,7 +387,7 @@ object RagService {
.maxResults(1)
.build()
val result = embeddingStore.search(searchRequest)
val result = currentEmbeddingStore.search(searchRequest)
return result.matches().isNotEmpty()
}
@@ -402,6 +439,145 @@ object RagService {
}
}
/**
* UI 표시용 활성 RAG 임베딩 모델명을 반환합니다.
*/
fun getDisplayEmbeddingModelName(): String {
return if (LlamaServerManager.isOllamaEmbeddingActive) {
"[Ollama] ${LlamaServerManager.ollamaEmbeddingModel}"
} else if (KisSession.config.embedModelPath.isNotBlank()) {
val fileName = java.io.File(KisSession.config.embedModelPath).name
"[내장] $fileName"
} else {
"[내장] bge-m3"
}
}
/**
* 루씬 벡터 DB 통계 조회 (색인 기사 수, 디스크 파일 크기)
*/
fun getLuceneStats(): Pair<Int, String> {
val count = service.UrlCacheManager.getProcessedCount()
val idxPath = Paths.get("db/lucene_idx")
val sizeBytes = if (java.nio.file.Files.exists(idxPath)) {
try {
java.nio.file.Files.walk(idxPath)
.filter { java.nio.file.Files.isRegularFile(it) }
.mapToLong { java.nio.file.Files.size(it) }
.sum()
} catch (e: Exception) {
0L
}
} else 0L
val formattedSize = when {
sizeBytes >= 1024 * 1024 -> String.format(Locale.KOREA, "%.1f MB", sizeBytes / (1024.0 * 1024.0))
sizeBytes >= 1024 -> String.format(Locale.KOREA, "%.1f KB", sizeBytes / 1024.0)
else -> "$sizeBytes B"
}
return count to formattedSize
}
/**
* 루씬 벡터 DB 완전 초기화 (메모리 캐시 및 디스크 인덱스 삭제 후 재생성)
*/
fun resetLuceneStore(): Boolean {
return try {
synchronized(this) {
_embeddingStore = null
service.UrlCacheManager.clear()
val idxPath = Paths.get("db/lucene_idx")
if (java.nio.file.Files.exists(idxPath)) {
java.nio.file.Files.walk(idxPath)
.sorted(Comparator.reverseOrder())
.forEach { path ->
try {
if (path != idxPath) {
java.nio.file.Files.deleteIfExists(path)
}
} catch (e: Exception) {
// 잠긴 파일 예외 처리
}
}
}
java.nio.file.Files.createDirectories(idxPath)
recordCurrentEmbeddingTag()
val directory = MMapDirectory(idxPath)
val newStore = LuceneEmbeddingStore.builder().directory(directory).build()
_embeddingStore = newStore
println("🧹 [RagService] 루씬 벡터 DB 완전 초기화 성공")
true
}
} catch (e: Exception) {
println("❌ [RagService] 루씬 초기화 실패: ${e.message}")
false
}
}
/**
* 현재 사용 중인 임베딩 모델 식별자 (올라마 vs 내장)
*/
private fun getCurrentEmbeddingTag(): String {
return if (LlamaServerManager.isOllamaEmbeddingActive) {
"ollama:${LlamaServerManager.ollamaEmbeddingModel}"
} else if (KisSession.config.embedModelPath.isNotBlank()) {
"local:${java.io.File(KisSession.config.embedModelPath).name}"
} else {
"local:default"
}
}
/**
* 임베딩 모델 변경 감지 (차원 불일치 방지 및 자동 리셋)
*/
private fun checkEmbeddingModelConsistency() {
try {
val tagFile = Paths.get("db/lucene_idx/embed_model.tag")
val currentTag = getCurrentEmbeddingTag()
if (java.nio.file.Files.exists(tagFile)) {
val savedTag = java.nio.file.Files.readString(tagFile).trim()
if (savedTag.isNotBlank() && savedTag != currentTag) {
println("🔄 [RagService] 임베딩 모델 변경 감지 ($savedTag -> $currentTag). 벡터 차원 불일치 방지를 위해 루씬 인덱스를 재색인합니다.")
resetLuceneStore()
return
}
}
recordCurrentEmbeddingTag()
} catch (e: Exception) {
// 무시
}
}
private fun recordCurrentEmbeddingTag() {
try {
val tagFile = Paths.get("db/lucene_idx/embed_model.tag")
java.nio.file.Files.writeString(tagFile, getCurrentEmbeddingTag())
} catch (e: Exception) {
// 무시
}
}
/**
* 일정 크기(기본 50MB) 초과 시 루씬 인덱스 자동 정리
*/
fun checkAndCleanOldIndices(maxMegabytes: Long = 50L) {
val (count, sizeStr) = getLuceneStats()
val idxPath = Paths.get("db/lucene_idx")
val sizeBytes = if (java.nio.file.Files.exists(idxPath)) {
try {
java.nio.file.Files.walk(idxPath)
.filter { java.nio.file.Files.isRegularFile(it) }
.mapToLong { java.nio.file.Files.size(it) }
.sum()
} catch (e: Exception) { 0L }
} else 0L
if (sizeBytes > maxMegabytes * 1024 * 1024) {
println("🧹 [RagService] 루씬 DB 용량($sizeStr)이 임계치(${maxMegabytes}MB)를 초과하여 자동 정리합니다.")
resetLuceneStore()
}
}
private suspend fun callLlamaWithSchema(prompt: String): String {
val jsonMediaType = "application/json; charset=utf-8".toMediaType()
val activeModel = resolveActiveModelName()
@@ -8,6 +8,10 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import network.RagService
import ui.store.TradingLogStore
import util.HardwareDetector
@@ -28,9 +32,20 @@ object LlamaServerManager {
var isOllamaActive: Boolean = false
private set
@Volatile
var isOllamaEmbeddingActive: Boolean = false
private set
@Volatile
var ollamaEmbeddingModel: String = ""
private set
val activeLlmPort: Int
get() = if (isOllamaActive) 11434 else LLM_PORT
val activeEmbeddingPort: Int
get() = if (isOllamaEmbeddingActive) 11434 else EMBEDDING_PORT
init {
Runtime.getRuntime().addShutdownHook(Thread {
stopAll()
@@ -54,6 +69,40 @@ object LlamaServerManager {
}
}
// Ollama에 설치된 임베딩 전용 모델을 자동 탐색 (bge, embed, nomic, minilm 키워드 매칭)
fun findOllamaEmbeddingModel(ollamaPort: Int = 11434): String? {
return try {
val url = URL("http://127.0.0.1:$ollamaPort/api/tags")
val conn = (url.openConnection() as HttpURLConnection).apply {
requestMethod = "GET"
connectTimeout = 1000
readTimeout = 1000
}
val responseText = conn.inputStream.bufferedReader().readText()
conn.disconnect()
val json = Json.parseToJsonElement(responseText).jsonObject
val models = json["models"]?.jsonArray ?: return null
// 1순위: bge 계열 (다국어/한국어 고성능 bge-m3 등)
val bgeModel = models.firstOrNull { model ->
val name = model.jsonObject["name"]?.jsonPrimitive?.content?.lowercase() ?: ""
name.contains("bge")
}?.jsonObject?.get("name")?.jsonPrimitive?.content
if (bgeModel != null) return bgeModel
// 2순위: 기타 임베딩 모델 (embed, nomic, minilm)
val embedModel = models.firstOrNull { model ->
val name = model.jsonObject["name"]?.jsonPrimitive?.content?.lowercase() ?: ""
name.contains("embed") || name.contains("nomic") || name.contains("minilm")
}?.jsonObject?.get("name")?.jsonPrimitive?.content
embedModel
} catch (e: Exception) {
null
}
}
// 특정 포트를 점유하고 있는 프로세스만 선별적으로 종료 (다른 사용자의 llama-server 프로세스 보호)
fun killProcessOnPort(port: Int) {
try {
@@ -118,6 +167,26 @@ object LlamaServerManager {
}
}
// 임베딩 서버 기동 요청 시 외부 Ollama(11434)에 임베딩 모델이 있다면 공유 모드로 우회
if (port == EMBEDDING_PORT) {
if (checkOllamaRunning(11434)) {
val embedModel = findOllamaEmbeddingModel(11434)
if (embedModel != null) {
isOllamaEmbeddingActive = true
ollamaEmbeddingModel = embedModel
println("🔗 [System] 로컬 Ollama(11434) 임베딩 모델($embedModel) 감지 -> 내장 임베딩 서버 대신 Ollama를 공유합니다.")
AutoTradingManager.llmNews = true
RagService.active()
return
} else {
isOllamaEmbeddingActive = false
println("ℹ️ [System] Ollama에 임베딩 모델(bge/nomic/embed 등) 미보유 -> 내장 llama-server(Port: $port)를 기동합니다.")
}
} else {
isOllamaEmbeddingActive = false
}
}
val os = System.getProperty("os.name").lowercase()
val arch = System.getProperty("os.arch").lowercase()
val isWin = os.contains("win")
@@ -52,4 +52,17 @@ object UrlCacheManager {
fun addToCache(url: String) {
processedUrls.add(url)
}
/**
* 현재 캐시된 URL 수
*/
fun getProcessedCount(): Int = processedUrls.size
/**
* 캐시 완전 초기화
*/
fun clear() {
processedUrls.clear()
println("🧹 [UrlCacheManager] URL 캐시 초기화 완료")
}
}
+69 -13
View File
@@ -750,14 +750,27 @@ private fun ScheduleFilterTabContent(tradeConfig: TradeConfig) {
shape = RoundedCornerShape(8.dp),
border = BorderStroke(1.dp, Color(0xFFE2E8F0))
) {
var luceneStats by remember { mutableStateOf(RagService.getLuceneStats()) }
Column(modifier = Modifier.padding(12.dp)) {
Text(
"🤖 AI 분석 엔진 및 모델 현황",
fontSize = 12.sp,
fontWeight = FontWeight.Bold,
color = Color(0xFF1E293B),
modifier = Modifier.padding(bottom = 8.dp)
)
Row(
modifier = Modifier.fillMaxWidth().padding(bottom = 8.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Text(
"🤖 AI 분석 엔진 및 모델 현황",
fontSize = 12.sp,
fontWeight = FontWeight.Bold,
color = Color(0xFF1E293B)
)
Text(
"하이브리드 자동 분기 활성화",
fontSize = 10.sp,
color = Color(0xFF10B981),
fontWeight = FontWeight.SemiBold
)
}
Row(
modifier = Modifier.fillMaxWidth(),
@@ -794,13 +807,8 @@ private fun ScheduleFilterTabContent(tradeConfig: TradeConfig) {
Column(modifier = Modifier.padding(8.dp)) {
Text("뉴스/공시 RAG 임베딩", fontSize = 10.sp, color = Color(0xFF64748B))
Spacer(Modifier.height(2.dp))
val embedName = if (KisSession.config.embedModelPath.isNotBlank()) {
java.io.File(KisSession.config.embedModelPath).name
} else {
"bge-m3 (내장 임베딩)"
}
Text(
embedName,
RagService.getDisplayEmbeddingModelName(),
fontSize = 11.sp,
fontWeight = FontWeight.Bold,
color = Color(0xFF0F172A),
@@ -810,6 +818,54 @@ private fun ScheduleFilterTabContent(tradeConfig: TradeConfig) {
}
}
}
Spacer(Modifier.height(8.dp))
// 루씬 벡터 DB 실시간 상태 및 관리 버튼
Surface(
modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(6.dp),
color = Color(0xFFF1F5F9),
border = BorderStroke(1.dp, Color(0xFFE2E8F0))
) {
Row(
modifier = Modifier.padding(horizontal = 10.dp, vertical = 6.dp),
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(6.dp)
) {
Text("🗄️", fontSize = 12.sp)
Text(
"루씬 벡터 DB:",
fontSize = 11.sp,
fontWeight = FontWeight.SemiBold,
color = Color(0xFF334155)
)
Text(
"기사 ${luceneStats.first}건 (${luceneStats.second})",
fontSize = 11.sp,
fontWeight = FontWeight.Bold,
color = Color(0xFF4F46E5)
)
}
OutlinedButton(
onClick = {
RagService.resetLuceneStore()
luceneStats = RagService.getLuceneStats()
},
contentPadding = PaddingValues(horizontal = 8.dp, vertical = 2.dp),
modifier = Modifier.height(26.dp),
shape = RoundedCornerShape(4.dp),
border = BorderStroke(1.dp, Color(0xFFCBD5E1))
) {
Text("🧹 벡터 DB 초기화", fontSize = 10.sp, color = Color(0xFF475569), fontWeight = FontWeight.Bold)
}
}
}
}
}
}