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..1ecbdf1 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 ) { @@ -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() } 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/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) 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() + } }