Skip to content

Commit 90339a3

Browse files
aronbraunBlarc
authored andcommitted
feat(settings): add OpenAI temperature setting
Add new setting for OpenAI temperature in AppSettings with input validation and context help describing the functionality in the settings GUI. Use the temperature value from the settings in the OpenAIService for chat completion requests.
1 parent 0f8502f commit 90339a3

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [Unreleased]
44

5+
### Added
6+
- Add OpenAI temperature setting.
7+
58
## [1.1.0] - 2023-04-28
69

710
### Added

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ fun <T> createColumn(name: String, formatter: (T) -> String) : ColumnInfo<T, Str
1616
fun ValidationInfoBuilder.notBlank(value: String): ValidationInfo? =
1717
if (value.isBlank()) error(message("validation.required")) else null
1818

19+
20+
fun ValidationInfoBuilder.temperatureValid(value: String): ValidationInfo? {
21+
if (value.isNotBlank()) {
22+
value.toFloatOrNull().let {
23+
if (it != null && it in 0.0..2.0) {
24+
return null
25+
}
26+
}
27+
}
28+
return error(message("validation.temperature"))
29+
}
30+
1931
fun ValidationInfoBuilder.unique(value: String, existingValues: Set<String>): ValidationInfo? =
2032
if (existingValues.contains(value)) error(message("validation.unique")) else null
2133

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class OpenAIService {
3232
content = prompt
3333
)
3434
),
35-
temperature = 0.7,
35+
temperature = AppSettings.instance.openAITemperature.toDouble(),
3636
topP = 1.0,
3737
frequencyPenalty = 0.0,
3838
presencePenalty = 0.0,
@@ -42,7 +42,6 @@ class OpenAIService {
4242

4343
val completion: ChatCompletion = openAI.chatCompletion(chatCompletionRequest)
4444
return completion.choices[0].message!!.content
45-
4645
}
4746

4847
suspend fun refreshOpenAIModelIds() {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class AppSettings : PersistentStateComponent<AppSettings> {
4141

4242
var openAIModelId = "gpt-3.5-turbo"
4343
var openAIModelIds = listOf("gpt-3.5-turbo", "gpt-4")
44+
var openAITemperature = "0.7"
4445

4546
var appExclusions: Set<String> = setOf()
4647

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.github.blarc.ai.commits.intellij.plugin.AICommitsBundle.message
66
import com.github.blarc.ai.commits.intellij.plugin.OpenAIService
77
import com.github.blarc.ai.commits.intellij.plugin.settings.prompt.Prompt
88
import com.github.blarc.ai.commits.intellij.plugin.settings.prompt.PromptTable
9+
import com.github.blarc.ai.commits.intellij.plugin.temperatureValid
910
import com.intellij.icons.AllIcons
1011
import com.intellij.openapi.options.BoundConfigurable
1112
import com.intellij.openapi.progress.runBackgroundableTask
@@ -106,6 +107,25 @@ class AppSettingsConfigurable : BoundConfigurable(message("settings.general.grou
106107
.align(AlignX.RIGHT)
107108
.widthGroup("button")
108109
}
110+
111+
row {
112+
label(message("settings.openAITemperature"))
113+
.widthGroup("label")
114+
115+
textField()
116+
.bindText(AppSettings.instance::openAITemperature)
117+
.applyToComponent { minimumWidth = 400 }
118+
.resizableColumn()
119+
.widthGroup("input")
120+
.validationOnInput { temperatureValid(it.text) }
121+
122+
contextHelp(message("settings.openAITemperatureComment"))
123+
}
124+
125+
row {
126+
cell(verifyLabel)
127+
.align(AlignX.RIGHT)
128+
}
109129
}
110130

111131
group(JBLabel("Prompt")) {

src/main/resources/messages/MyBundle.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ settings.openAIToken=OpenAI token
66
settings.locale=Locale
77
settings.prompt=Prompt
88
settings.openAIModel=OpenAI model
9+
settings.openAITemperature=OpenAI temperature
910
settings.openAITokenComment=\
1011
<p>You can get your token <a href="https://platform.openai.com/account/api-keys">here.</a/></p>
12+
settings.openAITemperatureComment=What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make \
13+
the output more random,while lower values like 0.2 will make it more focused and deterministic.
1114
settings.report-bug=Report bug
1215
settings.verifyToken=Verify
1316
settings.verify.valid=Open AI configuration is valid.
@@ -41,6 +44,7 @@ settings.prompt.name=Name
4144
settings.prompt.content=Content
4245
validation.required=This value is required.
4346
validation.number=Value is not a number.
47+
validation.temperature=Temperature should be between 0 and 2.
4448
settings.prompt.comment=You can use {locale}, {diff}, {branch} variables to customise your prompt.
4549
actions.update=Update
4650
actions.add=Add

0 commit comments

Comments
 (0)