Skip to content

Commit bc30d1a

Browse files
committed
Add temperature parameter
1 parent 70e29fc commit bc30d1a

File tree

6 files changed

+66
-3
lines changed

6 files changed

+66
-3
lines changed

src/main/java/org/jabref/gui/preferences/ai/AiTab.fxml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
<Label alignment="BASELINE_CENTER" text="%Message window size" />
4343
<IntegerInputField fx:id="messageWindowSizeTextField" HBox.hgrow="ALWAYS" />
4444
</children>
45+
</HBox>
46+
<HBox alignment="CENTER_LEFT" layoutX="10.0" layoutY="448.0" spacing="10.0">
47+
<children>
48+
<Label alignment="BASELINE_CENTER" text="%Temperature" />
49+
<DoubleInputField fx:id="temperatureTextField" HBox.hgrow="ALWAYS"/>
50+
</children>
4551
</HBox>
4652
<VBox>
4753
<children>

src/main/java/org/jabref/gui/preferences/ai/AiTab.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
import com.dlsc.unitfx.DoubleInputField;
1717
import com.dlsc.unitfx.IntegerInputField;
1818
import de.saxsys.mvvmfx.utils.validation.visualization.ControlsFxVisualizer;
19-
import scala.Int;
2019

2120
public class AiTab extends AbstractPreferenceTabView<AiTabViewModel> implements PreferencesTab {
2221
@FXML private CheckBox enableChat;
2322
@FXML private TextField openAiToken;
2423
@FXML private ComboBox<AiPreferences.AiModel> modelComboBox;
2524

2625
@FXML private TextArea systemMessageTextArea;
26+
@FXML private DoubleInputField temperatureTextField;
2727
@FXML private IntegerInputField messageWindowSizeTextField;
2828
@FXML private IntegerInputField documentSplitterChunkSizeTextField;
2929
@FXML private IntegerInputField documentSplitterOverlapSizeTextField;
@@ -47,6 +47,7 @@ public void initialize() {
4747
openAiToken.textProperty().bindBidirectional(viewModel.openAiTokenProperty());
4848
modelComboBox.valueProperty().bindBidirectional(viewModel.modelProperty());
4949
systemMessageTextArea.textProperty().bindBidirectional(viewModel.systemMessageProperty());
50+
temperatureTextField.valueProperty().bindBidirectional(viewModel.temperatureProperty().asObject());
5051
messageWindowSizeTextField.valueProperty().bindBidirectional(viewModel.messageWindowSizeProperty().asObject());
5152
documentSplitterChunkSizeTextField.valueProperty().bindBidirectional(viewModel.documentSplitterChunkSizeProperty().asObject());
5253
documentSplitterOverlapSizeTextField.valueProperty().bindBidirectional(viewModel.documentSplitterOverlapSizeProperty().asObject());
@@ -56,6 +57,7 @@ public void initialize() {
5657
openAiToken.setDisable(!enableChat.isSelected());
5758
modelComboBox.setDisable(!enableChat.isSelected());
5859
systemMessageTextArea.setDisable(!enableChat.isSelected());
60+
temperatureTextField.setDisable(!enableChat.isSelected());
5961
messageWindowSizeTextField.setDisable(!enableChat.isSelected());
6062
documentSplitterChunkSizeTextField.setDisable(!enableChat.isSelected());
6163
documentSplitterOverlapSizeTextField.setDisable(!enableChat.isSelected());
@@ -67,6 +69,7 @@ public void initialize() {
6769
modelComboBox.setDisable(!newValue);
6870

6971
systemMessageTextArea.setDisable(!newValue);
72+
temperatureTextField.setDisable(!newValue);
7073
messageWindowSizeTextField.setDisable(!newValue);
7174
documentSplitterChunkSizeTextField.setDisable(!newValue);
7275
documentSplitterOverlapSizeTextField.setDisable(!newValue);
@@ -77,6 +80,7 @@ public void initialize() {
7780
Platform.runLater(() -> {
7881
visualizer.initVisualization(viewModel.getOpenAiTokenValidatorStatus(), openAiToken);
7982
visualizer.initVisualization(viewModel.getSystemMessageValidatorStatus(), systemMessageTextArea);
83+
visualizer.initVisualization(viewModel.getTemperatureValidatorStatus(), temperatureTextField);
8084
visualizer.initVisualization(viewModel.getMessageWindowSizeValidatorStatus(), messageWindowSizeTextField);
8185
visualizer.initVisualization(viewModel.getDocumentSplitterChunkSizeValidatorStatus(), documentSplitterChunkSizeTextField);
8286
visualizer.initVisualization(viewModel.getDocumentSplitterOverlapSizeValidatorStatus(), documentSplitterOverlapSizeTextField);

src/main/java/org/jabref/gui/preferences/ai/AiTabViewModel.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.jabref.gui.preferences.ai;
22

3+
import java.util.Arrays;
4+
35
import javafx.beans.property.BooleanProperty;
46
import javafx.beans.property.DoubleProperty;
57
import javafx.beans.property.FloatProperty;
@@ -32,6 +34,7 @@ public class AiTabViewModel implements PreferenceTabViewModel {
3234
private final ObjectProperty<AiPreferences.AiModel> modelProperty = new SimpleObjectProperty<>();
3335

3436
private final StringProperty systemMessage = new SimpleStringProperty();
37+
private final DoubleProperty temperature = new SimpleDoubleProperty();
3538
private final IntegerProperty messageWindowSize = new SimpleIntegerProperty();
3639
private final IntegerProperty documentSplitterChunkSize = new SimpleIntegerProperty();
3740
private final IntegerProperty documentSplitterOverlapSize = new SimpleIntegerProperty();
@@ -42,6 +45,7 @@ public class AiTabViewModel implements PreferenceTabViewModel {
4245

4346
private final Validator openAiTokenValidator;
4447
private final Validator systemMessageValidator;
48+
private final Validator temperatureValidator;
4549
private final Validator messageWindowSizeValidator;
4650
private final Validator documentSplitterChunkSizeValidator;
4751
private final Validator documentSplitterOverlapSizeValidator;
@@ -61,6 +65,11 @@ public AiTabViewModel(PreferencesService preferencesService) {
6165
(message) -> !StringUtil.isBlank(message),
6266
ValidationMessage.error(Localization.lang("The system message cannot be empty")));
6367

68+
this.temperatureValidator = new FunctionBasedValidator<>(
69+
temperature,
70+
(temp) -> (double)temp >= 0 && (double)temp <= 2,
71+
ValidationMessage.error(Localization.lang("Temperature must be between 0 and 2")));
72+
6473
this.messageWindowSizeValidator = new FunctionBasedValidator<>(
6574
messageWindowSize,
6675
(size) -> (int)size > 0,
@@ -94,6 +103,7 @@ public void setValues() {
94103
modelProperty.setValue(aiPreferences.getModel());
95104

96105
systemMessage.setValue(aiPreferences.getSystemMessage());
106+
temperature.setValue(aiPreferences.getTemperature());
97107
messageWindowSize.setValue(aiPreferences.getMessageWindowSize());
98108
documentSplitterChunkSize.setValue(aiPreferences.getDocumentSplitterChunkSize());
99109
documentSplitterOverlapSize.setValue(aiPreferences.getDocumentSplitterOverlapSize());
@@ -108,6 +118,7 @@ public void storeSettings() {
108118
aiPreferences.setModel(modelProperty.get());
109119

110120
aiPreferences.setSystemMessage(systemMessage.get());
121+
aiPreferences.setTemperature(temperature.get());
111122
aiPreferences.setMessageWindowSize(messageWindowSize.get());
112123
aiPreferences.setDocumentSplitterChunkSize(documentSplitterChunkSize.get());
113124
aiPreferences.setDocumentSplitterOverlapSize(documentSplitterOverlapSize.get());
@@ -117,7 +128,18 @@ public void storeSettings() {
117128

118129
@Override
119130
public boolean validateSettings() {
120-
return openAiTokenValidator.getValidationStatus().isValid() && systemMessageValidator.getValidationStatus().isValid() && messageWindowSizeValidator.getValidationStatus().isValid() && documentSplitterChunkSizeValidator.getValidationStatus().isValid() && documentSplitterOverlapSizeValidator.getValidationStatus().isValid() && ragMaxResultsCountValidator.getValidationStatus().isValid() && ragMinScoreValidator.getValidationStatus().isValid();
131+
Validator[] validators = {
132+
openAiTokenValidator,
133+
systemMessageValidator,
134+
temperatureValidator,
135+
messageWindowSizeValidator,
136+
documentSplitterChunkSizeValidator,
137+
documentSplitterOverlapSizeValidator,
138+
ragMaxResultsCountValidator,
139+
ragMinScoreValidator
140+
};
141+
142+
return Arrays.stream(validators).map(Validator::getValidationStatus).allMatch(ValidationStatus::isValid);
121143
}
122144

123145
public StringProperty openAiTokenProperty() {
@@ -136,6 +158,10 @@ public StringProperty systemMessageProperty() {
136158
return systemMessage;
137159
}
138160

161+
public DoubleProperty temperatureProperty() {
162+
return temperature;
163+
}
164+
139165
public IntegerProperty messageWindowSizeProperty() {
140166
return messageWindowSize;
141167
}
@@ -164,6 +190,10 @@ public ValidationStatus getSystemMessageValidatorStatus() {
164190
return systemMessageValidator.getValidationStatus();
165191
}
166192

193+
public ValidationStatus getTemperatureValidatorStatus() {
194+
return temperatureValidator.getValidationStatus();
195+
}
196+
167197
public ValidationStatus getMessageWindowSizeValidatorStatus() {
168198
return messageWindowSizeValidator.getValidationStatus();
169199
}

src/main/java/org/jabref/logic/ai/AiService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ private void bindToPreferences() {
122122
EasyBind.listen(aiPreferences.modelProperty(), (property, oldValue, newValue) -> {
123123
rebuildChatModel();
124124
});
125+
126+
EasyBind.listen(aiPreferences.temperatureProperty(), (property, oldValue, newValue) -> {
127+
rebuildChatModel();
128+
});
125129
}
126130

127131
public void startIngestingFile(String path) {
@@ -195,6 +199,7 @@ private void rebuildChatModel() {
195199
.builder()
196200
.apiKey(aiPreferences.getOpenAiToken())
197201
.modelName(aiPreferences.getModel().getName())
202+
.temperature(aiPreferences.getTemperature())
198203
.logRequests(true)
199204
.logResponses(true)
200205
.build();

src/main/java/org/jabref/preferences/AiPreferences.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,20 @@ public String getName() {
5454
private final ObjectProperty<AiModel> model;
5555

5656
private final StringProperty systemMessage;
57+
private final DoubleProperty temperature;
5758
private final IntegerProperty messageWindowSize;
5859
private final IntegerProperty documentSplitterChunkSize;
5960
private final IntegerProperty documentSplitterOverlapSize;
6061
private final IntegerProperty ragMaxResultsCount;
6162
private final DoubleProperty ragMinScore;
6263

63-
public AiPreferences(boolean enableChatWithFiles, String openAiToken, AiModel aiModel, String systemMessage, int messageWindowSize, int documentSplitterChunkSize, int documentSplitterOverlapSize, int ragMaxResultsCount, double ragMinScore) {
64+
public AiPreferences(boolean enableChatWithFiles, String openAiToken, AiModel aiModel, String systemMessage, double temperature, int messageWindowSize, int documentSplitterChunkSize, int documentSplitterOverlapSize, int ragMaxResultsCount, double ragMinScore) {
6465
this.enableChatWithFiles = new SimpleBooleanProperty(enableChatWithFiles);
6566
this.openAiToken = new SimpleStringProperty(openAiToken);
6667
this.model = new SimpleObjectProperty<>(aiModel);
6768

6869
this.systemMessage = new SimpleStringProperty(systemMessage);
70+
this.temperature = new SimpleDoubleProperty(temperature);
6971
this.messageWindowSize = new SimpleIntegerProperty(messageWindowSize);
7072
this.documentSplitterChunkSize = new SimpleIntegerProperty(documentSplitterChunkSize);
7173
this.documentSplitterOverlapSize = new SimpleIntegerProperty(documentSplitterOverlapSize);
@@ -121,6 +123,18 @@ public void setSystemMessage(String systemMessage) {
121123
this.systemMessage.set(systemMessage);
122124
}
123125

126+
public DoubleProperty temperatureProperty() {
127+
return temperature;
128+
}
129+
130+
public double getTemperature() {
131+
return temperature.get();
132+
}
133+
134+
public void setTemperature(double temperature) {
135+
this.temperature.set(temperature);
136+
}
137+
124138
public IntegerProperty messageWindowSizeProperty() {
125139
return messageWindowSize;
126140
}

src/main/java/org/jabref/preferences/JabRefPreferences.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ public class JabRefPreferences implements PreferencesService {
457457
private static final String AI_ENABLE_CHAT = "aiEnableChat";
458458
private static final String AI_MODEL = "aiModel";
459459
private static final String AI_SYSTEM_MESSAGE = "aiSystemMessage";
460+
private static final String AI_TEMPERATURE = "aiTemperature";
460461
private static final String AI_MESSAGE_WINDOW_SIZE = "aiMessageWindowSize";
461462
private static final String AI_DOCUMENT_SPLITTER_CHUNK_SIZE = "aiDocumentSplitterChunkSize";
462463
private static final String AI_DOCUMENT_SPLITTER_OVERLAP_SIZE = "aiDocumentSplitterOverlapSize";
@@ -853,6 +854,7 @@ private JabRefPreferences() {
853854
defaults.put(AI_ENABLE_CHAT, Boolean.FALSE);
854855
defaults.put(AI_MODEL, AiPreferences.AiModel.GPT_3_5_TURBO.getName());
855856
defaults.put(AI_SYSTEM_MESSAGE, "You are an AI assistant.");
857+
defaults.put(AI_TEMPERATURE, 0.7);
856858
defaults.put(AI_MESSAGE_WINDOW_SIZE, 10);
857859
defaults.put(AI_DOCUMENT_SPLITTER_CHUNK_SIZE, 300);
858860
defaults.put(AI_DOCUMENT_SPLITTER_OVERLAP_SIZE, 100);
@@ -2769,6 +2771,7 @@ public AiPreferences getAiPreferences() {
27692771
token,
27702772
AiPreferences.AiModel.fromString(get(AI_MODEL)),
27712773
get(AI_SYSTEM_MESSAGE),
2774+
getDouble(AI_TEMPERATURE),
27722775
getInt(AI_MESSAGE_WINDOW_SIZE),
27732776
getInt(AI_DOCUMENT_SPLITTER_CHUNK_SIZE),
27742777
getInt(AI_DOCUMENT_SPLITTER_OVERLAP_SIZE),
@@ -2780,6 +2783,7 @@ public AiPreferences getAiPreferences() {
27802783
EasyBind.listen(aiPreferences.modelProperty(), (obs, oldValue, newValue) -> put(AI_MODEL, aiPreferences.getModel().getName()));
27812784

27822785
EasyBind.listen(aiPreferences.systemMessageProperty(), (obs, oldValue, newValue) -> put(AI_SYSTEM_MESSAGE, newValue));
2786+
EasyBind.listen(aiPreferences.temperatureProperty(), (obs, oldValue, newValue) -> putDouble(AI_TEMPERATURE, (double)newValue));
27832787
EasyBind.listen(aiPreferences.messageWindowSizeProperty(), (obs, oldValue, newValue) -> putInt(AI_MESSAGE_WINDOW_SIZE, newValue));
27842788
EasyBind.listen(aiPreferences.documentSplitterChunkSizeProperty(), (obs, oldValue, newValue) -> putInt(AI_DOCUMENT_SPLITTER_CHUNK_SIZE, newValue));
27852789
EasyBind.listen(aiPreferences.documentSplitterOverlapSizeProperty(), (obs, oldValue, newValue) -> putInt(AI_DOCUMENT_SPLITTER_OVERLAP_SIZE, newValue));

0 commit comments

Comments
 (0)