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)))