From 4699b50676f7e1d019f7c665ad81b884cec43e96 Mon Sep 17 00:00:00 2001 From: lauvsong Date: Mon, 8 Apr 2024 16:07:08 +0900 Subject: [PATCH 1/4] :sparkles: Change model to gpt-3.5-turbo-instruct --- .../com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt b/src/main/kotlin/com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt index 2f46e6f..6bf9567 100644 --- a/src/main/kotlin/com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt +++ b/src/main/kotlin/com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt @@ -1,9 +1,9 @@ package com.lauvsong.refactorgpt.dto.request data class ChatGptRequest( - val model: String = "text-davinci-003", + val model: String = "gpt-3.5-turbo-instruct", val prompt: String, - val maxTokens: Int = 500, + val maxTokens: Int = 1600, val temperature: Int = 0 ) { From a1938e7670e68a4f76f9360cf85214260dad8529 Mon Sep 17 00:00:00 2001 From: lauvsong Date: Mon, 8 Apr 2024 16:09:00 +0900 Subject: [PATCH 2/4] :sparkles: Improve prompt clearer --- .../com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt b/src/main/kotlin/com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt index 6bf9567..1ecbdf1 100644 --- a/src/main/kotlin/com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt +++ b/src/main/kotlin/com/lauvsong/refactorgpt/dto/request/ChatGptRequest.kt @@ -13,15 +13,14 @@ data class ChatGptRequest( private fun makePrompt(fileExtension: String, code: String): String = """ - You role is perfect code refactoring prompt. + You role is code refactoring output. Refactor the following code for better readability and maintainability. - Don't say ANY explain. Just response the code strictly. + Return only the refactored code and don't explain anything. This code's file extension: $fileExtension Here is the code: ``` $code ``` - Respond start with the line 'Code:' """.trimIndent() } From c65928412d118f7a13a6b8b2e6312309798b2e79 Mon Sep 17 00:00:00 2001 From: lauvsong Date: Mon, 8 Apr 2024 16:11:50 +0900 Subject: [PATCH 3/4] =?UTF-8?q?=E2=9C=A8=20Improve=20alert=20message=20::?= =?UTF-8?q?=20authentication=20failed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../refactorgpt/exception/ChatGptAuthenticationException.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/lauvsong/refactorgpt/exception/ChatGptAuthenticationException.kt b/src/main/kotlin/com/lauvsong/refactorgpt/exception/ChatGptAuthenticationException.kt index b7c661c..7d00a74 100644 --- a/src/main/kotlin/com/lauvsong/refactorgpt/exception/ChatGptAuthenticationException.kt +++ b/src/main/kotlin/com/lauvsong/refactorgpt/exception/ChatGptAuthenticationException.kt @@ -2,6 +2,10 @@ package com.lauvsong.refactorgpt.exception class ChatGptAuthenticationException( message: String? = """ - Authentication failed. Check API key settings. + Authentication failed. Check below things. + 1. API key settings configured in Settings. + 2. API key settings configured in OpenAI. + 3. Ensure a credit card is registered with OpenAI. + If these steps do not resolve the issue, please get a new API key and try again. """ ) : Exception(message) From 13f61273521a82b097c6160855be8f55410c8d83 Mon Sep 17 00:00:00 2001 From: lauvsong Date: Mon, 8 Apr 2024 16:16:50 +0900 Subject: [PATCH 4/4] =?UTF-8?q?=E2=9C=A8=20Separate=20error=20cases=20clea?= =?UTF-8?q?rer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/ChatGptResponse.kt | 4 +++ .../refactorgpt/service/ChatGptService.kt | 34 ++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/com/lauvsong/refactorgpt/dto/response/ChatGptResponse.kt b/src/main/kotlin/com/lauvsong/refactorgpt/dto/response/ChatGptResponse.kt index 2b0af79..854619e 100644 --- a/src/main/kotlin/com/lauvsong/refactorgpt/dto/response/ChatGptResponse.kt +++ b/src/main/kotlin/com/lauvsong/refactorgpt/dto/response/ChatGptResponse.kt @@ -18,4 +18,8 @@ data class ChatGptResponse( code = choices.first().text.substringAfter("Code:").trim('`', '\n', ' ') ) } + + fun getFinishReason(): String { + return choices.first().finishReason + } } diff --git a/src/main/kotlin/com/lauvsong/refactorgpt/service/ChatGptService.kt b/src/main/kotlin/com/lauvsong/refactorgpt/service/ChatGptService.kt index a1ac363..a41545a 100644 --- a/src/main/kotlin/com/lauvsong/refactorgpt/service/ChatGptService.kt +++ b/src/main/kotlin/com/lauvsong/refactorgpt/service/ChatGptService.kt @@ -21,14 +21,38 @@ class ChatGptService( onSuccess = { response -> onRefactorSuccess(response) }, onFailure = { exception -> if (exception is SocketTimeoutException) { - throw ChatGptFetchFailureException("timeout error.\nPlease check your network or set longer timeout in settings.") + throw ChatGptFetchFailureException( + """ + timeout error. + Please check your network or set longer timeout in settings. + """ + ) } throw ChatGptFetchFailureException(exception.message) } ) - private fun onRefactorSuccess(response: Response): Refactored = - takeUnless { response.code() == 401 } - ?.let { response.body()?.toRefactored() } - ?: throw ChatGptAuthenticationException() + private fun onRefactorSuccess(response: Response): Refactored { + if (response.code() == 401) { + throw ChatGptAuthenticationException() + } + + val body = response.body() + ?: throw ChatGptFetchFailureException("OpenAI's response body is null.") + + if (response.isSuccessful.not()) { + throw ChatGptFetchFailureException("${response.errorBody()?.string()}") + } + + if (body.getFinishReason() == "length") { + throw ChatGptFetchFailureException( + """ + The response exceeds the maximum token limit. + Please try again with a shorter code. + """ + ) + } + + return body.toRefactored() + } }