atrade/src/main/kotlin/network/LlamaServerManager.kt

53 lines
1.8 KiB
Kotlin
Raw Normal View History

2026-01-10 18:16:50 +09:00
package network
import java.io.File
import java.io.BufferedReader
import java.io.InputStreamReader
import kotlinx.coroutines.*
object LlamaServerManager {
private var process: Process? = null
private val scope = CoroutineScope(Dispatchers.IO + Job())
fun startServer(binPath: String, modelPath: String) {
2026-01-13 16:04:25 +09:00
if (process != null || modelPath.isNullOrBlank()) return // 이미 실행 중이면 무시
2026-01-10 18:16:50 +09:00
val command = listOf(
binPath,
"-m", modelPath,
"--port", "8080",
"-c", "2048", // 컨텍스트 길이
"-t", "4", // 인텔 맥 코어 수에 맞춰 스레드 제한 (부하 방지)
"--embedding" // 나중에 유사도 분석 등을 위해 활성화
)
scope.launch {
try {
val pb = ProcessBuilder(command)
// 실행 파일 권한 확인 (자동 부여)
File(binPath).setExecutable(true)
process = pb.start()
println("✅ AI 서버 시작됨: http://localhost:8080")
// 서버 로그 모니터링 (에러 디버깅용)
val reader = BufferedReader(InputStreamReader(process?.inputStream))
var line: String?
while (reader.readLine().also { line = it } != null) {
// 서버 준비 완료 로그 확인용
if (line?.contains("HTTP server listening") == true) {
println("🚀 AI 모델 로딩 완료 및 대기 중")
}
}
} catch (e: Exception) {
println("❌ AI 서버 실행 실패: ${e.message}")
}
}
}
fun stopServer() {
process?.destroy()
process = null
println("🛑 AI 서버 종료")
}
}