Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Aug 6, 2024
1 parent 7c1a27e commit f9d6dbf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/entryeditor/AiChatTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private void showErrorWhileBuildingEmbeddingModel() {
Localization.lang("Unable to chat"),
Localization.lang("An error occurred while building the embedding model"),
aiService.getEmbeddingModel().getErrorWhileBuildingModel(),
Localization.lang("Try to rebuild again"),
Localization.lang("Rebuild"),
() -> aiService.getEmbeddingModel().startRebuildingTask()
)
);
Expand Down Expand Up @@ -236,7 +236,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, entry.getCitationKey().get());
return aiService.getChatHistoryManager().getChatHistory(bibDatabaseContext.getDatabasePath().get(), entry.getCitationKey().get());
}
}

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 @@ -189,7 +189,7 @@ private void showErrorWhileSummarizing(Exception e) {
Localization.lang("Unable to chat"),
Localization.lang("Got error while processing the file:"),
e.getMessage(),
Localization.lang("Try to regenerate"),
Localization.lang("Regenerate"),
() -> bindToEntry(currentEntry)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,23 @@ public BibDatabaseChatHistoryManager(MVStore mvStore) {
this.mvStore = mvStore;
}

private Map<Integer, ChatHistoryRecord> getMap(BibDatabaseContext bibDatabaseContext, String citationKey) {
if (bibDatabaseContext.getDatabasePath().isPresent()) {
return mvStore.openMap("chathistory-" + bibDatabaseContext.getDatabasePath().get() + "-" + citationKey);
} else {
LOGGER.warn("Tried to call BibDatabaseChatHistoryManager.getMap with no database path present. Will use a Java HashMap instead of MVMap from MVStore.");
return new HashMap<>();
}
private Map<Integer, ChatHistoryRecord> getMap(Path bibDatabasePath, String citationKey) {
return mvStore.openMap("chathistory-" + bibDatabasePath + "-" + citationKey);
}

public AiChatHistory getChatHistory(BibDatabaseContext bibDatabaseContext, String citationKey) {
public AiChatHistory getChatHistory(Path bibDatabasePath, String citationKey) {
return new AiChatHistory() {
@Override
public List<ChatMessage> getMessages() {
Map<Integer, ChatHistoryRecord> messages = getMap(bibDatabaseContext, citationKey);
Map<Integer, ChatHistoryRecord> messages = getMap(bibDatabasePath, citationKey);
return IntStream.range(0, messages.size())
.mapToObj(key -> messages.get(key).toLangchainMessage())
.toList();
}

@Override
public void add(ChatMessage chatMessage) {
Map<Integer, ChatHistoryRecord> map = getMap(bibDatabaseContext, citationKey);
Map<Integer, ChatHistoryRecord> map = getMap(bibDatabasePath, citationKey);

// We count 0-based, thus "size()" is the next number
// 0 entries -> 0 is the first new id
Expand All @@ -102,7 +97,7 @@ public void add(ChatMessage chatMessage) {

@Override
public void clear() {
getMap(bibDatabaseContext, citationKey).clear();
getMap(bibDatabasePath, citationKey).clear();
}
};
}
Expand All @@ -122,8 +117,15 @@ private void fieldChangedEventListener(FieldChangedEvent event) {
return;
}

Map<Integer, ChatHistoryRecord> oldMap = getMap(bibDatabaseContext.get(), event.getOldValue());
getMap(bibDatabaseContext.get(), event.getNewValue()).putAll(oldMap);
Optional<Path> bibDatabasePath = bibDatabaseContext.get().getDatabasePath();

if (bibDatabasePath.isEmpty()) {
LOGGER.error("Could not listen to field change event because no database path was found. BibEntry: {}", event.getBibEntry());
return;
}

Map<Integer, ChatHistoryRecord> oldMap = getMap(bibDatabasePath.get(), event.getOldValue());
getMap(bibDatabasePath.get(), event.getNewValue()).putAll(oldMap);
oldMap.clear();
}
}
11 changes: 5 additions & 6 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2534,7 +2534,6 @@ Reset\ expert\ settings\ to\ default=Reset expert settings to default
Leave\ these\ fields\ as\ is,\ if\ you\ are\ not\ sure\ of\ their\ purpose.=Leave these fields as is, if you are not sure of their purpose.
Only\ PDF\ files\ are\ supported.=Only PDF files are supported.
Please\ provide\ a\ non-empty\ and\ unique\ citation\ key\ for\ this\ entry.=Please provide a non-empty and unique citation key for this entry.
Please\ wait=Please wait
RAG\ -\ maximum\ results\ count=RAG - maximum results count
RAG\ -\ minimum\ score=RAG - minimum score
RAG\ max\ results\ count\ must\ be\ greater\ than\ 0=RAG max results count must be greater than 0
Expand All @@ -2560,20 +2559,15 @@ Chat\ model=Chat model
Chat\ model\ has\ to\ be\ provided=Chat model has to be provided
AI\ provider=AI provider
API\ base\ URL\ (used\ only\ for\ LLM)=API base URL (used only for LLM)
API\ token=API token
An\ error\ occurred\ while\ building\ the\ embedding\ model=An error occurred while building the embedding model
Embedding\ model\ is\ not\ set\ up=Embedding model is not set up
Try\ to\ rebuild\ again=Try to rebuild again
AI\ summary=AI summary
Regenerate=Regenerate
The\ path\ of\ the\ current\ library\ is\ not\ set,\ but\ it\ is\ required\ for\ summarization=The path of the current library is not set, but it is required for summarization
Try\ to\ regenerate=Try to regenerate
As\ soon\ as\ you\ ask\ a\ question,\ the\ text\ content\ of\ all\ PDFs\ attached\ to\ the\ entry\ are\ send\ to\ external\ service.=As soon as you ask a question, the text content of all PDFs attached to the entry are send to external service.
Enable\ AI\ functionality\ (summary\ generation\ and\ chatting)\ in\ JabRef=Enable AI functionality (summary generation and chatting) in JabRef
AI-generated\ summary\ of\ attached\ files=AI-generated summary of attached files
API\ key\ is\ missing.=API key is missing.
%0%\ work\ done\ for\ embeddings\ generation=%0% work done for embeddings generation
%0%\ work\ done\ for\ summarization=%0% work done for summarization
Customize\ expert\ settings=Customize expert settings
Downloading\ embedding\ model=Downloading embedding model
Downloading\ embedding\ model...\ Afterwards,\ you\ will\ be\ able\ to\ chat\ with\ your\ files.=Downloading embedding model... Afterwards, you will be able to chat with your files.
Expand All @@ -2590,6 +2584,11 @@ If\ you\ have\ chosen\ the\ OpenAI\ as\ AI\ provider,\ the\ privacy\ policy\ of\
In\ order\ to\ use\ AI\ chat,\ set\ OpenAI\ API\ key\ inside\ JabRef\ preferences\ (AI\ tab).=In order to use AI chat, set OpenAI API key inside JabRef preferences (AI tab).
In\ order\ to\ use\ AI\ chat,\ you\ need\ to\ enable\ chatting\ with\ attached\ PDF\ files\ in\ JabRef\ preferences\ (AI\ tab).=In order to use AI chat, you need to enable chatting with attached PDF files in JabRef preferences (AI tab).
Unable\ to\ chat\ with\ AI.=Unable to chat with AI.
API\ key=API key
Downloading...=Downloading...
No\ summary\ can\ be\ generated\ for\ entry\ '%0'\ as\ the\ database\ doesn't\ have\ path=No summary can be generated for entry '%0' as the database doesn't have path
No\ summary\ can\ be\ generated\ for\ entry\ '%0'.\ Could\ not\ find\ attached\ linked\ files.=No summary can be generated for entry '%0'. Could not find attached linked files.
Rebuild=Rebuild
Edit\ file\ link=Edit file link
Expand Down

0 comments on commit f9d6dbf

Please sign in to comment.