Skip to content

Commit

Permalink
Fix checkstyle and localization. And refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Aug 9, 2024
1 parent 4cfeef0 commit a5ef15f
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private void initialize() {
}

default ->
LOGGER.warn("ChatMessageComponent supports only user or AI messages, but other type was passed: {}", chatMessage.type().name());
LOGGER.warn("ChatMessageComponent supports only user, AI, or error messages, but other type was passed: {}", chatMessage.type().name());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import org.jabref.gui.DialogService;
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.preferences.ai.AiPreferences;
import org.jabref.preferences.FilePreferences;
import org.jabref.preferences.ai.AiPreferences;

import com.airhacks.afterburner.views.ViewLoader;
import org.slf4j.Logger;
Expand Down Expand Up @@ -95,7 +95,7 @@ private void openBrowser(String link) {
try {
JabRefDesktop.openBrowser(link, filePreferences);
} catch (IOException e) {
LOGGER.error("Error opening the browser to AI provider's privacy policy page.", e);
LOGGER.error("Error opening the browser to the Privacy Policy page of the AI provider.", e);
dialogService.showErrorDialogAndWait(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package org.jabref.gui.ai.components.summary;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Calendar;
import java.util.Locale;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/entryeditor/AiSummaryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private void showErrorNoFiles() {
setContent(
new ErrorStateComponent(
Localization.lang("Unable to chat"),
Localization.lang("Please attach at least one PDF file to enable chatting with PDF files.")
Localization.lang("Please attach at least one PDF file to enable chatting with PDF file(s).")
)
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/jabref/gui/preferences/ai/AiTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.help.HelpFile;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.ai.AiPreferences;
import org.jabref.preferences.ai.AiProvider;
import org.jabref.preferences.ai.EmbeddingModel;

import com.airhacks.afterburner.views.ViewLoader;
Expand All @@ -30,7 +30,7 @@ public class AiTab extends AbstractPreferenceTabView<AiTabViewModel> implements

@FXML private CheckBox enableAi;

@FXML private ComboBox<AiPreferences.AiProvider> aiProviderComboBox;
@FXML private ComboBox<AiProvider> aiProviderComboBox;
@FXML private ComboBox<String> chatModelComboBox;
@FXML private CustomPasswordField apiKeyTextField;

Expand Down Expand Up @@ -72,8 +72,8 @@ public void initialize() {

enableAi.selectedProperty().bindBidirectional(viewModel.enableAi());

new ViewModelListCellFactory<AiPreferences.AiProvider>()
.withText(AiPreferences.AiProvider::toString)
new ViewModelListCellFactory<AiProvider>()
.withText(AiProvider::toString)
.install(aiProviderComboBox);
aiProviderComboBox.setItems(viewModel.aiProvidersProperty());
aiProviderComboBox.valueProperty().bindBidirectional(viewModel.selectedAiProviderProperty());
Expand All @@ -87,7 +87,7 @@ public void initialize() {
chatModelComboBox.disableProperty().bind(viewModel.disableBasicSettingsProperty());

this.aiProviderComboBox.valueProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == AiPreferences.AiProvider.HUGGING_FACE) {
if (newValue == AiProvider.HUGGING_FACE) {
chatModelComboBox.setPromptText(HUGGING_FACE_CHAT_MODEL_PROMPT);
}
});
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/org/jabref/gui/preferences/ai/AiTabViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
import org.jabref.logic.ai.AiDefaultPreferences;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.strings.StringUtil;
import org.jabref.preferences.ai.AiPreferences;
import org.jabref.preferences.PreferencesService;
import org.jabref.preferences.ai.AiPreferences;
import org.jabref.preferences.ai.AiProvider;
import org.jabref.preferences.ai.EmbeddingModel;

import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator;
Expand All @@ -34,9 +35,9 @@
public class AiTabViewModel implements PreferenceTabViewModel {
private final BooleanProperty enableAi = new SimpleBooleanProperty();

private final ListProperty<AiPreferences.AiProvider> aiProvidersList =
new SimpleListProperty<>(FXCollections.observableArrayList(AiPreferences.AiProvider.values()));
private final ObjectProperty<AiPreferences.AiProvider> selectedAiProvider = new SimpleObjectProperty<>();
private final ListProperty<AiProvider> aiProvidersList =
new SimpleListProperty<>(FXCollections.observableArrayList(AiProvider.values()));
private final ObjectProperty<AiProvider> selectedAiProvider = new SimpleObjectProperty<>();

private final ListProperty<String> chatModelsList =
new SimpleListProperty<>(FXCollections.observableArrayList());
Expand Down Expand Up @@ -98,9 +99,9 @@ public AiTabViewModel(PreferencesService preferencesService) {
disableExpertSettings.set(!newValue || !customizeExpertSettings.get());

if (newValue) {
openAiApiKey.set(preferencesService.getApiKeyForAiProvider(AiPreferences.AiProvider.OPEN_AI));
mistralAiApiKey.set(preferencesService.getApiKeyForAiProvider(AiPreferences.AiProvider.MISTRAL_AI));
huggingFaceApiKey.set(preferencesService.getApiKeyForAiProvider(AiPreferences.AiProvider.HUGGING_FACE));
openAiApiKey.set(preferencesService.getApiKeyForAiProvider(AiProvider.OPEN_AI));
mistralAiApiKey.set(preferencesService.getApiKeyForAiProvider(AiProvider.MISTRAL_AI));
huggingFaceApiKey.set(preferencesService.getApiKeyForAiProvider(AiProvider.HUGGING_FACE));

if (selectedAiProvider.get() != null) {
switch (selectedAiProvider.get()) {
Expand Down Expand Up @@ -133,7 +134,7 @@ public AiTabViewModel(PreferencesService preferencesService) {
String oldChatModel = currentChatModel.get();
chatModelsList.setAll(models);

disableApiBaseUrl.set(newValue == AiPreferences.AiProvider.HUGGING_FACE);
disableApiBaseUrl.set(newValue == AiProvider.HUGGING_FACE);

if (oldValue != null) {
switch (oldValue) {
Expand Down Expand Up @@ -385,11 +386,11 @@ public boolean getEnableAi() {
return enableAi.get();
}

public ReadOnlyListProperty<AiPreferences.AiProvider> aiProvidersProperty() {
public ReadOnlyListProperty<AiProvider> aiProvidersProperty() {
return aiProvidersList;
}

public ObjectProperty<AiPreferences.AiProvider> selectedAiProviderProperty() {
public ObjectProperty<AiProvider> selectedAiProviderProperty() {
return selectedAiProvider;
}

Expand Down
36 changes: 18 additions & 18 deletions src/main/java/org/jabref/logic/ai/AiDefaultPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
import java.util.List;
import java.util.Map;

import org.jabref.preferences.ai.AiPreferences;
import org.jabref.preferences.ai.AiProvider;
import org.jabref.preferences.ai.EmbeddingModel;

public class AiDefaultPreferences {
public static final Map<AiPreferences.AiProvider, List<String>> AVAILABLE_CHAT_MODELS = Map.of(
AiPreferences.AiProvider.OPEN_AI, List.of("gpt-4o-mini", "gpt-4o", "gpt-4", "gpt-4-turbo", "gpt-3.5-turbo"),
public static final Map<AiProvider, List<String>> AVAILABLE_CHAT_MODELS = Map.of(
AiProvider.OPEN_AI, List.of("gpt-4o-mini", "gpt-4o", "gpt-4", "gpt-4-turbo", "gpt-3.5-turbo"),
// "mistral" and "mixtral" are not language mistakes.
AiPreferences.AiProvider.MISTRAL_AI, List.of("open-mistral-nemo", "open-mistral-7b", "open-mixtral-8x7b", "open-mixtral-8x22b", "mistral-large-latest"),
AiPreferences.AiProvider.HUGGING_FACE, List.of()
AiProvider.MISTRAL_AI, List.of("open-mistral-nemo", "open-mistral-7b", "open-mixtral-8x7b", "open-mixtral-8x22b", "mistral-large-latest"),
AiProvider.HUGGING_FACE, List.of()
);

public static final Map<AiPreferences.AiProvider, String> PROVIDERS_API_URLS = Map.of(
AiPreferences.AiProvider.OPEN_AI, "https://api.openai.com/v1",
AiPreferences.AiProvider.MISTRAL_AI, "https://api.mistral.ai/v1",
AiPreferences.AiProvider.HUGGING_FACE, "https://huggingface.co/api"
public static final Map<AiProvider, String> PROVIDERS_API_URLS = Map.of(
AiProvider.OPEN_AI, "https://api.openai.com/v1",
AiProvider.MISTRAL_AI, "https://api.mistral.ai/v1",
AiProvider.HUGGING_FACE, "https://huggingface.co/api"
);

public static final Map<AiPreferences.AiProvider, Map<String, Integer>> CONTEXT_WINDOW_SIZES = Map.of(
AiPreferences.AiProvider.OPEN_AI, Map.of(
public static final Map<AiProvider, Map<String, Integer>> CONTEXT_WINDOW_SIZES = Map.of(
AiProvider.OPEN_AI, Map.of(
"gpt-4o-mini", 128000,
"gpt-4o", 128000,
"gpt-4", 8192,
"gpt-4-turbo", 128000,
"gpt-3.5-turbo", 16385
),
AiPreferences.AiProvider.MISTRAL_AI, Map.of(
AiProvider.MISTRAL_AI, Map.of(
"mistral-large-latest", 128000,
"open-mistral-nemo", 128000,
"open-mistral-7b", 32000,
Expand All @@ -39,12 +39,12 @@ public class AiDefaultPreferences {

public static final boolean ENABLE_CHAT = false;

public static final AiPreferences.AiProvider PROVIDER = AiPreferences.AiProvider.OPEN_AI;
public static final AiProvider PROVIDER = AiProvider.OPEN_AI;

public static final Map<AiPreferences.AiProvider, String> CHAT_MODELS = Map.of(
AiPreferences.AiProvider.OPEN_AI, "gpt-4o-mini",
AiPreferences.AiProvider.MISTRAL_AI, "open-mixtral-8x22b",
AiPreferences.AiProvider.HUGGING_FACE, ""
public static final Map<AiProvider, String> CHAT_MODELS = Map.of(
AiProvider.OPEN_AI, "gpt-4o-mini",
AiProvider.MISTRAL_AI, "open-mixtral-8x22b",
AiProvider.HUGGING_FACE, ""
);

public static final boolean CUSTOMIZE_SETTINGS = false;
Expand All @@ -59,7 +59,7 @@ public class AiDefaultPreferences {

public static final int CONTEXT_WINDOW_SIZE = 8196;

public static int getContextWindowSize(AiPreferences.AiProvider aiProvider, String model) {
public static int getContextWindowSize(AiProvider aiProvider, String model) {
return CONTEXT_WINDOW_SIZES.getOrDefault(aiProvider, Map.of()).getOrDefault(model, 0);
}
}
7 changes: 4 additions & 3 deletions src/main/java/org/jabref/logic/ai/AiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
import org.jabref.gui.desktop.JabRefDesktop;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.ai.chathistory.BibDatabaseChatHistoryManager;
import org.jabref.logic.ai.models.JabRefEmbeddingModel;
import org.jabref.logic.ai.models.JabRefChatLanguageModel;
import org.jabref.logic.ai.models.JabRefEmbeddingModel;
import org.jabref.logic.ai.summarization.SummariesStorage;
import org.jabref.logic.l10n.Localization;
import org.jabref.preferences.ai.AiPreferences;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
Expand Down Expand Up @@ -66,8 +67,8 @@ public AiService(AiPreferences aiPreferences, DialogService dialogService, TaskE

mvStore = MVStore.open(mvStorePath.toString());
} catch (Exception e) {
LOGGER.error("An error occurred while creating directories for storing chat history. Chat history won't be remembered in next session", e);
dialogService.notify("An error occurred while creating directories for storing chat history. Chat history won't be remembered in next session");
LOGGER.error("An error occurred while creating directories for AI cache and chat history. Chat history will not be remembered in next session", e);
dialogService.notify(Localization.lang("An error occurred while creating directories for AI cache and chat history. Chat history will not be remembered in next session"));
mvStore = MVStore.open(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.IntStream;

Expand All @@ -18,7 +17,6 @@
import com.google.common.eventbus.Subscribe;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ChatMessageType;
import dev.langchain4j.data.message.UserMessage;
import org.h2.mvstore.MVStore;
import org.slf4j.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static Optional<Document> fromFile(Path path) {
if (FileUtil.isPDFFile(path)) {
return FileToDocument.fromPdfFile(path);
} else {
LOGGER.info("Unsupported file type of file: {}. For now, only PDF files are supported", path);
LOGGER.info("Unsupported file type of file: {}. Currently, only PDF files are supported", path);
return Optional.empty();
}
}
Expand All @@ -39,7 +39,7 @@ private static Optional<Document> fromPdfFile(Path path) {

return FileToDocument.fromString(writer.toString());
} catch (Exception e) {
LOGGER.error("An error occurred while reading a PDF file: {}", path, e);
LOGGER.error("An error occurred while reading the PDF file: {}", path, e);
return Optional.empty();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import dev.langchain4j.store.embedding.filter.comparison.IsIn;
import jakarta.annotation.Nullable;
import org.h2.mvstore.MVStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.util.Comparator.comparingDouble;

Expand All @@ -42,11 +40,8 @@
* <p>
*/
public class MVStoreEmbeddingStore implements EmbeddingStore<TextSegment> {
private static final Logger LOGGER = LoggerFactory.getLogger(MVStoreEmbeddingStore.class);

// `file` field is nullable, because {@link Optional} can't be serialized.
private record EmbeddingRecord(@Nullable String file, String content, float[] embeddingVector) implements Serializable {
}
private record EmbeddingRecord(@Nullable String file, String content, float[] embeddingVector) implements Serializable { }

private final Map<String, EmbeddingRecord> embeddingsMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private void summarizeAll() throws InterruptedException {
// is used internally in the summarization, and it also throws RuntimeExceptions.

if (bibDatabaseContext.getDatabasePath().isEmpty()) {
throw new RuntimeException(Localization.lang("No summary can be generated for entry '%0' as the database doesn't have path", citationKey));
throw new RuntimeException(Localization.lang("No summary can be generated for entry '%0' as the database does not have path", citationKey));
}

addMoreWork(1); // For generating final summary.
Expand Down Expand Up @@ -188,7 +188,7 @@ public String summarizeOneDocument(String filePath, String document) throws Inte

do {
passes++;
LOGGER.info("Summarizing chunks for file \"{}\" of entry {} ({} pass)", filePath, citationKey, passes);
LOGGER.info("Summarizing chunk(s) for file \"{}\" of entry {} ({} pass)", filePath, citationKey, passes);

addMoreWork(chunkSummaries.size());

Expand Down Expand Up @@ -224,7 +224,7 @@ public String summarizeOneDocument(String filePath, String document) throws Inte
throw new InterruptedException();
}

LOGGER.info("Sending request to AI provider to combine summary chunks for file \"{}\" of entry {}", filePath, citationKey);
LOGGER.info("Sending request to AI provider to combine summary chunk(s) for file \"{}\" of entry {}", filePath, citationKey);
String result = aiService.getChatLanguageModel().generate(prompt.toString());
LOGGER.info("Summary of the file \"{}\" of entry {} was generated successfully", filePath, citationKey);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.jabref.model.entry.event.FieldChangedEvent;
import org.jabref.model.entry.field.InternalField;
import org.jabref.preferences.ai.AiPreferences;
import org.jabref.preferences.ai.AiProvider;

import com.airhacks.afterburner.injection.Injector;
import com.google.common.eventbus.EventBus;
Expand All @@ -23,17 +24,15 @@
public class SummariesStorage {
private final static Logger LOGGER = LoggerFactory.getLogger(SummariesStorage.class);

private final AiPreferences aiPreferences;
private final MVStore mvStore;

private final EventBus eventBus = new EventBus();

@Inject private StateManager stateManager = Injector.instantiateModelOrService(StateManager.class);

public record SummarizationRecord(LocalDateTime timestamp, AiPreferences.AiProvider aiProvider, String model, String content) implements Serializable { }
public record SummarizationRecord(LocalDateTime timestamp, AiProvider aiProvider, String model, String content) implements Serializable { }

public SummariesStorage(AiPreferences aiPreferences, MVStore mvStore) {
this.aiPreferences = aiPreferences;
this.mvStore = mvStore;
}

Expand Down
Loading

0 comments on commit a5ef15f

Please sign in to comment.