Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change model to gpt-3.5-turbo-instruct (#1) #2

Merged
merged 4 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}
Loading