Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

### Added

- We added a "Regenerate" button for the AI chat allowing the user to make the LLM reformulate its answer to the previous prompt [#12191](https://github.com/JabRef/jabref/issues/12191)

### Changed

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.google.common.annotations.VisibleForTesting;
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.controlsfx.control.PopOver;
import org.slf4j.Logger;
Expand Down Expand Up @@ -191,6 +192,20 @@ private void initializeChatPrompt() {
onSendMessage(userMessage);
});

chatPrompt.setRegenerateCallback(() -> {
String lastUserPrompt = "";
if (!aiChatLogic.getChatHistory().isEmpty()) {
lastUserPrompt = getLastUserMessage().singleText();
deleteLastMessage();
deleteLastMessage();
}

chatPrompt.switchToNormalState();
if (!lastUserPrompt.isEmpty()) {
onSendMessage(lastUserPrompt);
}
});

chatPrompt.requestPromptFocus();

updatePromptHistory();
Expand Down Expand Up @@ -334,4 +349,19 @@ private void deleteLastMessage() {
aiChatLogic.getChatHistory().remove(index);
}
}

private UserMessage getLastUserMessage() {
if (!aiChatLogic.getChatHistory().isEmpty() && aiChatLogic.getChatHistory().size() >= 2) {
int userMessageIndex = aiChatLogic.getChatHistory().size() - 2;
ChatMessage chat = aiChatLogic.getChatHistory().get(userMessageIndex);

if (chat.type() == ChatMessageType.USER) {
return (UserMessage) chat;
} else {
return new UserMessage("");
}
} else {
return new UserMessage("");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ChatPromptComponent extends HBox {
private final ObjectProperty<Consumer<String>> sendCallback = new SimpleObjectProperty<>();
private final ObjectProperty<Consumer<String>> retryCallback = new SimpleObjectProperty<>();
private final ObjectProperty<Runnable> cancelCallback = new SimpleObjectProperty<>();
private final ObjectProperty<Runnable> regenerateCallback = new SimpleObjectProperty<>();

private final ListProperty<String> history = new SimpleListProperty<>(FXCollections.observableArrayList());

Expand All @@ -44,6 +45,7 @@ public class ChatPromptComponent extends HBox {

@FXML private ExpandingTextArea userPromptTextArea;
@FXML private Button submitButton;
@FXML private Button regenerateButton;

public ChatPromptComponent() {
ViewLoader.view(this)
Expand All @@ -68,6 +70,10 @@ public void setCancelCallback(Runnable cancelCallback) {
this.cancelCallback.set(cancelCallback);
}

public void setRegenerateCallback(Runnable regenerateCallback) {
this.regenerateCallback.set(regenerateCallback);
}

public ListProperty<String> getHistory() {
return history;
}
Expand Down Expand Up @@ -174,6 +180,7 @@ public void switchToNormalState() {
this.getChildren().clear();
this.getChildren().add(userPromptTextArea);
this.getChildren().add(submitButton);
this.getChildren().add(regenerateButton);
requestPromptFocus();
}

Expand All @@ -191,4 +198,11 @@ private void onSendMessage() {
sendCallback.get().accept(userPrompt);
}
}

@FXML
private void onRegenerateMessage() {
if (regenerateCallback.get() != null) {
regenerateCallback.get().run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
mnemonicParsing="false"
onAction="#onSendMessage"
text="%Submit"/>
<Button fx:id="regenerateButton"
mnemonicParsing="false"
onAction="#onRegenerateMessage"
text="%Regenerate"/>
</fx:root>
Loading