diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/LunApplication.kt b/src/main/kotlin/kr/lunaticbum/back/lun/LunApplication.kt index eecad44..2c799bb 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/LunApplication.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/LunApplication.kt @@ -2,6 +2,7 @@ package kr.lunaticbum.back.lun import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication +import org.springframework.scheduling.annotation.EnableAsync import org.springframework.scheduling.annotation.EnableScheduling import org.springframework.web.reactive.function.client.WebClient import java.io.IOException @@ -9,7 +10,7 @@ import java.util.concurrent.ExecutionException import java.util.concurrent.TimeUnit import java.util.concurrent.TimeoutException - +@EnableAsync @EnableScheduling // 추가 @SpringBootApplication class LunApplication { diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt b/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt index 70d88be..71c4208 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/configs/AppConfig.kt @@ -36,17 +36,17 @@ class AppConfig : WebMvcConfigurer { // return QdrantClient("https://ollama.lunaticbum.kr:6334") // } - @Bean - fun chatClient(): OllamaApi { - return OllamaApi("https://lama.lunaticbum.kr") - -// .withDefaultOptions( -// OllamaOptions.create() -// .withModel("phi4:14b") -// .withNumThread(5) -// .withSeed(5) -// .withTemperature(0.9f)) - } +// @Bean +// fun chatClient(): OllamaApi { +// return OllamaApi("https://lama.lunaticbum.kr") +// +//// .withDefaultOptions( +//// OllamaOptions.create() +//// .withModel("phi4:14b") +//// .withNumThread(5) +//// .withSeed(5) +//// .withTemperature(0.9f)) +// } // @Bean // fun getProperty() : Map{ // println("telegramBotKey >>>> $telegramBotKey") diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/configs/AsyncConfig.kt b/src/main/kotlin/kr/lunaticbum/back/lun/configs/AsyncConfig.kt new file mode 100644 index 0000000..a07b849 --- /dev/null +++ b/src/main/kotlin/kr/lunaticbum/back/lun/configs/AsyncConfig.kt @@ -0,0 +1,24 @@ +package kr.lunaticbum.back.lun.configs + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor + + +@Configuration +class AsyncConfig { + @Bean + fun taskExecutor(): ThreadPoolTaskExecutor { + val executor = ThreadPoolTaskExecutor() + executor.corePoolSize = 10 // 코어 스레드 풀 크기 설정 + executor.maxPoolSize = 20 // 최대 스레드 풀 크기 설정 + executor.queueCapacity = 500 // 작업 큐 용량 설정 + executor.setThreadNamePrefix("AsyncThread-") // 스레드 이름 접두사 설정 + + // 작업이 완료된 후 스레드 풀이 종료될 때까지 대기할 시간 설정 (단위: 초) + executor.setAwaitTerminationSeconds(60) + + executor.initialize() + return executor + } +} \ No newline at end of file diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/configs/BumsInterceptor.kt b/src/main/kotlin/kr/lunaticbum/back/lun/configs/BumsInterceptor.kt index bf3b43e..beb4069 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/configs/BumsInterceptor.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/configs/BumsInterceptor.kt @@ -28,11 +28,11 @@ class BumsInterceptor : HandlerInterceptor { @Throws(Exception::class) override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { var skippResourcesExtension = arrayListOf(".js",".css").filter { request.requestURI.contains(it) }.size > 0 - if (!skippResourcesExtension) { - println("===============================================") - println("==================== BEGIN ====================") - println("Request URL ===> " + request.requestURL) - } +// if (!skippResourcesExtension) { +// println("===============================================") +// println("==================== BEGIN ====================") +// println("Request URL ===> " + request.requestURL) +// } return super.preHandle(request, response, handler) } @@ -44,7 +44,7 @@ class BumsInterceptor : HandlerInterceptor { handler: Any, @Nullable modelAndView: ModelAndView? ) { - var skippResourcesExtension = arrayListOf(".js",".css").filter { request.requestURI.contains(it) }.size > 0 + var skippResourcesExtension = arrayListOf(".ajax",".js",".css").filter { request.requestURI.contains(it) }.size > 0 if (!skippResourcesExtension) { if (request.requestURI.contains("logout") == false && !request.cookies.isNullOrEmpty() && request.cookies.filter { it.name.equals( @@ -60,6 +60,7 @@ class BumsInterceptor : HandlerInterceptor { access = it accessOk = true println("==================== accessOk ${accessOk} ======================") + } } request.cookies.forEach { @@ -70,44 +71,41 @@ class BumsInterceptor : HandlerInterceptor { } } if (refreshOk || accessOk) { - if (refreshOk) { - if (!accessOk) { - refresh?.let { refresh -> - jwtService.getUserIdFromRefresh(refresh.value)?.let { userId -> - userManager.findById(userId)?.block()?.let { user -> - jwtService.generate(user)?.let { token -> - response.addCookie(cookieUpdate(Cookie("access", token.tokenKey))) - response.addCookie(cookieUpdate(Cookie("refresh", token.refreshToken))) - } - } - } - } - } else { - response.addCookie(cookieUpdate(access)) - } - } else if (accessOk) { - response.addCookie(cookieUpdate(access)) + request.getSession(true)?.let { session -> + session.setAttribute(WRITE_PERMISSION_KEY, true) + session.maxInactiveInterval = 60 * 5 } - modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY, "OK") - modelAndView?.modelMap?.put("user_id", jwtService.getUserIdFromToken(access?.value ?: "")) } else { - println("==================== accessOk ${accessOk} && refreshOk ${refreshOk} ======================") - response.addCookie(Cookie("access", "").apply { maxAge = -1 }) - response.addCookie(Cookie("refresh", "").apply { maxAge = -1 }) - modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY, "NO") - modelAndView?.modelMap?.put("user_id", "") - } - println("Response modelMap ===> ${Gson().toJson(modelAndView?.modelMap)}") - } else if (request.requestURI.contains("logout")) { - modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY, "NO") - modelAndView?.modelMap?.put("user_id", "") - } + } + } else if (request.requestURI.contains("logout")) { + request.getSession(true)?.let { session -> + session.invalidate() + session.setAttribute(WRITE_PERMISSION_KEY, false) + } + } println("==================== END ======================") println("===============================================") } + request.cookies?.forEach { + if (it.name.equals("CLEAR", true)) { + request.getSession(true)?.let { session -> + session.invalidate() + session.setAttribute(WRITE_PERMISSION_KEY, false) + } + } + } + modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY,"NO") + request.getSession(true)?.let { + (it.getAttribute(WRITE_PERMISSION_KEY) as? Boolean)?.let { permission -> + if (permission) { + modelAndView?.modelMap?.put(WRITE_PERMISSION_KEY,"OK") + } + } + } super.postHandle(request, response, handler, modelAndView) } + fun cookieUpdate(cookie: Cookie?) : Cookie? { cookie?.maxAge = (globalEvv.ACCESS_EXPIRATION / 1000).toInt() cookie?.domain = "lunaticbum.kr" diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt index 0b898f4..af4e1db 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt @@ -143,39 +143,34 @@ class BlogController() { fun modify(httpServletRequest: HttpServletRequest, @RequestParam("token") token : String?) : ResultMV{ logService.log("incoming modify") val vm = ResultMV("content/blog/modify") - var s33Key : String? = null - if(jwtService.hasPerrmission(httpServletRequest)) { - postManageg.find20()?.apply { - forEach { - it.title = URLDecoder.decode(it.title) - val content = URLDecoder.decode(it.content) - it.content = if (content.length > 50) content.substring(0,150) else content + vm.modelMap.put(WRITE_PERMISSION_KEY,"NO") + httpServletRequest.getSession(true)?.let { session -> + (session.getAttribute(WRITE_PERMISSION_KEY) as? Boolean)?.let { + postManageg.find20()?.apply { + forEach { + it.title = URLDecoder.decode(it.title) + val content = URLDecoder.decode(it.content) + it.content = if (content.length > 50) content.substring(0,150) else content + } + vm.modelMap.put("chunkedPosts", this.chunked(3)) } - vm.modelMap.put("chunkedPosts", this.chunked(3)) + vm.modelMap.put(WRITE_PERMISSION_KEY,"OK") + vm.modelMap.put("path","editor/") + vm.modelMap.put("SK",token) } - vm.modelMap.put(WRITE_PERMISSION_KEY,"OK") - vm.modelMap.put("path","editor/") - vm.modelMap.put("SK",token) - } else { - vm.modelMap.put(WRITE_PERMISSION_KEY,"NO") } vm.modelMap.put("rowKey","chunkedPosts_") return vm } @GetMapping("editor/{postId}") - fun editor(@PathVariable postId : String, @RequestParam("token") token : String?) : ResultMV{ + fun editor(@PathVariable postId : String) : ResultMV{ val vm = ResultMV("content/blog/editor") postManageg.getPost(postId).block().apply { this?.title = URLDecoder.decode(this?.title) this?.content = URLDecoder.decode(this?.content) vm.modelMap.put("srcPost",this) } - if (TEMPTOKEN.equals(token)) { - vm.modelMap.put(WRITE_PERMISSION_KEY,"OK") - } else { - vm.modelMap.put(WRITE_PERMISSION_KEY,"NO") - } return vm } @@ -183,7 +178,7 @@ class BlogController() { @GetMapping("recent") fun recent() : ResultMV{ val vm = ResultMV("content/blog/viewer") - locationLogService.find20().forEach { + locationLogService.find10().forEach { logService.log(Gson().toJson(it)) } locationLogService.getLocationLog()?.let { diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BumsPrivate.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BumsPrivate.kt index 0fcab16..4682b5b 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BumsPrivate.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BumsPrivate.kt @@ -33,11 +33,9 @@ class BumsPrivate { @GetMapping("where") fun where() : ResultMV { val m = ResultMV("content/private/where") - locationService.find20().apply { + + locationService.find10().apply { m.modelMap.put("locations",this.reversed()) -// forEach { -// logService.log(it.timeString.plus(it.mAddressLines.joinToString(","))) -// } } m.setTitle("돼지 여기있다요~!!") return m @@ -60,15 +58,15 @@ class BumsPrivate { val responce = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(ResponceResult().apply { }) - CoroutineScope(Dispatchers.IO).launch { - location?.let { - val client = WebClient.create() - client.get() - .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${it.mAddressLines.first()} 저장") - .retrieve() - .bodyToMono(String::class.java).block() ?: "FAIL" - } - } +// CoroutineScope(Dispatchers.IO).launch { +// location?.let { +// val client = WebClient.create() +// client.get() +// .uri("https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${it.mAddressLines.first()} 저장") +// .retrieve() +// .bodyToMono(String::class.java).block() ?: "FAIL" +// } +// } return responce } diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt index e412795..67c7ca3 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/UserController.kt @@ -74,6 +74,7 @@ class UserController { @PostMapping("login.ajax") fun login(httpServletRequest: HttpServletRequest, @RequestBody jsonString: String) : ResponseEntity { try { + logService.log(httpServletRequest.requestURI) logService.log(jsonString) var lResultCode = 0 diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/model/BumsPrivate.kt b/src/main/kotlin/kr/lunaticbum/back/lun/model/BumsPrivate.kt index ad6be4e..33d4c0f 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/model/BumsPrivate.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/model/BumsPrivate.kt @@ -90,8 +90,8 @@ class LocationLogService : LocationService { @Autowired private lateinit var logRepository: LocationLogRepository - fun find20() : List { - return logRepository.findAll().takeLast(20).buffer(20).blockLast(Duration.ofSeconds(30)) ?: listOf() + fun find10() : List { + return logRepository.findAll().takeLast(10).buffer(10).blockLast(Duration.ofSeconds(30)) ?: listOf() } fun getLocationLog() : LocationLog? { return logRepository.findFirstByOrderByTimeDesc().block() diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/model/SearXng.kt b/src/main/kotlin/kr/lunaticbum/back/lun/model/SearXng.kt index 6bb724e..2b7d4df 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/model/SearXng.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/model/SearXng.kt @@ -12,7 +12,8 @@ class SearXng { var unresponsive_engines: ArrayList>? = null } class SearXngResult { -// var originQuery : String? = null + var originQuery : String? = null + var refinedQuery : String? = null var url: String? = null var title: String? = null var content: String? = null diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/service/Lama.kt b/src/main/kotlin/kr/lunaticbum/back/lun/service/Lama.kt index 802aec6..7d7d4c8 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/service/Lama.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/service/Lama.kt @@ -7,6 +7,7 @@ import com.google.gson.annotations.SerializedName import io.micrometer.observation.ObservationRegistry import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async import kotlinx.coroutines.launch import kr.lunaticbum.back.lun.configs.GlobalEnvironment import kr.lunaticbum.back.lun.controllers.TelegramSendMsg @@ -21,6 +22,7 @@ import org.springframework.ai.ollama.management.ModelManagementOptions import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Qualifier import org.springframework.http.MediaType +import org.springframework.scheduling.annotation.Async import org.springframework.stereotype.Service import org.springframework.web.reactive.function.BodyInserters import org.springframework.web.reactive.function.client.WebClient @@ -28,15 +30,14 @@ import reactor.kotlin.core.publisher.toMono import java.text.SimpleDateFormat import java.time.Duration import java.util.* +import kotlin.collections.ArrayList @Service class Lama { - @Qualifier("chatClient") - @Autowired - private lateinit var chatClient: OllamaApi + //, val date : String = SimpleDateFormat("yyyyMMddHHmmss").format(Date()) // data class QSearchData(val query : FloatArray,val limit : Int) @@ -62,7 +63,7 @@ class Lama { // .retrieve() // .bodyToMono(String::class.java).timeout(Duration.ofMinutes(20L)).block() ?: "" // } - var qPointsCount : Long = 0 + private fun checkCollection() : Long { val qUrl = "https://ollama.lunaticbum.kr/collections/blama_vectors" val client = WebClient.create() @@ -119,9 +120,9 @@ class Lama { } } return if (lastElement.size > 0) { - lastElement.map { it.children().eachText() }.joinToString(joinString) + lastElement.eachText().joinToString(joinString) } else { - body.children().map { it.children().eachText() }.joinToString(joinString) + body.children().eachText().joinToString(joinString) } } @@ -137,71 +138,116 @@ class Lama { // } - private fun addDocuments(query : String) { - val embeddingModel = OllamaEmbeddingModel( - chatClient, - OllamaOptions.builder().build(), - ObservationRegistry.create(), - ModelManagementOptions.defaults() - ) - val gSearch = "https://psn.lunaticbum.kr/search?q=${query?.replace("오늘", SimpleDateFormat("yyyMMdd").format(Date()))}&language=ko&time_range=month&safesearch=0&categories=general&format=json" - println("gSearch >>> ${gSearch}") - val sdss = QPut(arrayListOf()) - WebClient.create().get() - .uri(gSearch) - .retrieve() - .bodyToMono(SearXng::class.java).timeout(Duration.ofMinutes(20L)).block()?.let { gsResult -> - gsResult.results?.filter { it.score > 0.3}?.forEach { - qPointsCount += 1 - println("in filter ${it.url}") -// it.originQuery = query - val data = Gson().toJson(it) - println(it.title) - jsopFilter(it.url!!).let { text -> - try { - println("text >>>>> ${text?.chunked(50)?.first() ?: ""}") - var dispoable = chatClient.chat(OllamaApi.ChatRequest.Builder("phi4:14b").stream(false).format("json").messages( - listOf(OllamaApi.Message.Builder(OllamaApi.Message.Role.USER).content("원문:\n'${text}'\n원문의 웹 페이지 소스는 '$query'이 질문에 대해 연관 결과로 받은 내용이야. 해당 정보를 파악해서 'query:{질문},contents:{본문내용},summary:{요약},keywords:[키워드],related_links:[링크],relatedness_score:{0.0~10.0}'이 형식의 결과만들어줘 내용은 한국어로 부탁할께").build()) - ).build()).toMono().subscribe({aiResponce -> - it.pageData = aiResponce.message.content -// println(aiResponce) - println("summary result >>>>> ${it.pageData}") -// it.originHtml = text - val embeddingResponse = embeddingModel.call( - EmbeddingRequest( - listOf(data), - OllamaOptions.builder() - .model("nomic-embed-text") - .truncate(false).build() - ) - ) - sdss.points.add(QData(id = qPointsCount,embeddingResponse.result.output,it)) - },{err-> - err.printStackTrace() - }) - }catch (e : Exception) { - e.printStackTrace() + fun addDocuments(query : String , refinedQuery: RefinedQuery?) { + var querys : ArrayList = ArrayList() + querys.add(query) + refinedQuery?.ko_query?.let { querys.add(it) } + refinedQuery?.en_query?.let { querys.add(it) } + refinedQuery?.keywords?.let { querys.add(it.joinToString { " " })} + val readedUrls = ArrayList() + querys.forEach { refinedQuery -> + val gSearch = "https://psn.lunaticbum.kr/search?q=${refinedQuery?.replace("오늘", SimpleDateFormat("yyyMMdd").format(Date()))}&language=ko&time_range=month&safesearch=0&categories=general&format=json" + println("gSearch >>> ${gSearch}") + WebClient.create().get() + .uri(gSearch) + .retrieve() + .bodyToMono(SearXng::class.java).timeout(Duration.ofMinutes(20L)).block()?.let { gsResult -> + gsResult.results?.filter { it.url?.startsWith("https://") == true && it.score > 0.4}?.forEach { + println("in filter ${it.url}") + if (readedUrls.contains(it.url) == false) { + readedUrls.add(it.url!!) + it.originQuery = query + it.refinedQuery = refinedQuery + println(it.title) + try { + jsopFilter(it.url!!).let { text -> + it.originHtml = text + webPageSummarize(it,text) + } + }catch(e:Exception){e.printStackTrace()} } } } - } - println("out filter") - if (sdss.points.size > 0) { - val qUrl = "https://ollama.lunaticbum.kr/collections/blama_vectors".plus("/points") - val client = WebClient.create() - client.put() - .uri(qUrl) - .header("api-key", "blama-admin-key-gb") - .body(BodyInserters.fromValue(Gson().toJson(sdss))) - .retrieve() - .bodyToMono(String::class.java).timeout(Duration.ofMinutes(20L)).block() ?: "" - + println("end of search") } - println("end of search") } - private fun embedQuery(embedFlots : FloatArray) : QContents?{ + var format = "원문:\n'%s'\n원문의 웹 페이지 소스는 '%s'이 질문에 대해 연관 결과로 받은 내용이야. 해당 정보를 파악해서 'query:{질문},contents:{본문내용 한국어},summary:{요약 한국어},keywords:[키워드],related_links:[링크],relatedness_score:{0.0~10.0}'이 형식의 결과만들어줘" + internal fun makeSummarizeRequestMsg(it : SearXngResult) : String= format.format(it.originHtml,it.originQuery) + + internal fun makeCahtReq(reqMsg:String) = OllamaApi.ChatRequest.Builder("phi4:14b").stream(false).format("json").messages(listOf(OllamaApi.Message.Builder(OllamaApi.Message.Role.USER).content(reqMsg).build())).build() + + @Async + fun webPageSummarize(it : SearXngResult , text : String) { + try { + val chatClient = OllamaApi("https://lama.lunaticbum.kr") + val embeddingModel = OllamaEmbeddingModel( + chatClient, OllamaOptions.builder().build(), ObservationRegistry.create(), ModelManagementOptions.defaults()) + println("text >>>>> ${text?.chunked(50)?.first() ?: ""}") + var dispoable = chatClient.chat(makeCahtReq(makeSummarizeRequestMsg(it))).toMono().subscribe({aiResponce -> + it.pageData = aiResponce.message.content + println("summary result >>>>> ${it.pageData}") + val embeddingResponse = embeddingModel.call( + EmbeddingRequest( + listOf(aiResponce.message.content), + OllamaOptions.builder() + .model("bge-m3") + .truncate(false).build() + ) + ) + infomationDic.put(it.url!!,aiResponce.message.content) + val sdss = QPut(arrayListOf()) + sdss.points.add(QData(id = System.currentTimeMillis(),embeddingResponse.result.output,it)) + if (sdss.points.size > 0) { + val qUrl = "https://ollama.lunaticbum.kr/collections/blama_vectors".plus("/points") + val client = WebClient.create() + client.put() + .uri(qUrl) + .header("api-key", "blama-admin-key-gb") + .body(BodyInserters.fromValue(Gson().toJson(sdss))) + .retrieve() + .bodyToMono(String::class.java).timeout(Duration.ofMinutes(20L)).subscribe( + {resultString -> },{error-> error.printStackTrace()} + ) + } + },{err-> + err.printStackTrace() + }) + }catch (e : Exception) { + + e.printStackTrace() + } + } + + class RefinedQuery { + var ko_query : String? = null + var en_query : String? = null + var keywords : Array? = null + } + + var queryFormat = "질문:\n'%s'\n앞은 질문의 내용을 정리해서 '{ko_query:한국어 질문,en_query:영어 번역된 질문,ko_keywords:[한국어 키워드],en_keyword:[영문키워드]}'이 형식의 결과를 부탁할께" + internal fun makeQuerySummarizeRequestMsg(query : String) : String= queryFormat.format(query) + fun querySummarize(query: String) : RefinedQuery? { + var refinedQuery : RefinedQuery? = null + try { + val chatClient = OllamaApi("https://lama.lunaticbum.kr") + var dispoable = chatClient.chat(makeCahtReq(makeQuerySummarizeRequestMsg(query))).toMono().subscribe({aiResponce -> + println("summary result >>>>> ${aiResponce.message.content}") + refinedQuery = Gson().fromJson(aiResponce.message.content, RefinedQuery::class.java) + },{err-> + err.printStackTrace() + }) + }catch (e : Exception) { + + e.printStackTrace() + } + return refinedQuery + } + + + @Async + public fun embedQuery(embedFlots : FloatArray) : QContents?{ val qUrl = "https://ollama.lunaticbum.kr/collections/blama_vectors".plus("/points/search") val client = WebClient.create() var lists = client.post() @@ -233,63 +279,41 @@ class Lama { @Autowired lateinit var globalEvv : GlobalEnvironment + var infomationDic = hashMapOf() suspend fun generateResponse(query: String?, targetId: String? = globalEvv.telegramMyId) { + infomationDic.clear() + val chatClient = OllamaApi("https://lama.lunaticbum.kr") val embeddingModel = OllamaEmbeddingModel( - chatClient, - OllamaOptions.builder().build(), - ObservationRegistry.create(), - ModelManagementOptions.defaults() - ) + chatClient, OllamaOptions.builder().build(), ObservationRegistry.create(), ModelManagementOptions.defaults()) println("On generateResponse :: find something ${query}") - query?.let { - var embeddingResponse = embeddingModel.call( - EmbeddingRequest( - listOf(query), - OllamaOptions.builder() - .model("nomic-embed-text") - .truncate(false) - .build() - ) - ) + query?.let { originalQuery -> + var embeddingResponse = embeddingModel.call(EmbeddingRequest(listOf(originalQuery), OllamaOptions.builder().model("bge-m3").truncate(false).build())) + addDocuments(originalQuery, querySummarize(originalQuery)) println("points size ${embeddingResponse.result.output.size}") - qPointsCount = checkCollection() - addDocuments(it) - embeddingResponse = embeddingModel.call( - EmbeddingRequest( - listOf(query), - OllamaOptions.builder() - .model("nomic-embed-text") - .truncate(false) - .build() - ) - ) - println(embeddingResponse.result.output) var context : String? = "" try { embedQuery(embeddingResponse.result.output)?.result?.forEach { result -> - context += "참고자료:".plus(if (result.payload?.pageData?.length ?: 0 > 10) { - result.payload?.pageData - } else { - result.payload?.content - }) - context +="\n" + if (infomationDic.contains(result.payload?.url ?: "NONE") == false) { + context += "\n# :".plus(if (result.payload?.pageData?.length ?: 0 > 10) { + result.payload?.pageData + } else { + result.payload?.content + }) + } } - }catch (e:Exception){ e.printStackTrace() } - val response: OllamaApi.ChatResponse = if (context?.length ?: 0 > 10) { - val prompt = "Context information is below.\n$context\nGiven the context information and not prior knowledge, answer the query: $query\n한국어로 대답해줘".trimIndent() - chatClient.chat(OllamaApi.ChatRequest.Builder("phi4:14b").stream(false).format("json").messages( - listOf(OllamaApi.Message.Builder(OllamaApi.Message.Role.USER).content(prompt).build()) - ).build()) - } else { - chatClient.chat(OllamaApi.ChatRequest.Builder("phi4:14b").stream(false).format("json").messages( - listOf(OllamaApi.Message.Builder(OllamaApi.Message.Role.USER).content(query).build()) - ).build()) - } + + infomationDic.iterator().forEach { context += "\n#${it.key}:${it.value}" } + + val prompt = "참조:\n$context\n참조 내용을 고려해서\n해당 질문:${query}\n에 {질문내용:[한국어],답변내용:[한국어],전체키워드:[],참조링크:[]}형식으로 대답 해줘 ".trimIndent() + println(prompt) + val response: OllamaApi.ChatResponse = chatClient.chat(OllamaApi.ChatRequest.Builder("phi4:14b").stream(false).format("json").messages( + listOf(OllamaApi.Message.Builder(OllamaApi.Message.Role.USER).content(prompt).build()) + ).build()) + println(response.message.content) -// val fullUrl = "https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage?chat_id=${globalEvv.telegramMyId}&text=${query}의 대답이 도착했어요.\n\n${response.message.content}" CoroutineScope(Dispatchers.IO).launch { var toalmsg = "${query}의 대답이 도착했어요.\n${response.message.content}" val fullUrl = "https://api.telegram.org/${globalEvv.telegramBotKey}/sendMessage" @@ -309,8 +333,6 @@ class Lama { } } } - - println("On generateResponse :: END OF Answer") } } diff --git a/src/main/resources/static/js/common.js b/src/main/resources/static/js/common.js index b99f0f3..f17618e 100644 --- a/src/main/resources/static/js/common.js +++ b/src/main/resources/static/js/common.js @@ -5,13 +5,9 @@ onload = function() { var refreshToken = get_cookie("refresh") console.log("access === " + accToken + " || " + accToken.length); console.log("refresh === " + refreshToken + " || " + refreshToken.length); - - if (accToken.length < 1) { - document.cookie = "refresh="+ window.sessionStorage.getItem("REFRESH") + ";"; - } - if (refreshToken.length < 1) { - window.sessionStorage.setItem("REFRESH",get_cookie("refresh")) - } + document.cookie = "access=; expires=Thu, 01 Jan 1970 00:00:01 GMT;" + document.cookie = "refresh=; expires=Thu, 01 Jan 1970 00:00:01 GMT;" + document.cookie = "CLEAR=; expires=Thu, 01 Jan 1970 00:00:01 GMT;" } onbeforeunload = function () { var accToken = get_cookie("access") @@ -160,6 +156,10 @@ function logout() { // retrieve all cookies document.cookie = "access=; expires=Thu, 01 Jan 1970 00:00:01 GMT;" document.cookie = "refresh=; expires=Thu, 01 Jan 1970 00:00:01 GMT;" + + console.log(document.cookie["JSESSIONID"]) + document.cookie = "JSESSIONID=; expires=Thu, 01 Jan 1970 00:00:01 GMT;" + document.cookie = "CLEAR="+Date.now()+""; let logOutUrl = getMainPath() + "/user/logout.ajax"; post(logOutUrl,"","","", function (resultData) { alert("로그아웃 됨요~! 빠염~!")