Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit dfeec3c

Browse files
committed
feat(plugin): add prompt size validation
This commit adds a feature to validate the size of the prompt before sending it to the OpenAI service. If the prompt is too large, a notification is sent and the prompt is not sent to the service. Reason for the change: The plugin was not validating the size of the prompt before sending it to the OpenAI service, which could cause errors in the service. This feature ensures that the prompt is within the acceptable size limits.
1 parent ae1ff2b commit dfeec3c

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/AICommitAction.kt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import com.intellij.openapi.vcs.VcsDataKeys
1515
import com.intellij.openapi.vcs.changes.Change
1616
import com.intellij.openapi.vcs.ui.CommitMessage
1717
import com.intellij.vcs.commit.AbstractCommitWorkflowHandler
18-
import com.knuddels.jtokkit.Encodings
19-
import com.knuddels.jtokkit.api.EncodingType
2018
import git4idea.repo.GitRepositoryManager
2119
import kotlinx.coroutines.DelicateCoroutinesApi
2220
import kotlinx.coroutines.Dispatchers
@@ -109,10 +107,4 @@ class AICommitAction : AnAction(), DumbAware {
109107
}
110108
.joinToString("\n")
111109
}
112-
113-
private fun isPromptTooLarge(prompt: String): Boolean {
114-
val registry = Encodings.newDefaultEncodingRegistry()
115-
val encoding = registry.getEncoding(EncodingType.CL100K_BASE)
116-
return encoding.countTokens(prompt) > 4000
117-
}
118110
}

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/OpenAIService.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import com.github.blarc.ai.commits.intellij.plugin.settings.AppSettings
1010
import com.intellij.openapi.application.ApplicationManager
1111
import com.intellij.openapi.components.Service
1212

13+
import com.knuddels.jtokkit.Encodings
14+
import com.knuddels.jtokkit.api.EncodingType
15+
1316
@Service
1417
class OpenAIService {
1518
companion object {
@@ -27,6 +30,11 @@ class OpenAIService {
2730
val prompt = AppSettings.instance.getPrompt().replace("{diffs}", diff)
2831
.replace("{hint}", hint ?: "No hint provided")
2932

33+
if (isPromptTooLarge(prompt)) {
34+
sendNotification(Notification.promptTooLarge())
35+
return "Prompt is too large"
36+
}
37+
3038
sendNotification(Notification.usedPrompt(prompt))
3139

3240
val chatCompletionRequest = ChatCompletionRequest(
@@ -52,4 +60,10 @@ class OpenAIService {
5260
suspend fun verifyToken(token: String){
5361
OpenAI(token).models()
5462
}
63+
64+
private fun isPromptTooLarge(prompt: String): Boolean {
65+
val registry = Encodings.newDefaultEncodingRegistry()
66+
val encoding = registry.getEncoding(EncodingType.CL100K_BASE)
67+
return encoding.countTokens(prompt) > 4000
68+
}
5569
}

0 commit comments

Comments
 (0)