From 5d3c0561c67cbe2d91095dbb4d8b803a94b57e4c Mon Sep 17 00:00:00 2001 From: Joseph Ottinger Date: Mon, 5 Feb 2024 15:49:47 -0500 Subject: [PATCH] Changing ChatGPTDao to a singleton so the configuration and usage is cleaner --- src/main/kotlin/javabot/dao/ChatGPTDao.kt | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/src/main/kotlin/javabot/dao/ChatGPTDao.kt b/src/main/kotlin/javabot/dao/ChatGPTDao.kt index 7592c8cc..f200c5a1 100644 --- a/src/main/kotlin/javabot/dao/ChatGPTDao.kt +++ b/src/main/kotlin/javabot/dao/ChatGPTDao.kt @@ -1,6 +1,7 @@ package javabot.dao import com.google.inject.Inject +import com.google.inject.Singleton import gpt.api.GPT import gpt.models.Message import gpt.models.MessageModel @@ -9,25 +10,18 @@ import javabot.JavabotConfig import javabot.operations.throttle.BotRateLimiter +@Singleton class ChatGPTDao @Inject constructor(val javabotConfig: JavabotConfig) { - companion object { - private var limiter: BotRateLimiter? = null - fun acquire(javabotConfig: JavabotConfig): Boolean { - if (limiter == null) { - limiter = BotRateLimiter( - javabotConfig.chatGptLimit(), - Duration.days(1).toMilliseconds() - ) - } - return limiter!!.tryAcquire() - } - } + private var limiter: BotRateLimiter = BotRateLimiter( + javabotConfig.chatGptLimit(), + Duration.days(1).toMilliseconds() + ) fun sendPromptToChatGPT(prompt: String): String? { - return if (javabotConfig.chatGptKey().isNotEmpty() && acquire(javabotConfig)) { + return if (javabotConfig.chatGptKey().isNotEmpty() && limiter.tryAcquire()) { val model = "gpt-4" val gpt = GPT(javabotConfig.chatGptKey()) val messageModel = MessageModel(model, listOf(Message("user", prompt)))