From 58196dd43e5e29f589b4618d61d1c3fb50e5e41d Mon Sep 17 00:00:00 2001 From: InAnYan Date: Wed, 7 Aug 2024 14:02:33 +0300 Subject: [PATCH] Fixes from commit comments --- .../java/org/jabref/logic/ai/AiChatLogic.java | 8 ++++++-- .../jabref/preferences/JabRefPreferences.java | 20 ++++++++++--------- .../preferences/PreferencesService.java | 6 ++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/jabref/logic/ai/AiChatLogic.java b/src/main/java/org/jabref/logic/ai/AiChatLogic.java index 9e0678eaa2a..a6cebab0cd1 100644 --- a/src/main/java/org/jabref/logic/ai/AiChatLogic.java +++ b/src/main/java/org/jabref/logic/ai/AiChatLogic.java @@ -4,6 +4,7 @@ import org.jabref.logic.ai.chathistory.AiChatHistory; import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.CanonicalBibEntry; import org.jabref.model.entry.LinkedFile; import org.jabref.preferences.AiPreferences; @@ -115,9 +116,12 @@ private void rebuildChain() { } private void setSystemMessage(String systemMessage) { - String realSystemMessage = systemMessage + "\n\n" + "Here is the paper you are analyzing: " + entry.toString(); + String newSystemMessage = augmentSystemMessage(systemMessage); + chatMemory.add(new SystemMessage(newSystemMessage)); + } - chatMemory.add(new SystemMessage(realSystemMessage)); + private String augmentSystemMessage(String oldSystemMessage) { + return oldSystemMessage + "\n\n" + "Here is the paper you are analyzing: " + CanonicalBibEntry.getCanonicalRepresentation(entry); } public AiMessage execute(UserMessage message) { diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index ced41f98fe4..563ffaa6659 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -2784,15 +2784,17 @@ public AiPreferences getAiPreferences() { return aiPreferences; } + boolean aiEnabled = getBoolean(AI_ENABLED); + aiPreferences = new AiPreferences( - getBoolean(AI_ENABLED), + aiEnabled, AiPreferences.AiProvider.valueOf(get(AI_PROVIDER)), get(AI_OPEN_AI_CHAT_MODEL), get(AI_MISTRAL_AI_CHAT_MODEL), get(AI_HUGGING_FACE_CHAT_MODEL), - getBoolean(AI_ENABLED) ? getApiKeyForAiProvider(AiPreferences.AiProvider.OPEN_AI) : "", - getBoolean(AI_ENABLED) ? getApiKeyForAiProvider(AiPreferences.AiProvider.MISTRAL_AI) : "", - getBoolean(AI_ENABLED) ? getApiKeyForAiProvider(AiPreferences.AiProvider.HUGGING_FACE) : "", + aiEnabled ? getApiKeyForAiProvider(AiPreferences.AiProvider.OPEN_AI) : "", + aiEnabled ? getApiKeyForAiProvider(AiPreferences.AiProvider.MISTRAL_AI) : "", + aiEnabled ? getApiKeyForAiProvider(AiPreferences.AiProvider.HUGGING_FACE) : "", getBoolean(AI_CUSTOMIZE_SETTINGS), get(AI_OPEN_AI_API_BASE_URL), get(AI_MISTRAL_AI_API_BASE_URL), @@ -2815,19 +2817,19 @@ public AiPreferences getAiPreferences() { EasyBind.listen(aiPreferences.huggingFaceChatModelProperty(), (obs, oldValue, newValue) -> put(AI_HUGGING_FACE_CHAT_MODEL, newValue)); EasyBind.listen(aiPreferences.openAiApiTokenProperty(), (obs, oldValue, newValue) -> { - if (getBoolean(AI_ENABLED)) { + if (aiPreferences.getEnableAi()) { storeAiApiTokenInKeyring(AiPreferences.AiProvider.OPEN_AI, newValue); } }); EasyBind.listen(aiPreferences.mistralAiApiTokenProperty(), (obs, oldValue, newValue) -> { - if (getBoolean(AI_ENABLED)) { + if (aiPreferences.getEnableAi()) { storeAiApiTokenInKeyring(AiPreferences.AiProvider.MISTRAL_AI, newValue); } }); EasyBind.listen(aiPreferences.huggingFaceApiTokenProperty(), (obs, oldValue, newValue) -> { - if (getBoolean(AI_ENABLED)) { + if (aiPreferences.getEnableAi()) { storeAiApiTokenInKeyring(AiPreferences.AiProvider.HUGGING_FACE, newValue); } }); @@ -2840,12 +2842,12 @@ public AiPreferences getAiPreferences() { EasyBind.listen(aiPreferences.embeddingModelProperty(), (obs, oldValue, newValue) -> put(AI_EMBEDDING_MODEL, newValue.name())); EasyBind.listen(aiPreferences.instructionProperty(), (obs, oldValue, newValue) -> put(AI_SYSTEM_MESSAGE, newValue)); - EasyBind.listen(aiPreferences.temperatureProperty(), (obs, oldValue, newValue) -> putDouble(AI_TEMPERATURE, (double) newValue)); + EasyBind.listen(aiPreferences.temperatureProperty(), (obs, oldValue, newValue) -> putDouble(AI_TEMPERATURE, newValue.doubleValue())); EasyBind.listen(aiPreferences.contextWindowSizeProperty(), (obs, oldValue, newValue) -> putInt(AI_CONTEXT_WINDOW_SIZE, newValue)); EasyBind.listen(aiPreferences.documentSplitterChunkSizeProperty(), (obs, oldValue, newValue) -> putInt(AI_DOCUMENT_SPLITTER_CHUNK_SIZE, newValue)); EasyBind.listen(aiPreferences.documentSplitterOverlapSizeProperty(), (obs, oldValue, newValue) -> putInt(AI_DOCUMENT_SPLITTER_OVERLAP_SIZE, newValue)); EasyBind.listen(aiPreferences.ragMaxResultsCountProperty(), (obs, oldValue, newValue) -> putInt(AI_RAG_MAX_RESULTS_COUNT, newValue)); - EasyBind.listen(aiPreferences.ragMinScoreProperty(), (obs, oldValue, newValue) -> putDouble(AI_RAG_MIN_SCORE, (Double) newValue)); + EasyBind.listen(aiPreferences.ragMinScoreProperty(), (obs, oldValue, newValue) -> putDouble(AI_RAG_MIN_SCORE, newValue.doubleValue())); return aiPreferences; } diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index cd9798c596c..5e06ace333f 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -152,5 +152,11 @@ public interface PreferencesService { AiPreferences getAiPreferences(); + /** + * Retrieves the API key for the specified AI provider. + * + * @param provider the AI provider for which the API key is requested + * @return the API key for the specified AI provider, or empty string if no key is found + */ String getApiKeyForAiProvider(AiPreferences.AiProvider provider); }