Skip to content

Commit

Permalink
feat: init ui config for custom json format #25
Browse files Browse the repository at this point in the history
  • Loading branch information
phodal committed Aug 18, 2023
1 parent 5cef076 commit b249f72
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 23 deletions.
Expand Up @@ -47,7 +47,7 @@ open class JavaContextPrompter : ContextPrompter() {
}

init {
val prompts = autoDevSettingsState.customEnginePrompts
val prompts = autoDevSettingsState.customPrompts
customPromptConfig = CustomPromptConfig.tryParse(prompts)
}

Expand Down
Expand Up @@ -20,7 +20,7 @@ data class CustomPromptConfig(
private val logger = logger<CustomPromptConfig>()

fun load(): CustomPromptConfig {
val config = tryParse(AutoDevSettingsState.getInstance().customEnginePrompts)
val config = tryParse(AutoDevSettingsState.getInstance().customPrompts)
logger.info("Loaded prompt config: $config")
return config
}
Expand Down
Expand Up @@ -49,7 +49,7 @@ class AzureOpenAIProvider(val project: Project) : LLMProvider {
private val openAiVersion: String

init {
val prompts = autoDevSettingsState.customEnginePrompts
val prompts = autoDevSettingsState.customPrompts
openAiVersion = AutoDevSettingsState.getInstance().openAiModel
customPromptConfig = CustomPromptConfig.tryParse(prompts)
}
Expand Down
21 changes: 15 additions & 6 deletions src/main/kotlin/cc/unitmesh/devti/llms/custom/CustomLLMProvider.kt
Expand Up @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.intellij.openapi.components.Service
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.jayway.jsonpath.JsonPath
import com.theokanning.openai.completion.chat.ChatCompletionResult
import com.theokanning.openai.service.SSE
import io.reactivex.BackpressureStrategy
Expand Down Expand Up @@ -34,13 +35,14 @@ class CustomLLMProvider(val project: Project) : LLMProvider {
private val autoDevSettingsState = AutoDevSettingsState.getInstance()
private val url = autoDevSettingsState.customEngineServer
private val key = autoDevSettingsState.customEngineToken
private val engineFormat = autoDevSettingsState.customEngineResponseFormat
private var customPromptConfig: CustomPromptConfig? = null
private var client = OkHttpClient()
private val timeout = Duration.ofSeconds(600)
private val messages: MutableList<Message> = ArrayList()

init {
val prompts = autoDevSettingsState.customEnginePrompts
val prompts = autoDevSettingsState.customPrompts
customPromptConfig = CustomPromptConfig.tryParse(prompts)
}

Expand Down Expand Up @@ -84,11 +86,18 @@ class CustomLLMProvider(val project: Project) : LLMProvider {
sseFlowable
.doOnError(Throwable::printStackTrace)
.blockingForEach { sse ->
val result: ChatCompletionResult =
ObjectMapper().readValue(sse!!.data, ChatCompletionResult::class.java)
val completion = result.choices[0].message
if (completion != null && completion.content != null) {
trySend(completion.content)
if (engineFormat.isNotEmpty()) {
JsonPath.parse(sse.data).read(engineFormat, String::class.java).let {
trySend(it)
}
} else {
val result: ChatCompletionResult =
ObjectMapper().readValue(sse!!.data, ChatCompletionResult::class.java)

val completion = result.choices[0].message
if (completion != null && completion.content != null) {
trySend(completion.content)
}
}
}

Expand Down
30 changes: 17 additions & 13 deletions src/main/kotlin/cc/unitmesh/devti/settings/AppSettingsComponent.kt
Expand Up @@ -10,9 +10,6 @@ import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBPasswordField
import com.intellij.ui.components.JBTextField
import com.intellij.util.ui.FormBuilder
import com.jetbrains.jsonSchema.JsonSchemaMappingsProjectConfiguration
import com.jetbrains.jsonSchema.UserDefinedJsonSchemaConfiguration
import com.jetbrains.jsonSchema.impl.JsonSchemaVersion
import java.awt.Dimension
import java.awt.FontMetrics
import javax.swing.JComponent
Expand All @@ -35,7 +32,7 @@ class AppSettingsComponent(settings: AutoDevSettingsState) {
private val aiEngine = ComboBox(AI_ENGINES)
private val customEngineServer = JBTextField()
private val customEngineToken = JBTextField()
private val customEngineResponse = JBTextField()
private val customEngineResponseFormat = JBTextField()
private val language = ComboBox(HUMAN_LANGUAGES)
private val maxTokenLengthInput = JBTextField()
//
Expand Down Expand Up @@ -93,7 +90,12 @@ class AppSettingsComponent(settings: AutoDevSettingsState) {
.addSeparator()
.addLabeledComponent(JBLabel("Custom Engine Server: "), customEngineServer, 1, false)
.addLabeledComponent(JBLabel("Custom Engine Token: "), customEngineToken, 1, false)
.addLabeledComponent(JBLabel("Custom Engine Response (Json Path): "), customEngineResponse, 1, false)
.addLabeledComponent(
JBLabel("Custom Engine Response Format (Json Path): "),
customEngineResponseFormat,
1,
false
)
.addVerticalGap(2)
.addSeparator()
.addLabeledComponent(JBLabel("Customize Prompt (Json): "), customEnginePrompt, 1, true)
Expand Down Expand Up @@ -162,19 +164,19 @@ class AppSettingsComponent(settings: AutoDevSettingsState) {
customEngineToken.text = newText
}

private fun getCustomEngineResponse(): String {
return customEngineResponse.text
private fun getCustomEngineResponseFormat(): String {
return customEngineResponseFormat.text
}

private fun setCustomEngineResponse(newText: String) {
customEngineResponse.text = newText
private fun setCustomEngineResponseFormat(newText: String) {
customEngineResponseFormat.text = newText
}

private fun getCustomEnginePrompt(): String {
return customEnginePrompt.text
}

private fun setCustomEnginePrompt(newText: String) {
private fun setCustomPrompts(newText: String) {
customEnginePrompt.text = newText
}

Expand Down Expand Up @@ -203,7 +205,7 @@ class AppSettingsComponent(settings: AutoDevSettingsState) {
settings.aiEngine != getAiEngine() ||
settings.customEngineServer != getCustomEngineServer() ||
settings.customEngineToken != getCustomEngineToken() ||
settings.customEnginePrompts != getCustomEnginePrompt() ||
settings.customPrompts != getCustomEnginePrompt() ||
settings.language != getLanguage() ||
settings.maxTokenLength != getMaxTokenLength()
}
Expand All @@ -220,7 +222,8 @@ class AppSettingsComponent(settings: AutoDevSettingsState) {
aiEngine = getAiEngine()
customEngineServer = getCustomEngineServer()
customEngineToken = getCustomEngineToken()
customEnginePrompts = getCustomEnginePrompt()
customPrompts = getCustomEnginePrompt()
customEngineResponseFormat = getCustomEngineResponseFormat()
language = getLanguage()
maxTokenLength = getMaxTokenLength()
}
Expand All @@ -238,7 +241,8 @@ class AppSettingsComponent(settings: AutoDevSettingsState) {
setAiEngine(it.aiEngine)
setCustomEngineServer(it.customEngineServer)
setCustomEngineToken(it.customEngineToken)
setCustomEnginePrompt(it.customEnginePrompts)
setCustomPrompts(it.customPrompts)
setCustomEngineResponseFormat(it.customEngineResponseFormat)
setLanguage(it.language)
setMaxTokenLength(it.maxTokenLength)
}
Expand Down
Expand Up @@ -16,7 +16,12 @@ class AutoDevSettingsState : PersistentStateComponent<AutoDevSettingsState> {
var customOpenAiHost = ""
var customEngineServer = ""
var customEngineToken = ""
var customEnginePrompts = ""
var customPrompts = ""

/**
* should be a json path
*/
var customEngineResponseFormat = ""
var language = DEFAULT_HUMAN_LANGUAGE
var maxTokenLength = MAX_TOKEN_LENGTH.toString()

Expand Down

0 comments on commit b249f72

Please sign in to comment.