From 8dd0fa579adfd055efa9799b86c31f6bb2a904a2 Mon Sep 17 00:00:00 2001 From: "Yii.Guxing" Date: Tue, 30 Jan 2024 23:01:22 +0800 Subject: [PATCH] Optimize OpenAi translator disk cache --- .../plugin/translate/trans/openai/OpenAITranslator.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/kotlin/cn/yiiguxing/plugin/translate/trans/openai/OpenAITranslator.kt b/src/main/kotlin/cn/yiiguxing/plugin/translate/trans/openai/OpenAITranslator.kt index 20dc1a82e..2e8744619 100644 --- a/src/main/kotlin/cn/yiiguxing/plugin/translate/trans/openai/OpenAITranslator.kt +++ b/src/main/kotlin/cn/yiiguxing/plugin/translate/trans/openai/OpenAITranslator.kt @@ -65,8 +65,8 @@ object OpenAITranslator : AbstractTranslator(), DocumentationTranslator { return cache } - val request = getChatCompletionRequest(text, srcLang, targetLang, isFofDocumentation) - val chatCompletion = OpenAIService.get(settings).chatCompletion(request) + val messages = getChatCompletionMessages(text, srcLang, targetLang, isFofDocumentation) + val chatCompletion = OpenAIService.get(settings).chatCompletion(messages) var result = chatCompletion.choices.first().message!!.content if (!isFofDocumentation && result.length > 1 && result.first() == '"' && result.last() == '"') { result = result.substring(1, result.lastIndex) @@ -76,7 +76,7 @@ object OpenAITranslator : AbstractTranslator(), DocumentationTranslator { return result } - private fun getChatCompletionRequest( + private fun getChatCompletionMessages( text: String, srcLang: Lang, targetLang: Lang, @@ -103,7 +103,8 @@ object OpenAITranslator : AbstractTranslator(), DocumentationTranslator { private fun getCacheKey(text: String, srcLang: Lang, targetLang: Lang): String { val model = settings.model - return "$id$model$text$srcLang$targetLang".md5() + val provider = settings.provider + return "$id$provider$model$text$srcLang$targetLang".md5() } override fun createErrorInfo(throwable: Throwable): ErrorInfo? {