...
This commit is contained in:
@@ -3,6 +3,7 @@ package network
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.call.*
|
||||
import io.ktor.client.engine.cio.*
|
||||
import io.ktor.client.plugins.HttpTimeout
|
||||
import io.ktor.client.plugins.contentnegotiation.*
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.http.*
|
||||
@@ -19,10 +20,15 @@ object AiService {
|
||||
coerceInputValues = true
|
||||
})
|
||||
}
|
||||
install(HttpTimeout) {
|
||||
requestTimeoutMillis = 60_000 // 전체 요청 대기 시간을 60초로 설정
|
||||
connectTimeoutMillis = 10_000 // 서버 연결 대기 시간 10초
|
||||
socketTimeoutMillis = 60_000 // 데이터 수신 대기 시간 60초
|
||||
}
|
||||
}
|
||||
|
||||
private const val LLM_URL = "http://localhost:8080/completion"
|
||||
|
||||
// private const val LLM_URL = "http://localhost:8080/completion"
|
||||
private const val LLM_URL = "http://127.0.0.1:8080/completion"
|
||||
/**
|
||||
* 종목명, 현재가, 실시간 체결내역을 바탕으로 AI 분석 결과를 가져옵니다.
|
||||
*/
|
||||
@@ -38,26 +44,15 @@ object AiService {
|
||||
|
||||
// Gemma에게 전달할 프롬프트 구성
|
||||
val prompt = """
|
||||
<start_of_turn>user
|
||||
당신은 20년 경력의 전문 주식 트레이더이자 데이터 분석가입니다.
|
||||
다음 데이터를 바탕으로 해당 종목의 현재 '수급 상황'과 '단기 전망'을 분석하여 3줄 이내로 핵심만 말해주세요.
|
||||
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
|
||||
당신은 20년 경력의 주식 트레이더입니다. 데이터를 분석하여 짧고 단호하게 조언합니다.<|eot_id|><|start_header_id|>user<|end_header_id|>
|
||||
다음 데이터를 분석하여 '수급 상황'과 '단기 전망'을 3줄 이내로 요약하세요.
|
||||
|
||||
[종목 정보]
|
||||
- 종목명: $stockName
|
||||
- 현재가: $currentPrice
|
||||
|
||||
[최근 실시간 체결 내역]
|
||||
$tradeSummary
|
||||
|
||||
분석 기준:
|
||||
1. 매수 체결 비중이 높은지, 매도 체결 비중이 높은지 판단하세요.
|
||||
2. 대량 체결(고래)의 움직임이 있는지 확인하세요.
|
||||
3. 단기적으로 진입하기에 적절한 시점인지 조언하세요.
|
||||
|
||||
답변은 한국어로, 친절하지만 단호한 전문가 말투를 사용하세요.<end_of_turn>
|
||||
<start_of_turn>model
|
||||
|
||||
""".trimIndent()
|
||||
[종목] $stockName ($currentPrice)
|
||||
[최근 체결]
|
||||
$tradeSummary
|
||||
<|eot_id|><|start_header_id|>assistant<|end_header_id|>
|
||||
""".trimIndent()
|
||||
|
||||
return try {
|
||||
val response = client.post(LLM_URL) {
|
||||
@@ -72,20 +67,53 @@ object AiService {
|
||||
"AI 서버 응답 오류: ${response.status}"
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
"분석 실패: 로컬 AI 서버(llama.cpp)가 실행 중인지 확인하세요. (${e.message})"
|
||||
var msg = "분석 실패: 로컬 AI 서버(llama.cpp)가 실행 중인지 확인하세요. (${e.message})"
|
||||
println(msg)
|
||||
msg
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
suspend fun getEmbedding(text: String): List<Double>? {
|
||||
return try {
|
||||
val response = client.post("http://127.0.0.1:8080/embedding") {
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(EmbeddingRequest(content = text))
|
||||
}
|
||||
if (response.status == HttpStatusCode.OK) {
|
||||
val res: EmbeddingResponse = response.body()
|
||||
res.embedding
|
||||
} else null
|
||||
} catch (e: Exception) {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Serializable
|
||||
data class EmbeddingRequest(val content: String)
|
||||
|
||||
@Serializable
|
||||
data class EmbeddingResponse(val embedding: List<Double>)
|
||||
|
||||
/**
|
||||
* llama.cpp 서버 요청 데이터 구조
|
||||
*/
|
||||
@Serializable
|
||||
data class LlamaRequest(
|
||||
val prompt: String,
|
||||
val n_predict: Int = 256,
|
||||
val temperature: Double = 0.7,
|
||||
val stop: List<String> = listOf("<|end_of_turn|>", "<end_of_turn>")
|
||||
val n_predict: Int = 256, // 답변 길이를 엄격히 제한
|
||||
val temperature: Double = 0.4, // M3 Pro에서 더 일관된 답변을 위해 낮춤
|
||||
val stop: List<String> = listOf(
|
||||
"<|eot_id|>",
|
||||
"<|end_of_text|>",
|
||||
"<|start_header_id|>",
|
||||
"user",
|
||||
"model"
|
||||
) // [중요] AI가 멈춰야 할 지점들을 명확히 지정
|
||||
)
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,50 +4,64 @@ import java.io.File
|
||||
import java.io.BufferedReader
|
||||
import java.io.InputStreamReader
|
||||
import kotlinx.coroutines.*
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
|
||||
object LlamaServerManager {
|
||||
private var process: Process? = null
|
||||
private val scope = CoroutineScope(Dispatchers.IO + Job())
|
||||
// 포트별로 프로세스를 관리합니다.
|
||||
private val processes = ConcurrentHashMap<Int, Process>()
|
||||
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
init {
|
||||
Runtime.getRuntime().addShutdownHook(Thread {
|
||||
stopAll()
|
||||
})
|
||||
}
|
||||
|
||||
fun startServer(binPath: String, modelPath: String) {
|
||||
if (process != null || modelPath.isNullOrBlank()) return // 이미 실행 중이면 무시
|
||||
fun startServer(binPath: String, modelPath: String, port: Int, nGpuLayers: Int = 99) {
|
||||
// 이미 해당 포트에서 실행 중이거나 모델 경로가 비었으면 무시합니다.
|
||||
if (processes.containsKey(port) || modelPath.isBlank()) return
|
||||
|
||||
val command = listOf(
|
||||
binPath,
|
||||
"-m", modelPath,
|
||||
"--port", "8080",
|
||||
"-c", "2048", // 컨텍스트 길이
|
||||
"-t", "4", // 인텔 맥 코어 수에 맞춰 스레드 제한 (부하 방지)
|
||||
"--embedding" // 나중에 유사도 분석 등을 위해 활성화
|
||||
"--port", port.toString(),
|
||||
"-c", if (port == 8081) "512" else "4096", // 임베딩용은 컨텍스트가 짧아도 충분합니다.
|
||||
"-ngl", nGpuLayers.toString(),
|
||||
"-t", "6", // M3 Pro의 성능 코어를 고려하여 6~8개 권장
|
||||
"--embedding" // 임베딩 기능을 활성화합니다.
|
||||
)
|
||||
|
||||
scope.launch {
|
||||
try {
|
||||
val pb = ProcessBuilder(command)
|
||||
// 실행 파일 권한 확인 (자동 부여)
|
||||
|
||||
pb.redirectErrorStream(true)
|
||||
File(binPath).setExecutable(true)
|
||||
|
||||
process = pb.start()
|
||||
println("✅ AI 서버 시작됨: http://localhost:8080")
|
||||
val process = pb.start()
|
||||
processes[port] = process
|
||||
println("✅ AI 서버 시작 시도 (Port: $port, Model: ${File(modelPath).name})")
|
||||
|
||||
// 서버 로그 모니터링 (에러 디버깅용)
|
||||
val reader = BufferedReader(InputStreamReader(process?.inputStream))
|
||||
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 모델 로딩 완료 및 대기 중")
|
||||
// 로그 출력 (디버깅용)
|
||||
println("[Server $port] $line")
|
||||
if (line?.contains("server is listening") == true) {
|
||||
println("🚀 AI 서버 준비 완료 (Port: $port)")
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println("❌ AI 서버 실행 실패: ${e.message}")
|
||||
println("❌ AI 서버 실행 실패 (Port: $port): ${e.message}")
|
||||
processes.remove(port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun stopServer() {
|
||||
process?.destroy()
|
||||
process = null
|
||||
println("🛑 AI 서버 종료")
|
||||
fun stopAll() {
|
||||
processes.forEach { (port, process) ->
|
||||
process.destroy()
|
||||
println("🛑 AI 서버 종료 (Port: $port)")
|
||||
}
|
||||
processes.clear()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
// src/main/kotlin/network/RagService.kt
|
||||
|
||||
import dev.langchain4j.data.segment.TextSegment
|
||||
import dev.langchain4j.model.openai.OpenAiChatModel
|
||||
import dev.langchain4j.model.openai.OpenAiEmbeddingModel
|
||||
import org.jetbrains.exposed.sql.*
|
||||
import org.jetbrains.exposed.sql.SqlExpressionBuilder.plus
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
import java.time.Duration
|
||||
|
||||
object RagService {
|
||||
// 임베딩 모델 (8081) 및 채팅 모델 (8080) 설정
|
||||
private val embeddingModel = OpenAiEmbeddingModel.builder()
|
||||
.baseUrl("http://127.0.0.1:8081/v1")
|
||||
.apiKey("unused")
|
||||
.build()
|
||||
|
||||
private val chatModel = OpenAiChatModel.builder()
|
||||
.baseUrl("http://127.0.0.1:8080/v1")
|
||||
.apiKey("unused")
|
||||
.timeout(Duration.ofSeconds(60))
|
||||
.build()
|
||||
|
||||
/**
|
||||
* 텍스트를 임베딩하여 H2 DB에 저장합니다.
|
||||
*/
|
||||
fun ingest(text: String, meta: String = "") {
|
||||
val embedding = embeddingModel.embed(text).content().vector()
|
||||
|
||||
transaction {
|
||||
VectorStoreTable.insert {
|
||||
it[content] = text
|
||||
it[metadata] = meta
|
||||
// 벡터 데이터를 문자열 형태로 저장 (H2 포맷)
|
||||
it[VectorStoreTable.embedding] = embedding.joinToString(",", "[", "]")
|
||||
}
|
||||
}
|
||||
println("💾 H2 벡터 저장 완료: ${text.take(15)}...")
|
||||
}
|
||||
|
||||
/**
|
||||
* 질문과 가장 유사한 정보를 H2에서 검색하여 AI 답변을 생성합니다.
|
||||
*/
|
||||
fun ask(question: String): String {
|
||||
val queryVector = embeddingModel.embed(question).content().vector()
|
||||
val vectorStr = queryVector.joinToString(",", "[", "]")
|
||||
|
||||
// H2의 VECTOR_COSINE_SIMILARITY 함수를 사용하여 검색
|
||||
val context = transaction {
|
||||
val query = "SELECT content FROM VECTOR_STORE " +
|
||||
"ORDER BY VECTOR_COSINE_SIMILARITY(embedding, '$vectorStr') DESC " +
|
||||
"LIMIT 3"
|
||||
|
||||
val results = mutableListOf<String>()
|
||||
exec(query) { rs ->
|
||||
while (rs.next()) {
|
||||
results.add(rs.getString("content"))
|
||||
}
|
||||
}
|
||||
results.joinToString("\n\n")
|
||||
}
|
||||
|
||||
val prompt = """
|
||||
[참고 정보]
|
||||
$context
|
||||
|
||||
[질문]
|
||||
$question
|
||||
|
||||
위 정보를 참고하여 분석 결과를 말해주세요.
|
||||
""".trimIndent()
|
||||
|
||||
return chatModel.generate(prompt)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user