Skip to content

Commit 31c96e4

Browse files
authored
Merge pull request #127 from jialiu-github/config
feat: should cut down prompts when they exceed the max token length
2 parents 7526032 + 941e993 commit 31c96e4

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/main/kotlin/cc/unitmesh/devti/intentions/action/task/BaseCompletionTask.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import cc.unitmesh.devti.util.InsertUtil
66
import cc.unitmesh.devti.util.LLMCoroutineScope
77
import cc.unitmesh.devti.intentions.action.CodeCompletionBaseIntention
88
import cc.unitmesh.devti.llms.LlmFactory
9+
import cc.unitmesh.devti.settings.AutoDevSettingsState
910
import cc.unitmesh.devti.statusbar.AutoDevStatus
1011
import cc.unitmesh.devti.statusbar.AutoDevStatusService
1112
import cc.unitmesh.devti.util.parser.Code
@@ -38,7 +39,9 @@ abstract class BaseCompletionTask(private val request: CodeCompletionRequest) :
3839
AutoDevStatusService.notifyApplication(AutoDevStatus.InProgress)
3940

4041
val prompt = promptText()
41-
val flow: Flow<String> = LlmFactory().create(request.project).stream(prompt, "", keepHistory())
42+
43+
val keepHistory = keepHistory() && prompt.length < AutoDevSettingsState.maxTokenLength
44+
val flow: Flow<String> = LlmFactory().create(request.project).stream(prompt, "", keepHistory)
4245
logger.info("Prompt: $prompt")
4346

4447
DumbAwareAction.create {

src/main/kotlin/cc/unitmesh/devti/intentions/action/task/SimilarCodeCompletionTask.kt

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cc.unitmesh.devti.intentions.action.task
22

3+
import cc.unitmesh.devti.settings.AutoDevSettingsState
34
import com.intellij.temporary.similar.chunks.SimilarChunksWithPaths
45
import com.intellij.lang.LanguageCommenters
56

@@ -28,20 +29,34 @@ class SimilarCodeCompletionTask(private val request: CodeCompletionRequest) : Ba
2829
private val commenter = LanguageCommenters.INSTANCE.forLanguage(request.element!!.language)
2930
private val commentPrefix = commenter?.lineCommentPrefix
3031

32+
val start = "code complete for given code, just return rest part of code. \n"
33+
val end = "\nreturn rest code:"
3134
override fun promptText(): String {
3235
val documentLength = request.editor.document.textLength
36+
val prefix = generatePrefix(documentLength)
37+
38+
return if (chunksString == null) {
39+
"$start$prefix\n$end"
40+
} else {
41+
"$start: \n$commentPrefix\n$chunksString\n$prefix\n$end"
42+
}
43+
}
44+
45+
private fun generatePrefix(documentLength: Int): String {
3346
val prefix = if (request.offset > documentLength) {
3447
request.prefixText
3548
} else {
36-
val text = request.editor.document.text
37-
text.substring(0, request.offset)
49+
request.editor.document.text.substring(0, request.offset)
3850
}
3951

40-
return if (chunksString == null) {
41-
"code complete for given code, just return rest part of code. \n$prefix\n\nreturn rest code:"
42-
} else {
43-
"complete code for given code, just return rest part of code.: \n$commentPrefix\n$chunksString\n$prefix\n\nreturn rest code:"
52+
val prefixMaxLength =
53+
AutoDevSettingsState.maxTokenLength - start.length - (commentPrefix?.length ?: 0) - (chunksString?.length
54+
?: 0)
55+
if (prefix.length >= prefixMaxLength){
56+
return prefix.substring(prefix.length - prefixMaxLength)
4457
}
58+
59+
return prefix
4560
}
4661
}
4762

0 commit comments

Comments
 (0)