Skip to content

Commit

Permalink
Merge pull request #2 from lauvsong/develop
Browse files Browse the repository at this point in the history
Change model to gpt-3.5-turbo-instruct (#1)
  • Loading branch information
lauvsong committed Apr 8, 2024
2 parents 2088887 + 13f6127 commit 571e89f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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
) {

Expand All @@ -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()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ data class ChatGptResponse(
code = choices.first().text.substringAfter("Code:").trim('`', '\n', ' ')
)
}

fun getFinishReason(): String {
return choices.first().finishReason
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
34 changes: 29 additions & 5 deletions src/main/kotlin/com/lauvsong/refactorgpt/service/ChatGptService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ChatGptResponse>): Refactored =
takeUnless { response.code() == 401 }
?.let { response.body()?.toRefactored() }
?: throw ChatGptAuthenticationException()
private fun onRefactorSuccess(response: Response<ChatGptResponse>): 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()
}
}

0 comments on commit 571e89f

Please sign in to comment.