Skip to content

Commit

Permalink
Fix for InAnYan#115
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Aug 7, 2024
1 parent 43f1f7c commit 32c4943
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.airhacks.afterburner.views.ViewLoader;
import com.dlsc.gemsfx.ExpandingTextArea;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
import org.slf4j.Logger;
Expand All @@ -29,6 +30,7 @@ public class AiChatComponent extends VBox {
private static final Logger LOGGER = LoggerFactory.getLogger(AiChatComponent.class);

private final AiChatLogic aiChatLogic;
private final String citationKey;
private final DialogService dialogService;
private final TaskExecutor taskExecutor;

Expand All @@ -38,8 +40,9 @@ public class AiChatComponent extends VBox {
@FXML private Button submitButton;
@FXML private StackPane stackPane;

public AiChatComponent(AiChatLogic aiChatLogic, DialogService dialogService, TaskExecutor taskExecutor) {
public AiChatComponent(AiChatLogic aiChatLogic, String citationKey, DialogService dialogService, TaskExecutor taskExecutor) {
this.aiChatLogic = aiChatLogic;
this.citationKey = citationKey;
this.dialogService = dialogService;
this.taskExecutor = taskExecutor;

Expand Down Expand Up @@ -82,23 +85,29 @@ private void onSendMessage() {
addMessage(userMessage);
setLoading(true);

BackgroundTask.wrap(() -> aiChatLogic.execute(userMessage))
.onSuccess(aiMessage -> {
setLoading(false);
addMessage(aiMessage);
requestUserPromptTextFieldFocus();
})
.onFailure(e -> {
LOGGER.error("Got an error while sending a message to AI", e);
setLoading(false);

if (e.getMessage().equals("401 - null") || e.getMessage().equals("404 - null")) {
addError(Localization.lang("API base URL setting appears to be incorrect. Please check it in AI expert settings."));
} else {
addError(e.getMessage());
}
})
.executeWith(taskExecutor);
BackgroundTask<AiMessage> task =
BackgroundTask
.wrap(() -> aiChatLogic.execute(userMessage))
.showToUser(true)
.onSuccess(aiMessage -> {
setLoading(false);
addMessage(aiMessage);
requestUserPromptTextFieldFocus();
})
.onFailure(e -> {
LOGGER.error("Got an error while sending a message to AI", e);
setLoading(false);

if (e.getMessage().equals("401 - null") || e.getMessage().equals("404 - null")) {
addError(Localization.lang("API base URL setting appears to be incorrect. Please check it in AI expert settings."));
} else {
addError(e.getMessage());
}
});

task.titleProperty().set(Localization.lang("Waiting for AI reply for %0...", citationKey));

task.executeWith(taskExecutor);
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/jabref/gui/entryeditor/AiChatTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,14 @@ private void showErrorWhileIngesting(Exception e) {
}

private void bindToCorrectEntry(BibEntry entry) {
assert entry.getCitationKey().isPresent();

AiChatHistory aiChatHistory = getAiChatHistory(aiService, entry, bibDatabaseContext);

AiChatLogic aiChatLogic = AiChatLogic.forBibEntry(aiService, aiChatHistory, entry);
Node content = new AiChatComponent(aiChatLogic, dialogService, taskExecutor);

Node content = new AiChatComponent(aiChatLogic, entry.getCitationKey().get(), dialogService, taskExecutor);

setContent(content);
}

Expand All @@ -236,7 +241,7 @@ private static AiChatHistory getAiChatHistory(AiService aiService, BibEntry entr
LOGGER.warn("AI chat is constructed, but the entry citation key is empty. Cannot store chat history");
return new InMemoryAiChatHistory();
} else {
return aiService.getChatHistoryManager().getChatHistory(bibDatabaseContext.getDatabasePath().get(), entry.getCitationKey().get());
return aiService.getChatHistoryManager().getChatHistory(databasePath.get(), entry.getCitationKey().get());
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/org/jabref/logic/ai/GenerateEmbeddingsTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -33,6 +35,7 @@ public class GenerateEmbeddingsTask extends BackgroundTask<Void> {

private final IntegerProperty workDone = new SimpleIntegerProperty(0);
private final IntegerProperty workMax = new SimpleIntegerProperty(0);
private Instant oneWorkTimeStart = Instant.now();

public GenerateEmbeddingsTask(String citationKey,
List<LinkedFile> linkedFiles,
Expand Down Expand Up @@ -121,6 +124,20 @@ private void ingestLinkedFile(LinkedFile linkedFile) throws InterruptedException
}

private void updateProgress() {
Instant oneWorkTimeEnd = Instant.now();
Duration duration = Duration.between(oneWorkTimeStart, oneWorkTimeEnd);
oneWorkTimeStart = oneWorkTimeEnd;

Duration eta = duration.multipliedBy(workMax.get() - workDone.get());

updateProgress(workDone.get(), workMax.get());

if (eta.getSeconds() < 60) {
updateMessage(Localization.lang("Estimated time left: %0 seconds", eta.getSeconds()));
} else if (eta.getSeconds() % 60 == 0) {
updateMessage(Localization.lang("Estimated time left: %0 minutes", eta.getSeconds() / 60));
} else {
updateMessage(Localization.lang("Estimated time left: %0 minutes, %1 seconds", eta.getSeconds() / 60, eta.getSeconds() % 60));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jabref.logic.ai.summarization;

import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -65,6 +67,7 @@ public class GenerateSummaryTask extends BackgroundTask<Void> {

private int workDone = 0;
private int workMax = 0;
private Instant oneWorkTimeStart = Instant.now();

public GenerateSummaryTask(BibDatabaseContext bibDatabaseContext,
String citationKey,
Expand All @@ -77,7 +80,7 @@ public GenerateSummaryTask(BibDatabaseContext bibDatabaseContext,
this.aiService = aiService;
this.filePreferences = filePreferences;

titleProperty().set(Localization.lang("Generating summary for %0...", citationKey));
titleProperty().set(Localization.lang("Waiting summary for %0...", citationKey));
showToUser(true);
}

Expand Down Expand Up @@ -242,6 +245,22 @@ private static int estimateTokenCount(int numOfChars) {
}

private void updateProgress() {
Instant oneWorkTimeEnd = Instant.now();
Duration duration = Duration.between(oneWorkTimeStart, oneWorkTimeEnd);
oneWorkTimeStart = oneWorkTimeEnd;

Duration eta = duration.multipliedBy(workMax - workDone);

updateProgress(workDone, workMax);

if (eta.getSeconds() < 60) {
updateMessage(Localization.lang("Estimated time left: %0 seconds", eta.getSeconds()));
} else if (eta.getSeconds() % 60 == 0) {
updateMessage(Localization.lang("Estimated time left: %0 minutes", eta.getSeconds() / 60));
} else {
updateMessage(Localization.lang("Estimated time left: %0 minutes, %1 seconds", eta.getSeconds() / 60, eta.getSeconds() % 60));
}

updateProgress(workDone, workMax);
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2571,7 +2571,6 @@ Generating\ embeddings\ for\ for\ %0=Generating embeddings for for %0
These\ parameters\ affect\ how\ the\ AI\ will\ answer\ you\ questions.=These parameters affect how the AI will answer you questions.
Additionally,\ we\ use\ Deep\ Java\ Library\ embedding\ models\ for\ both\ chatting\ and\ summarization.\ The\ embedding\ model\ will\ be\ downloaded\ in\ background\ from\ Deep\ Java\ Library\ servers\ anonymously.=Additionally, we use Deep Java Library embedding models for both chatting and summarization. The embedding model will be downloaded in background from Deep Java Library servers anonymously.
Chat\ with\ AI\ about\ content\ of\ attached\ file(s)=Chat with AI about content of attached file(s)
Generating\ summary\ for\ %0...=Generating summary for %0...
If\ you\ have\ chosen\ the\ Hugging\ Face\ as\ AI\ provider,\ the\ privacy\ policy\ of\ Hugging\ Face\ applies.\ You\ find\ it\ at\ %0.=If you have chosen the Hugging Face as AI provider, the privacy policy of Hugging Face applies. You find it at %0.
If\ you\ have\ chosen\ the\ Mistral\ AI\ as\ AI\ provider,\ the\ privacy\ policy\ of\ Mistral\ AI\ applies.\ You\ find\ it\ at\ %0.=If you have chosen the Mistral AI as AI provider, the privacy policy of Mistral AI applies. You find it at %0.
If\ you\ have\ chosen\ the\ OpenAI\ as\ AI\ provider,\ the\ privacy\ policy\ of\ OpenAI\ applies.\ You\ find\ it\ at\ %0.=If you have chosen the OpenAI as AI provider, the privacy policy of OpenAI applies. You find it at %0.
Expand All @@ -2594,6 +2593,11 @@ JabRef\ uses\ AI\ providers\ to\ enable\ AI\ functionality\ (chatting\ with\ att
Please\ attach\ at\ least\ one\ PDF\ file\ to\ enable\ chatting\ with\ PDF\ file(s).=Please attach at least one PDF file to enable chatting with PDF file(s).
The\ attached\ file(s)\ are\ currently\ being\ processed\ by\ %0.\ Once\ completed,\ you\ will\ be\ able\ to\ see\ the\ summary.=The attached file(s) are currently being processed by %0. Once completed, you will be able to see the summary.
The\ embeddings\ of\ the\ file(s)\ are\ currently\ being\ generated.\ Please\ wait,\ and\ at\ the\ end\ you\ will\ be\ able\ to\ chat.=The embeddings of the file(s) are currently being generated. Please wait, and at the end you will be able to chat.
Estimated\ time\ left\:\ %0\ minutes=Estimated time left: %0 minutes
Estimated\ time\ left\:\ %0\ minutes,\ %1\ seconds=Estimated time left: %0 minutes, %1 seconds
Estimated\ time\ left\:\ %0\ seconds=Estimated time left: %0 seconds
Waiting\ for\ AI\ reply\ for\ %0...=Waiting for AI reply for %0...
Waiting\ summary\ for\ %0...=Waiting summary for %0...
Edit\ file\ link=Edit file link
Expand Down

0 comments on commit 32c4943

Please sign in to comment.