Skip to content

Commit

Permalink
Optimize OpenAi translator disk cache
Browse files Browse the repository at this point in the history
  • Loading branch information
YiiGuxing committed Jan 30, 2024
1 parent 7c095af commit 8dd0fa5
Showing 1 changed file with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -76,7 +76,7 @@ object OpenAITranslator : AbstractTranslator(), DocumentationTranslator {
return result
}

private fun getChatCompletionRequest(
private fun getChatCompletionMessages(
text: String,
srcLang: Lang,
targetLang: Lang,
Expand All @@ -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? {
Expand Down

0 comments on commit 8dd0fa5

Please sign in to comment.