11package org .jabref .gui .preferences .ai ;
22
3+ import java .util .Arrays ;
34import java .util .List ;
45import java .util .Locale ;
6+ import java .util .Map ;
57import java .util .Objects ;
68
79import javafx .beans .property .BooleanProperty ;
2022import org .jabref .gui .preferences .PreferenceTabViewModel ;
2123import org .jabref .logic .ai .AiDefaultPreferences ;
2224import org .jabref .logic .ai .AiPreferences ;
25+ import org .jabref .logic .ai .templates .AiTemplate ;
2326import org .jabref .logic .l10n .Localization ;
2427import org .jabref .logic .preferences .CliPreferences ;
2528import org .jabref .logic .util .LocalizedNumbers ;
@@ -79,7 +82,13 @@ public class AiTabViewModel implements PreferenceTabViewModel {
7982 private final StringProperty huggingFaceApiBaseUrl = new SimpleStringProperty ();
8083 private final StringProperty gpt4AllApiBaseUrl = new SimpleStringProperty ();
8184
82- private final StringProperty instruction = new SimpleStringProperty ();
85+ private final Map <AiTemplate , StringProperty > templateSources = Map .of (
86+ AiTemplate .CHATTING_SYSTEM_MESSAGE , new SimpleStringProperty (),
87+ AiTemplate .CHATTING_USER_MESSAGE , new SimpleStringProperty (),
88+ AiTemplate .SUMMARIZATION_CHUNK , new SimpleStringProperty (),
89+ AiTemplate .SUMMARIZATION_COMBINE , new SimpleStringProperty ()
90+ );
91+
8392 private final StringProperty temperature = new SimpleStringProperty ();
8493 private final IntegerProperty contextWindowSize = new SimpleIntegerProperty ();
8594 private final IntegerProperty documentSplitterChunkSize = new SimpleIntegerProperty ();
@@ -96,7 +105,6 @@ public class AiTabViewModel implements PreferenceTabViewModel {
96105 private final Validator chatModelValidator ;
97106 private final Validator apiBaseUrlValidator ;
98107 private final Validator embeddingModelValidator ;
99- private final Validator instructionValidator ;
100108 private final Validator temperatureTypeValidator ;
101109 private final Validator temperatureRangeValidator ;
102110 private final Validator contextWindowSizeValidator ;
@@ -242,11 +250,6 @@ public AiTabViewModel(CliPreferences preferences) {
242250 Objects ::nonNull ,
243251 ValidationMessage .error (Localization .lang ("Embedding model has to be provided" )));
244252
245- this .instructionValidator = new FunctionBasedValidator <>(
246- instruction ,
247- message -> !StringUtil .isBlank (message ),
248- ValidationMessage .error (Localization .lang ("The instruction has to be provided" )));
249-
250253 this .temperatureTypeValidator = new FunctionBasedValidator <>(
251254 temperature ,
252255 temp -> LocalizedNumbers .stringToDouble (temp ).isPresent (),
@@ -318,7 +321,10 @@ public void setValues() {
318321 customizeExpertSettings .setValue (aiPreferences .getCustomizeExpertSettings ());
319322
320323 selectedEmbeddingModel .setValue (aiPreferences .getEmbeddingModel ());
321- instruction .setValue (aiPreferences .getInstruction ());
324+
325+ Arrays .stream (AiTemplate .values ()).forEach (template ->
326+ templateSources .get (template ).set (aiPreferences .getTemplate (template )));
327+
322328 temperature .setValue (LocalizedNumbers .doubleToString (aiPreferences .getTemperature ()));
323329 contextWindowSize .setValue (aiPreferences .getContextWindowSize ());
324330 documentSplitterChunkSize .setValue (aiPreferences .getDocumentSplitterChunkSize ());
@@ -359,7 +365,9 @@ public void storeSettings() {
359365 aiPreferences .setHuggingFaceApiBaseUrl (huggingFaceApiBaseUrl .get () == null ? "" : huggingFaceApiBaseUrl .get ());
360366 aiPreferences .setGpt4AllApiBaseUrl (gpt4AllApiBaseUrl .get () == null ? "" : gpt4AllApiBaseUrl .get ());
361367
362- aiPreferences .setInstruction (instruction .get ());
368+ Arrays .stream (AiTemplate .values ()).forEach (template ->
369+ aiPreferences .setTemplate (template , templateSources .get (template ).get ()));
370+
363371 // We already check the correctness of temperature and RAG minimum score in validators, so we don't need to check it here.
364372 aiPreferences .setTemperature (LocalizedNumbers .stringToDouble (oldLocale , temperature .get ()).get ());
365373 aiPreferences .setContextWindowSize (contextWindowSize .get ());
@@ -373,8 +381,6 @@ public void resetExpertSettings() {
373381 String resetApiBaseUrl = selectedAiProvider .get ().getApiUrl ();
374382 currentApiBaseUrl .set (resetApiBaseUrl );
375383
376- instruction .set (AiDefaultPreferences .SYSTEM_MESSAGE );
377-
378384 contextWindowSize .set (AiDefaultPreferences .getContextWindowSize (selectedAiProvider .get (), currentChatModel .get ()));
379385
380386 temperature .set (LocalizedNumbers .doubleToString (AiDefaultPreferences .TEMPERATURE ));
@@ -384,6 +390,11 @@ public void resetExpertSettings() {
384390 ragMinScore .set (LocalizedNumbers .doubleToString (AiDefaultPreferences .RAG_MIN_SCORE ));
385391 }
386392
393+ public void resetTemplates () {
394+ Arrays .stream (AiTemplate .values ()).forEach (template ->
395+ templateSources .get (template ).set (AiDefaultPreferences .TEMPLATES .get (template )));
396+ }
397+
387398 @ Override
388399 public boolean validateSettings () {
389400 if (enableAi .get ()) {
@@ -410,7 +421,6 @@ public boolean validateExpertSettings() {
410421 List <Validator > validators = List .of (
411422 apiBaseUrlValidator ,
412423 embeddingModelValidator ,
413- instructionValidator ,
414424 temperatureTypeValidator ,
415425 temperatureRangeValidator ,
416426 contextWindowSizeValidator ,
@@ -484,8 +494,8 @@ public BooleanProperty disableApiBaseUrlProperty() {
484494 return disableApiBaseUrl ;
485495 }
486496
487- public StringProperty instructionProperty () {
488- return instruction ;
497+ public Map < AiTemplate , StringProperty > getTemplateSources () {
498+ return templateSources ;
489499 }
490500
491501 public StringProperty temperatureProperty () {
@@ -536,10 +546,6 @@ public ValidationStatus getEmbeddingModelValidationStatus() {
536546 return embeddingModelValidator .getValidationStatus ();
537547 }
538548
539- public ValidationStatus getSystemMessageValidationStatus () {
540- return instructionValidator .getValidationStatus ();
541- }
542-
543549 public ValidationStatus getTemperatureTypeValidationStatus () {
544550 return temperatureTypeValidator .getValidationStatus ();
545551 }
0 commit comments