fix: prompt creation from placeholders for custom openai provider (fixes #662)

This commit is contained in:
Carl-Robert Linnupuu 2024-08-21 16:23:45 +03:00
parent e9c72dfdee
commit ce0b90f232
2 changed files with 20 additions and 12 deletions

View file

@ -6,7 +6,7 @@ import com.intellij.openapi.components.service
import ee.carlrobert.codegpt.completions.llama.LlamaModel
import ee.carlrobert.codegpt.credentials.CredentialsStore.CredentialKey
import ee.carlrobert.codegpt.credentials.CredentialsStore.getCredential
import ee.carlrobert.codegpt.settings.configuration.Placeholder
import ee.carlrobert.codegpt.settings.configuration.Placeholder.*
import ee.carlrobert.codegpt.settings.service.codegpt.CodeGPTServiceSettings
import ee.carlrobert.codegpt.settings.service.custom.CustomServiceSettings
import ee.carlrobert.codegpt.settings.service.llama.LlamaSettings
@ -140,11 +140,16 @@ object CodeCompletionRequestFactory {
details: InfillRequestDetails
): Any {
if (value !is String) return value
return when (value) {
"$" + Placeholder.FIM_PROMPT -> template.buildPrompt(details)
"$" + Placeholder.PREFIX -> details.prefix
"$" + Placeholder.SUFFIX -> details.suffix
else -> value
FIM_PROMPT.code -> template.buildPrompt(details)
PREFIX.code -> details.prefix
SUFFIX.code -> details.suffix
else -> {
return value.takeIf { it.contains(PREFIX.code) || it.contains(SUFFIX.code) }
?.replace(PREFIX.code, details.prefix)
?.replace(SUFFIX.code, details.suffix) ?: value
}
}
}
@ -158,4 +163,4 @@ object CodeCompletionRequestFactory {
}
private fun isBoundaryCharacter(c: Char): Boolean = c in "()[]{}<>~!@#$%^&*-+=|\\;:'\",./?"
}
}

View file

@ -5,12 +5,15 @@ import git4idea.GitUtil
import git4idea.branch.GitBranchUtil
import java.time.LocalDate
enum class Placeholder(val description: String) {
DATE_ISO_8601("Current date in ISO 8601 format, e.g. 2021-01-01."),
BRANCH_NAME("The name of the current branch."),
PREFIX("Code before the cursor."),
SUFFIX("Code after the cursor."),
FIM_PROMPT("Prebuilt Fill-In-The-Middle (FIM) prompt using the specified template."),
enum class Placeholder(val description: String, val code: String) {
DATE_ISO_8601("Current date in ISO 8601 format, e.g. 2021-01-01.", "$" + "DATE_ISO_8601"),
BRANCH_NAME("The name of the current branch.", "$" + "BRANCH_NAME"),
PREFIX("Code before the cursor.", "$" + "PREFIX"),
SUFFIX("Code after the cursor.", "$" + "SUFFIX"),
FIM_PROMPT(
"Prebuilt Fill-In-The-Middle (FIM) prompt using the specified template.",
"$" + "FIM_PROMPT"
),
}
interface PlaceholderStrategy {