Skip to content

Commit

Permalink
Finish work on refactoring the summarization
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Aug 24, 2024
1 parent 4910d12 commit 9e40ba9
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private List<Notification> updateNotificationsForEntry(BibEntry entry) {
case ERROR -> notifications.add(new Notification(
NotificationType.ERROR,
Localization.lang("File %0 could not be ingested", ingestionStatus.object().getLink()),
ingestionStatus.errorMessage().get()
ingestionStatus.exception().get().getLocalizedMessage()
));

case SUCCESS -> { }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,24 @@
package org.jabref.gui.ai.components.summary;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import javafx.scene.Node;

import org.jabref.gui.DialogService;
import org.jabref.gui.ai.components.privacynotice.AiPrivacyNoticeGuardedComponent;
import org.jabref.gui.ai.components.util.errorstate.ErrorStateComponent;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.gui.util.UiTaskExecutor;
import org.jabref.logic.ai.AiService;
import org.jabref.logic.ai.processingstatus.ProcessingInfo;
import org.jabref.logic.ai.processingstatus.ProcessingState;
import org.jabref.logic.ai.summarization.GenerateSummaryTask;
import org.jabref.logic.ai.summarization.SummariesStorage;
import org.jabref.logic.citationkeypattern.CitationKeyGenerator;
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.preferences.FilePreferences;
import org.jabref.preferences.PreferencesService;

import com.google.common.eventbus.Subscribe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -39,18 +31,23 @@ public class SummaryComponent extends AiPrivacyNoticeGuardedComponent {
private final BibEntry entry;
private final CitationKeyGenerator citationKeyGenerator;
private final AiService aiService;
private final FilePreferences filePreferences;
private final TaskExecutor taskExecutor;

public SummaryComponent(BibDatabaseContext bibDatabaseContext, BibEntry entry, AiService aiService, PreferencesService preferencesService, DialogService dialogService, TaskExecutor taskExecutor) {
super(aiService.getPreferences(), preferencesService.getFilePreferences(), dialogService);
public SummaryComponent(BibDatabaseContext bibDatabaseContext,
BibEntry entry, AiService aiService,
FilePreferences filePreferences,
CitationKeyPatternPreferences citationKeyPatternPreferences,
DialogService dialogService
) {
super(aiService.getPreferences(), filePreferences, dialogService);

this.bibDatabaseContext = bibDatabaseContext;
this.entry = entry;
this.citationKeyGenerator = new CitationKeyGenerator(bibDatabaseContext, preferencesService.getCitationKeyPatternPreferences());
this.citationKeyGenerator = new CitationKeyGenerator(bibDatabaseContext, citationKeyPatternPreferences);
this.aiService = aiService;
this.filePreferences = preferencesService.getFilePreferences();
this.taskExecutor = taskExecutor;

aiService.getSummariesService().summarize(entry, bibDatabaseContext).state().addListener(o -> rebuildUi());

rebuildUi();
}

@Override
Expand Down Expand Up @@ -105,50 +102,29 @@ private Node tryToGenerateCitationKeyThenBind(BibEntry entry) {
private Node tryToShowSummary() {
ProcessingInfo<BibEntry, SummariesStorage.SummarizationRecord> processingInfo = aiService.getSummariesService().summarize(entry, bibDatabaseContext);

switch (processingInfo.state().get()) {
case SUCCESS:
return showSummary(processingInfo.data());
case ERROR:
return showErrorNotSummarized();
case RUNNING:
return startGeneratingSummary(entry);
}

if (processingInfo.state() == ProcessingState.SUCCESS) {
return showSummary(processingInfo.data());
} else if (pro)
return switch (processingInfo.state().get()) {
case SUCCESS ->
showSummary(processingInfo.data().get());
case ERROR ->
showErrorWhileSummarizing(processingInfo);
case PROCESSING ->
showErrorNotSummarized();
};
}

private void showErrorWhileSummarizing(Exception e) {
LOGGER.error("Got an error while generating a summary for entry {}", entry.getCitationKey().orElse("<no citation key>"), e);

setContent(
ErrorStateComponent.withTextAreaAndButton(
Localization.lang("Unable to chat"),
Localization.lang("Got error while processing the file:"),
e.getMessage(),
Localization.lang("Regenerate"),
() -> bindToEntry(currentEntry)
)
private Node showErrorWhileSummarizing(ProcessingInfo<BibEntry, SummariesStorage.SummarizationRecord> processingInfo) {
LOGGER.error("Got an error while generating a summary for entry {}", entry.getCitationKey().orElse("<no citation key>"), processingInfo.exception().get());

return ErrorStateComponent.withTextAreaAndButton(
Localization.lang("Unable to chat"),
Localization.lang("Got error while processing the file:"),
processingInfo.exception().get().getLocalizedMessage(),
Localization.lang("Regenerate"),
() -> aiService.getSummariesService().regenerateSummary(entry, bibDatabaseContext)
);
}


private Node startGeneratingSummary(BibEntry entry) {
assert entry.getCitationKey().isPresent();

if (!entriesUnderSummarization.contains(entry)) {
entriesUnderSummarization.add(entry);

new GenerateSummaryTask(bibDatabaseContext, entry.getCitationKey().get(), entry.getFiles(), aiService, filePreferences)
.onSuccess(res -> rebuildUi())
.onFailure(e -> rebuildUi())
.executeWith(taskExecutor);
}

return showErrorNotSummarized();
}

private Node showErrorNotSummarized() {
return ErrorStateComponent.withSpinner(
Localization.lang("Processing..."),
Expand All @@ -157,9 +133,7 @@ private Node showErrorNotSummarized() {
}


private Node bindToCorrectEntry(SummariesStorage.SummarizationRecord summary) {
entriesUnderSummarization.remove(entry);

private Node showSummary(SummariesStorage.SummarizationRecord summary) {
return new SummaryShowingComponent(summary, () -> {
if (bibDatabaseContext.getDatabasePath().isEmpty()) {
LOGGER.error("Bib database path is not set, but it was expected to be present. Unable to regenerate summary");
Expand All @@ -171,8 +145,8 @@ private Node bindToCorrectEntry(SummariesStorage.SummarizationRecord summary) {
return;
}

aiService.getSummariesStorage().clear(bibDatabaseContext.getDatabasePath().get(), entry.getCitationKey().get());
rebuildUi();
aiService.getSummariesService().regenerateSummary(entry, bibDatabaseContext);
// No need to rebuildUi(), because this class listens to the state of ProcessingInfo of the summary.
});
}
}
26 changes: 10 additions & 16 deletions src/main/java/org/jabref/gui/entryeditor/AiChatTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ public AiChatTab(DialogService dialogService,

setText(Localization.lang("AI chat"));
setTooltip(new Tooltip(Localization.lang("Chat with AI about content of attached file(s)")));

aiService.getEmbeddingsManager().registerListener(this);
aiService.getEmbeddingModel().registerListener(this);
}

@Override
Expand All @@ -59,18 +56,15 @@ protected void bindToEntry(BibEntry entry) {
aiService.getChatHistoryService().closeChatHistoryForEntry(currentEntry);
}

AiChatGuardedComponentAi aiChatGuardedComponent =
new AiChatGuardedComponentAi(
"entry " + entry.getCitationKey().orElse("<no citation key>"),
aiService.getChatHistoryService().getChatHistoryForEntry(entry),
FXCollections.observableArrayList(new ArrayList<>(List.of(entry))),
dialogService,
filePreferences,
aiService,
bibDatabaseContext,
taskExecutor
);

setContent(aiChatGuardedComponent);
setContent(new AiChatGuardedComponentAi(
"entry " + entry.getCitationKey().orElse("<no citation key>"),
aiService.getChatHistoryService().getChatHistoryForEntry(entry),
FXCollections.observableArrayList(new ArrayList<>(List.of(entry))),
dialogService,
filePreferences,
aiService,
bibDatabaseContext,
taskExecutor
));
}
}
Loading

0 comments on commit 9e40ba9

Please sign in to comment.