Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AI chatting functionality #11430

Open
wants to merge 190 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
190 commits
Select commit Hold shift + click to select a range
d9d58f6
First AI steps
InAnYan May 8, 2024
7c358c9
Some changes to AI code
InAnYan May 8, 2024
0a05213
Add AI preferences
InAnYan May 8, 2024
812820a
First part of fix
koppor May 8, 2024
7f92e0d
Fix: second part
koppor May 8, 2024
cee2278
More fixes
koppor May 8, 2024
37d0534
Remove unneded Apache PDFBox
koppor May 8, 2024
d9b6c3e
Added AiChatTab to EntryEditor tabs
InAnYan May 8, 2024
c0b56f3
Merge remote-tracking branch 'origin/ai-1' into ai-1
InAnYan May 8, 2024
19d8aa3
Fix dependency
koppor May 8, 2024
2dcaa3a
Add require on kotlin.stdlib
koppor May 8, 2024
f389bad
Some changes
InAnYan May 8, 2024
971fdce
AI preferences tab
InAnYan May 10, 2024
ddacd47
AI is split to classes. Preferences tab. UI changes
InAnYan May 11, 2024
1da98b5
Changed according to code review
InAnYan May 15, 2024
9ffeaa4
Deleted check for OpenAI token format
InAnYan May 15, 2024
3bf8ac1
Tried to solve the bug with token
InAnYan May 15, 2024
14b7c72
Tried to solve the bug with token
InAnYan May 15, 2024
784434f
Split preferences: "Show AI chat tab" / "Chat with PDFs"
InAnYan May 17, 2024
7fac426
Fix typos
InAnYan May 17, 2024
8a12515
Improve UI
InAnYan May 17, 2024
221f961
Save chat history in memory. Split UI to classes
InAnYan May 18, 2024
185de4e
Manage chat history
InAnYan May 19, 2024
267efc9
Clean saving chat history
InAnYan May 25, 2024
62ea22e
Fix module-info.java
InAnYan May 25, 2024
4ddf173
Fix SaveDatabaseAction
InAnYan May 25, 2024
f9bd0d0
Fix chat UI
InAnYan May 25, 2024
43845ae
Make custom components for UI
InAnYan May 25, 2024
c9cf1a0
Improve AI service
InAnYan May 27, 2024
70ddca5
Pollute AI service everywhere
InAnYan May 27, 2024
6cec13a
Fix bugs
InAnYan May 27, 2024
008c8c7
Pollute AI service everywhere
InAnYan May 27, 2024
bda1efb
Merge remote-tracking branch 'origin/add-ai-service' into add-ai-service
InAnYan May 27, 2024
066747c
Fix logger messages (changes provided by @koppor)
InAnYan May 27, 2024
3943505
Fix chat message alignment
InAnYan May 27, 2024
3a39552
Remove comment
InAnYan May 27, 2024
51cdcde
Use CustomPasswordField
InAnYan May 27, 2024
2675b56
Improve the UI for AI chat tab states
InAnYan May 27, 2024
afb01cd
Autogenerate citation key
InAnYan May 27, 2024
74014c8
Revert "Autogenerate citation key"
InAnYan May 27, 2024
d8c19c3
Autogenerate citation key
InAnYan May 27, 2024
20bb6ac
Fix Jackson adding a `typeLabel` field
InAnYan May 27, 2024
c79e5d2
Improve `AiService` and `AiChat` classes
InAnYan May 27, 2024
5cffaa1
Fix code bug
InAnYan May 27, 2024
97349de
Fix code bug
InAnYan May 27, 2024
49d6e00
Merge pull request #31 from InAnYan/fix-for-issue-16
InAnYan May 28, 2024
add2e05
Merge pull request #28 from InAnYan/fix-for-issue-18
InAnYan May 28, 2024
284a050
Merge pull request #22 from InAnYan/fix-for-issue-12
InAnYan May 28, 2024
ad08227
Merge pull request #24 from InAnYan/fix-for-issue-20
InAnYan May 28, 2024
8b4d3c6
Merge pull request #30 from InAnYan/fix-for-issue-13
InAnYan May 28, 2024
c8048ca
Merge branch 'refs/heads/ai-1' into fix-for-issue-19
InAnYan May 29, 2024
df60c5f
Fix code after merge
InAnYan May 29, 2024
baad7b3
Merge pull request #26 from InAnYan/fix-for-issue-19
InAnYan May 29, 2024
5755334
Merge pull request #9 from InAnYan/add-ai-service
InAnYan May 29, 2024
391e921
Add draft ADRs
InAnYan May 29, 2024
1ea6029
Fix bugs
InAnYan May 29, 2024
80f591a
Add warning to AiChatComponent
InAnYan May 29, 2024
883dbe7
Add requestFocus on user prompt text field
InAnYan May 29, 2024
84220b3
Attempt to fix the issue
InAnYan May 29, 2024
2a18e01
First attempt
InAnYan Jun 3, 2024
bca31f6
Request focus on user prompt field after AI has generated answer
InAnYan Jun 3, 2024
94bf0e3
Change branches of `scanLabeledControls`
InAnYan Jun 3, 2024
7545c27
Fix bugs
InAnYan Jun 3, 2024
dec354e
Merge pull request #38 from InAnYan/fix-for-33
InAnYan Jun 4, 2024
9aacf9b
Merge pull request #37 from InAnYan/fix-for-34
InAnYan Jun 4, 2024
6ee1f0e
Add translation string
InAnYan Jun 4, 2024
b2ab59e
Clean code
InAnYan Jun 4, 2024
51d682b
Merge pull request #39 from InAnYan/fix-for-29
InAnYan Jun 4, 2024
cc3939f
Merge branch 'refs/heads/ai-1' into fix-for-32
InAnYan Jun 4, 2024
99e0f8d
Add missing localization keys
InAnYan Jun 4, 2024
6b6713a
Merge remote-tracking branch 'origin/ai-1' into ai-1
InAnYan Jun 4, 2024
a0bec89
Update ADR files
InAnYan Jun 4, 2024
0fa2450
Update ADR files
InAnYan Jun 4, 2024
afe98d3
Merge pull request #35 from InAnYan/fix-for-32
InAnYan Jun 4, 2024
359b7b9
Attempt to upgrade langchain4j version to 0.31.0
InAnYan Jun 4, 2024
0aa3880
Add missing dependency
koppor Jun 4, 2024
cf6be7e
Make and use MVStore
InAnYan Jun 6, 2024
2505656
Change directory creation logic
InAnYan Jun 6, 2024
3c4de4b
Track adding and removing from index
InAnYan Jun 6, 2024
7a803ef
Merge branch 'refs/heads/main' into ai-1
InAnYan Jun 7, 2024
eb53cb7
Merge branch 'refs/heads/ai-1' into fix-for-14
InAnYan Jun 7, 2024
0ab6e68
Change PdfIndexer
InAnYan Jun 9, 2024
edc22f6
Listen to ingested files changes
InAnYan Jun 9, 2024
f057a58
Refactor AiChatTab methods
InAnYan Jun 9, 2024
8ae7130
Implement removing of embeddings
InAnYan Jun 9, 2024
eccb2a8
Remove store path logging
InAnYan Jun 9, 2024
2010745
Fix bugs
InAnYan Jun 11, 2024
a8f164d
Improve ErrorStateComponent
InAnYan Jun 11, 2024
2c2617a
Merge branch 'refs/heads/ai-1' into fix-for-14
InAnYan Jun 11, 2024
7ce6eaf
Add spinner
InAnYan Jun 11, 2024
12430ec
Make all fields in AI preferences vertical
InAnYan Jun 11, 2024
59a8a78
Use MVStore for storing chats
InAnYan Jun 12, 2024
72e306c
Change "System message" to "Instruction"
InAnYan Jun 12, 2024
987e527
Reimplement ID logic for chat history
InAnYan Jun 14, 2024
31d4527
Use synchronize in BibDatabaseChats
InAnYan Jun 14, 2024
fbf2502
Merge pull request #50 from InAnYan/fix-for-11-2
InAnYan Jun 14, 2024
06e79af
Merge branch 'ai-1' into fix-for-14
InAnYan Jun 14, 2024
1f1dcf7
Merge pull request #48 from InAnYan/fix-for-14
InAnYan Jun 14, 2024
7706813
Fix merge conflicts
InAnYan Jun 14, 2024
b47db32
Improve expert settings. (Maybe)
InAnYan Jun 14, 2024
bdc4a9e
Add validation status visualization
InAnYan Jun 14, 2024
70e29fc
Add model ComboBox
InAnYan Jun 14, 2024
bc30d1a
Add temperature parameter
InAnYan Jun 14, 2024
ce85e89
Use MVStore for saving embeddings
InAnYan Jun 15, 2024
f0e518e
Implement EmbeddingsGenerationTaskManager
InAnYan Jun 15, 2024
dbb6f3b
Refactor AI classes
InAnYan Jun 16, 2024
14cf181
Implement the rest of logic of EmbeddingsGenerationTaskManager and ad…
InAnYan Jun 17, 2024
4a114df
For now use web page as help
InAnYan Jun 17, 2024
5b55391
Use MarkdownView for chat message content
InAnYan Jun 17, 2024
747132f
Implement clearing of chat history
InAnYan Jun 17, 2024
8fd52ef
Proper scrolling and change the design of progress indication
InAnYan Jun 17, 2024
5e638bd
Try to implement priority for selected linked files
InAnYan Jun 18, 2024
7df72ed
Attempt to properly handle errors from MVStore opening
InAnYan Jun 18, 2024
a264d6b
Merge pull request #61 from InAnYan/fix-for-51
InAnYan Jun 18, 2024
dbad13c
Merge remote-tracking branch 'refs/remotes/origin/fix-for-preferences…
InAnYan Jun 18, 2024
5a70790
Merge remote-tracking branch 'origin/ai-1' into ai-1
InAnYan Jun 18, 2024
bb716e3
Make public
InAnYan Jun 18, 2024
cd3c2a8
Merge branch 'refs/heads/fix-for-chat-ui' into ai-1
InAnYan Jun 18, 2024
ee8f2a3
Workaround for Markdown chat message content
InAnYan Jun 18, 2024
f4b0195
Use TextArea for chat message content
InAnYan Jun 20, 2024
2aad4b7
Make ExpandableTextArea work
InAnYan Jun 20, 2024
9d92566
Add ADRs
InAnYan Jun 20, 2024
3bc2c2f
Merge remote-tracking branch 'refs/remotes/origin/main' into ai-1
InAnYan Jun 20, 2024
cdc4f81
Resolve conflicts and move AiService
InAnYan Jun 21, 2024
0909743
Fix default parameters of AI
InAnYan Jun 21, 2024
b4e22cb
Fix sentence end mark appearing on left in user messages
InAnYan Jun 21, 2024
f5e432d
Add comments and refactoring
InAnYan Jun 21, 2024
2c091d9
Remove comment
InAnYan Jun 21, 2024
d0adf0b
Merge branch 'refs/heads/main-jabref'
InAnYan Jun 21, 2024
d0d4486
Improve AI preferences tab
InAnYan Jun 21, 2024
0ba1ee7
Fix some of the GitHub actions errors
InAnYan Jun 22, 2024
cc160e3
Fix tests issues
InAnYan Jun 22, 2024
512107f
Merge branch 'refs/heads/main-jabref'
InAnYan Jun 25, 2024
92db40f
Fixes
InAnYan Jun 25, 2024
302dbce
Merge remote-tracking branch 'origin/main'
InAnYan Jun 25, 2024
c2effff
Make new PR
InAnYan Jun 25, 2024
1f88436
Make new PR
InAnYan Jun 26, 2024
4f52037
Merge branch 'refs/heads/upstream-main' into ai-pr-1
InAnYan Jun 26, 2024
926b2a7
Merge branch 'main' into ai-pr-1
Siedlerchr Jun 26, 2024
0e07225
Merge branch 'main' into ai-pr-1
Siedlerchr Jun 26, 2024
91ded93
Use ActionButtons for AI settings documentation
InAnYan Jun 27, 2024
91aa5be
Fix checkstyle warnings
InAnYan Jun 27, 2024
5d49ae4
Update build step in docs
InAnYan Jul 1, 2024
10d72ac
Move // TODO marker
koppor Jul 3, 2024
8e24f93
Sort langchain4j readings
koppor Jul 3, 2024
299ee17
Try to fix "Error: Java heap space" by offering 32 GB RAM (instead of…
koppor Jul 4, 2024
6b6f40f
Order settings alphabetically
koppor Jul 4, 2024
216b9c0
Merge remote-tracking branch 'upstream/main' into ai-pr-1
koppor Jul 4, 2024
305f0d8
Refine description
koppor Jul 4, 2024
94652ba
Update langcahin4j
koppor Jul 4, 2024
6ac6b31
Merge remote-tracking branch 'upstream/main' into ai-pr-1
koppor Jul 4, 2024
468e662
Discard changes to src/main/java/org/jabref/logic/pdf/search/PdfIndex…
koppor Jul 4, 2024
51ce931
Discard changes to src/main/java/org/jabref/logic/pdf/search/Indexing…
koppor Jul 4, 2024
4b015ba
Discard changes to src/test/java/org/jabref/logic/pdf/search/PdfIndex…
koppor Jul 4, 2024
53aec04
Discard changes to src/test/java/org/jabref/logic/pdf/search/PdfSearc…
koppor Jul 4, 2024
4b910b1
Discard changes to src/main/resources/tinylog.properties
koppor Jul 4, 2024
49dce23
Convert to record
koppor Jul 4, 2024
7760b7f
Improve AiChatTab
koppor Jul 4, 2024
59ba6e3
Fix compile errors
koppor Jul 4, 2024
536fc84
Uses patched verison of org.javamodularity.moduleplugin
koppor Jul 4, 2024
d1ead33
Enable the fix
koppor Jul 4, 2024
06c9ec6
Discard changes to src/main/java/org/jabref/gui/exporter/SaveDatabase…
koppor Jul 4, 2024
7b14e50
Merge remote-tracking branch 'origin/fix-run' into ai-pr-1
koppor Jul 4, 2024
521866d
Discard changes to src/main/java/org/jabref/gui/theme/Theme.java
koppor Jul 4, 2024
ef847e5
Merge branch 'refs/heads/upstream-main' into ai-pr-1
InAnYan Jul 5, 2024
d597f5e
Merge remote-tracking branch 'origin/ai-pr-1' into ai-pr-1
InAnYan Jul 5, 2024
8ca536e
Fix the code from code review
InAnYan Jul 6, 2024
1e7221a
Fix from code review and create new AiChatTabWorking
InAnYan Jul 6, 2024
874e513
Improve chat history storage code
koppor Jul 4, 2024
a3b9dd9
More fix from code review
InAnYan Jul 6, 2024
2d65495
Remove obsolete parameter
koppor Jul 8, 2024
08c77ce
Add JavaDoc comment
koppor Jul 8, 2024
ab9ff18
Fix checkstyle
koppor Jul 8, 2024
eb00e30
Fix JavaDoc
koppor Jul 8, 2024
3e6ffb1
Fix more checkstyle
koppor Jul 8, 2024
154d621
More checkstyle fixes
koppor Jul 8, 2024
7e70584
Fix code changes
InAnYan Jul 8, 2024
27cb183
Merge remote-tracking branch 'origin/ai-pr-1' into ai-pr-1
InAnYan Jul 8, 2024
3fb4cbb
Improve the PR
InAnYan Jul 10, 2024
0d617b5
Merge remote-tracking branch 'origin/main' into ai-pr-1
koppor Jul 10, 2024
b3598f6
Rework ADR-0031 to enable to use another option
koppor Jul 10, 2024
ef8b16c
Add many LOGGEr.trace statements
koppor Jul 10, 2024
1b377ee
Change "message window" to "context window"
InAnYan Jul 10, 2024
6ba5cc3
Fix compiler errors
InAnYan Jul 10, 2024
861da12
Fix issue list index issue of langchain4j
koppor Jul 10, 2024
53f1b0f
Fix lint issue
koppor Jul 10, 2024
0034f94
Update 0031-store-chats-alongside-database.md
koppor Jul 10, 2024
81f0278
More tracing
koppor Jul 10, 2024
8f6b252
Merge remote-tracking branch 'origin/main' into ai-pr-1
koppor Jul 11, 2024
e50ff5c
Refine logging
koppor Jul 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Clean saving chat history
  • Loading branch information
InAnYan committed May 25, 2024
commit 267efc9dc1ae477fe21b5d2bb0706e84984c7a5b
3 changes: 3 additions & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,7 @@
requires de.saxsys.mvvmfx.validation;
requires dd.plist;
requires mslinks;
requires langchain4j.embeddings.all.minilm.l6.v2;
requires langchain4j.core;
requires langchain4j.open.ai;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public void setMessage(ChatMessage chatMessage) {
}

public void setError(String message) {
contentPane.setStyle("-fx-background-color: -jr-red");
contentPane.getChildren().clear();
contentPane.setStyle("");
contentPane.setStyle("-fx-background-color: -jr-red");

VBox paneVBox = new VBox(10);
paneVBox.setMaxWidth(500);
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -25,11 +26,13 @@
import org.jabref.gui.autosaveandbackup.BackupManager;
import org.jabref.gui.importer.actions.loadchathistory.AiChatFile;
import org.jabref.gui.importer.actions.loadchathistory.AiChatFileMessage;
import org.jabref.gui.importer.actions.loadchathistory.LoadChatHistoryAction;
import org.jabref.gui.importer.actions.LoadChatHistoryAction;
import org.jabref.gui.maintable.BibEntryTableViewModel;
import org.jabref.gui.maintable.columns.MainTableColumn;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.ai.AiChatsFile;
import org.jabref.logic.ai.ChatMessage;
import org.jabref.logic.exporter.AtomicFileWriter;
import org.jabref.logic.exporter.BibDatabaseWriter;
import org.jabref.logic.exporter.BibWriter;
Expand Down Expand Up @@ -296,7 +299,7 @@ private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding,
// TODO: What to do with selectedOnly?

ObjectMapper objectMapper = new ObjectMapper();
AiChatFile aiChatFile = makeAiChatFile(bibDatabaseContext.getDatabase());
AiChatsFile aiChatFile = makeAiChatsFile(bibDatabaseContext.getDatabase());
objectMapper.writeValue(fileWriter, aiChatFile);

if (fileWriter.hasEncodingProblems()) {
Expand All @@ -312,19 +315,18 @@ private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding,
}
}

private AiChatFile makeAiChatFile(BibDatabase bibDatabase) {
AiChatFile aiChatFile = new AiChatFile();
aiChatFile.chatHistoryMap = new HashMap<>();
private AiChatsFile makeAiChatsFile(BibDatabase bibDatabase) {
Map<String, List<ChatMessage>> chatHistoryMap = new HashMap<>();

bibDatabase.getEntries().forEach(entry ->
entry.getCitationKey().ifPresent(citationKey -> {
List<AiChatFileMessage> aiChatFileMessages = entry.getAiChatMessages().stream().map(AiChatFileMessage::fromLangchain).toList();
aiChatFile.chatHistoryMap.put(citationKey, aiChatFileMessages);
List<ChatMessage> aiChatFileMessages = entry.getAiChatMessages();
chatHistoryMap.put(citationKey, aiChatFileMessages);
}
)
);

return aiChatFile;
return new AiChatsFile(chatHistoryMap);
}

private void saveWithDifferentEncoding(Path file, boolean selectedOnly, Charset encoding, Set<Character> encodingProblems, BibDatabaseWriter.SaveType saveType, SelfContainedSaveOrder saveOrder) throws SaveException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package org.jabref.gui.importer.actions.loadchathistory;
package org.jabref.gui.importer.actions;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.jabref.gui.DialogService;
import org.jabref.gui.importer.actions.GUIPostOpenAction;
import org.jabref.logic.ai.AiChatsFile;
import org.jabref.logic.ai.ChatMessage;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.entry.BibEntry;

import com.fasterxml.jackson.databind.ObjectMapper;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.UserMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -45,17 +42,17 @@ private void readAiChatsFile(BibDatabase bibDatabase, Path databasePath) {
try {
InputStream inputStream = new FileInputStream(chatFile);
ObjectMapper objectMapper = new ObjectMapper();
AiChatFile aiChatFile = objectMapper.readValue(inputStream, AiChatFile.class);
AiChatsFile aiChatFile = objectMapper.readValue(inputStream, AiChatsFile.class);
loadAiChatHistory(bibDatabase, aiChatFile);
} catch (FileNotFoundException e) {
LOGGER.info("There is no .aichats file for the opened library.");
LOGGER.info("There is no " + AI_CHAT_HISTORY_EXTENSION + " file for the opened library.");
} catch (IOException e) {
LOGGER.error("An error occurred while reading " + chatPath, e);
}
}

private void loadAiChatHistory(BibDatabase bibDatabase, AiChatFile aiChatFile) {
aiChatFile.chatHistoryMap.forEach((citationKey, chatHistory) -> {
private void loadAiChatHistory(BibDatabase bibDatabase, AiChatsFile aiChatFile) {
aiChatFile.getChatHistoryMap().forEach((citationKey, chatHistory) -> {
List<BibEntry> bibEntries = bibDatabase.getEntriesByCitationKey(citationKey);

if (bibEntries.isEmpty()) {
Expand All @@ -64,14 +61,9 @@ private void loadAiChatHistory(BibDatabase bibDatabase, AiChatFile aiChatFile) {
LOGGER.warn("Found a chat history for an bib entry with citation key \"" + citationKey + "\" but there are several bib entries in the database with the same key");
} else {
BibEntry bibEntry = bibEntries.getFirst();
List<ChatMessage> parsedChatMessages = parseChatMessages(chatHistory);
bibEntry.getAiChatMessages().addAll(parsedChatMessages);
bibEntry.getAiChatMessages().addAll(chatHistory);
}
});
}

private List<ChatMessage> parseChatMessages(List<AiChatFileMessage> chatHistory) {
return chatHistory.stream().map(AiChatFileMessage::toLangchainMessage).filter(Optional::isPresent).map(Optional::get).toList();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.autosaveandbackup.BackupManager;
import org.jabref.gui.dialogs.BackupUIManager;
import org.jabref.gui.importer.actions.loadchathistory.LoadChatHistoryAction;
import org.jabref.gui.shared.SharedDatabaseUIManager;
import org.jabref.gui.telemetry.Telemetry;
import org.jabref.gui.undo.CountingUndoManager;
Expand Down

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/ai/AiChat.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.jabref.logic.ai;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

import dev.langchain4j.chain.ConversationalRetrievalChain;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.data.segment.TextSegment;
Expand Down Expand Up @@ -70,6 +70,6 @@ public String execute(String prompt) {
}

public void restoreMessages(List<ChatMessage> messages) {
messages.forEach(this.chatMemory::add);
messages.stream().map(ChatMessage::toLangchainMessage).filter(Optional::isPresent).map(Optional::get).forEach(this.chatMemory::add);
}
}
20 changes: 20 additions & 0 deletions src/main/java/org/jabref/logic/ai/AiChatsFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jabref.logic.ai;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class AiChatsFile {
private final Map<String, List<ChatMessage>> chatHistoryMap;

@JsonCreator
public AiChatsFile(@JsonProperty("chatHistoryMap") Map<String, List<ChatMessage>> chatHistoryMap) {
this.chatHistoryMap = chatHistoryMap;
}

public Map<String, List<ChatMessage>> getChatHistoryMap() {
return chatHistoryMap;
}
}
4 changes: 0 additions & 4 deletions src/main/java/org/jabref/logic/ai/AiIngestor.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.jabref.logic.ai;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.file.Path;
import java.util.Optional;

import org.jabref.logic.JabRefException;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.logic.xmp.XmpUtilReader;
import org.jabref.model.database.BibDatabaseContext;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/org/jabref/logic/ai/AiService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import dev.langchain4j.model.embedding.AllMiniLmL6V2EmbeddingModel;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.store.memory.chat.ChatMemoryStore;
import dev.langchain4j.store.memory.chat.InMemoryChatMemoryStore;

/**
* This class maintains the connection to AI services.
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/org/jabref/logic/ai/ChatMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.jabref.logic.ai;

import java.util.Optional;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ChatMessage {
private static final Logger LOGGER = LoggerFactory.getLogger(ChatMessage.class);

private final ChatMessageType type;
private final String content;

@JsonCreator
public ChatMessage(@JsonProperty("type") ChatMessageType type,
@JsonProperty("content") String content) {
this.type = type;
this.content = content;
}

public ChatMessageType getType() {
return type;
}

public String getContent() {
return content;
}

public static Optional<ChatMessage> fromLangchain(dev.langchain4j.data.message.ChatMessage chatMessage) {
switch (chatMessage) {
case UserMessage userMessage -> {
return Optional.of(new ChatMessage(ChatMessageType.USER, userMessage.singleText()));
}
case AiMessage aiMessage -> {
return Optional.of(new ChatMessage(ChatMessageType.ASSISTANT, aiMessage.text()));
}
default -> {
LOGGER.error("Unable to convert langchain4j chat message to JabRef chat message, the type is {}", chatMessage.getClass());
return Optional.empty();
}
}
}

public Optional<dev.langchain4j.data.message.ChatMessage> toLangchainMessage() {
switch (type) {
case ChatMessageType.USER -> {
return Optional.of(new UserMessage(content));
}
case ChatMessageType.ASSISTANT -> {
return Optional.of(new AiMessage(content));
}
default -> {
LOGGER.error("Unable to convert JabRef chat message to langchain4j chat message, the type is {}", type);
return Optional.empty();
}
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/jabref/logic/ai/ChatMessageType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.jabref.logic.ai;

public enum ChatMessageType {
USER,
ASSISTANT,
}
5 changes: 1 addition & 4 deletions src/main/java/org/jabref/model/entry/BibEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import javafx.beans.Observable;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableMap;

import org.jabref.architecture.AllowedToUseLogic;
import org.jabref.logic.ai.ChatMessage;
import org.jabref.logic.bibtex.FileFieldWriter;
import org.jabref.logic.importer.util.FileFieldParser;
import org.jabref.model.FieldChange;
Expand Down