Skip to content

Commit

Permalink
Fix for InAnYan#114
Browse files Browse the repository at this point in the history
  • Loading branch information
InAnYan committed Aug 9, 2024
1 parent a38c439 commit 27f941b
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/entryeditor/AiChatTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ protected void handleFocus() {
protected void bindToEntry(BibEntry entry) {
if (!aiService.getPreferences().getEnableAi()) {
showPrivacyNotice(entry);
} else if (aiService.getPreferences().getSelectedApiToken().isEmpty()) {
} else if (aiService.getPreferences().getSelectedApiKey().isEmpty()) {
showApiKeyMissing();
} else if (entry.getFiles().isEmpty()) {
showErrorNoFiles();
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 @@ -82,7 +82,7 @@ protected void handleFocus() {
protected void bindToEntry(BibEntry entry) {
if (!aiService.getPreferences().getEnableAi()) {
showPrivacyNotice(entry);
} else if (aiService.getPreferences().getSelectedApiToken().isEmpty()) {
} else if (aiService.getPreferences().getSelectedApiKey().isEmpty()) {
showApiKeyMissing();
} else if (bibDatabaseContext.getDatabasePath().isEmpty()) {
showErrorNoDatabasePath();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/preferences/ai/AiTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void initialize() {
}
});

apiKeyTextField.textProperty().bindBidirectional(viewModel.apiTokenProperty());
apiKeyTextField.textProperty().bindBidirectional(viewModel.apiKeyProperty());
apiKeyTextField.disableProperty().bind(viewModel.disableBasicSettingsProperty());

customizeExpertSettingsCheckbox.selectedProperty().bindBidirectional(viewModel.customizeExpertSettingsProperty());
Expand Down
68 changes: 34 additions & 34 deletions src/main/java/org/jabref/gui/preferences/ai/AiTabViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ public class AiTabViewModel implements PreferenceTabViewModel {
private final StringProperty mistralAiChatModel = new SimpleStringProperty();
private final StringProperty huggingFaceChatModel = new SimpleStringProperty();

private final StringProperty currentApiToken = new SimpleStringProperty();
private final StringProperty currentApiKey = new SimpleStringProperty();

private final StringProperty openAiApiToken = new SimpleStringProperty();
private final StringProperty mistralAiApiToken = new SimpleStringProperty();
private final StringProperty huggingFaceApiToken = new SimpleStringProperty();
private final StringProperty openAiApiKey = new SimpleStringProperty();
private final StringProperty mistralAiApiKey = new SimpleStringProperty();
private final StringProperty huggingFaceApiKey = new SimpleStringProperty();

private final BooleanProperty customizeExpertSettings = new SimpleBooleanProperty();

Expand Down Expand Up @@ -78,7 +78,7 @@ public class AiTabViewModel implements PreferenceTabViewModel {

private final AiPreferences aiPreferences;

private final Validator apiTokenValidator;
private final Validator apiKeyValidator;
private final Validator chatModelValidator;
private final Validator apiBaseUrlValidator;
private final Validator instructionValidator;
Expand All @@ -97,22 +97,22 @@ public AiTabViewModel(PreferencesService preferencesService) {
disableExpertSettings.set(!newValue || !customizeExpertSettings.get());

if (newValue) {
openAiApiToken.set(preferencesService.getApiKeyForAiProvider(AiPreferences.AiProvider.OPEN_AI));
mistralAiApiToken.set(preferencesService.getApiKeyForAiProvider(AiPreferences.AiProvider.MISTRAL_AI));
huggingFaceApiToken.set(preferencesService.getApiKeyForAiProvider(AiPreferences.AiProvider.HUGGING_FACE));
openAiApiKey.set(preferencesService.getApiKeyForAiProvider(AiPreferences.AiProvider.OPEN_AI));
mistralAiApiKey.set(preferencesService.getApiKeyForAiProvider(AiPreferences.AiProvider.MISTRAL_AI));
huggingFaceApiKey.set(preferencesService.getApiKeyForAiProvider(AiPreferences.AiProvider.HUGGING_FACE));

if (selectedAiProvider.get() != null) {
switch (selectedAiProvider.get()) {
case OPEN_AI -> {
currentApiToken.set(openAiApiToken.get());
currentApiKey.set(openAiApiKey.get());
}

case MISTRAL_AI -> {
currentApiToken.set(mistralAiApiToken.get());
currentApiKey.set(mistralAiApiKey.get());
}

case HUGGING_FACE -> {
currentApiToken.set(huggingFaceApiToken.get());
currentApiKey.set(huggingFaceApiKey.get());
}
}
}
Expand All @@ -138,18 +138,18 @@ public AiTabViewModel(PreferencesService preferencesService) {
switch (oldValue) {
case OPEN_AI -> {
openAiChatModel.set(oldChatModel);
openAiApiToken.set(currentApiToken.get());
openAiApiKey.set(currentApiKey.get());
openAiApiBaseUrl.set(currentApiBaseUrl.get());
}

case MISTRAL_AI -> {
mistralAiChatModel.set(oldChatModel);
mistralAiApiToken.set(currentApiToken.get());
mistralAiApiKey.set(currentApiKey.get());
mistralAiApiBaseUrl.set(currentApiBaseUrl.get()); }

case HUGGING_FACE -> {
huggingFaceChatModel.set(oldChatModel);
huggingFaceApiToken.set(currentApiToken.get());
huggingFaceApiKey.set(currentApiKey.get());
huggingFaceApiBaseUrl.set(currentApiBaseUrl.get());
}
}
Expand All @@ -158,19 +158,19 @@ public AiTabViewModel(PreferencesService preferencesService) {
switch (newValue) {
case OPEN_AI -> {
currentChatModel.set(openAiChatModel.get());
currentApiToken.set(openAiApiToken.get());
currentApiKey.set(openAiApiKey.get());
currentApiBaseUrl.set(openAiApiBaseUrl.get());
}

case MISTRAL_AI -> {
currentChatModel.set(mistralAiChatModel.get());
currentApiToken.set(mistralAiApiToken.get());
currentApiKey.set(mistralAiApiKey.get());
currentApiBaseUrl.set(mistralAiApiBaseUrl.get());
}

case HUGGING_FACE -> {
currentChatModel.set(huggingFaceChatModel.get());
currentApiToken.set(huggingFaceApiToken.get());
currentApiKey.set(huggingFaceApiKey.get());
currentApiBaseUrl.set(huggingFaceApiBaseUrl.get());
}
}
Expand All @@ -193,11 +193,11 @@ public AiTabViewModel(PreferencesService preferencesService) {
contextWindowSize.set(modelContextWindows.getOrDefault(newValue, AiDefaultPreferences.CONTEXT_WINDOW_SIZE));
});

this.currentApiToken.addListener((observable, oldValue, newValue) -> {
this.currentApiKey.addListener((observable, oldValue, newValue) -> {
switch (selectedAiProvider.get()) {
case OPEN_AI -> openAiApiToken.set(newValue);
case MISTRAL_AI -> mistralAiApiToken.set(newValue);
case HUGGING_FACE -> huggingFaceApiToken.set(newValue);
case OPEN_AI -> openAiApiKey.set(newValue);
case MISTRAL_AI -> mistralAiApiKey.set(newValue);
case HUGGING_FACE -> huggingFaceApiKey.set(newValue);
}
});

Expand All @@ -209,10 +209,10 @@ public AiTabViewModel(PreferencesService preferencesService) {
}
});

this.apiTokenValidator = new FunctionBasedValidator<>(
currentApiToken,
this.apiKeyValidator = new FunctionBasedValidator<>(
currentApiKey,
token -> !StringUtil.isBlank(token),
ValidationMessage.error(Localization.lang("An OpenAI token has to be provided")));
ValidationMessage.error(Localization.lang("An API key has to be provided")));

this.chatModelValidator = new FunctionBasedValidator<>(
currentChatModel,
Expand Down Expand Up @@ -269,9 +269,9 @@ public void setValues() {
mistralAiChatModel.setValue(aiPreferences.getMistralAiChatModel());
huggingFaceChatModel.setValue(aiPreferences.getHuggingFaceChatModel());

openAiApiToken.setValue(aiPreferences.getOpenAiApiToken());
mistralAiApiToken.setValue(aiPreferences.getMistralAiApiToken());
huggingFaceApiToken.setValue(aiPreferences.getHuggingFaceApiToken());
openAiApiKey.setValue(aiPreferences.getOpenAiApiKey());
mistralAiApiKey.setValue(aiPreferences.getMistralAiApiKey());
huggingFaceApiKey.setValue(aiPreferences.getHuggingFaceApiKey());

customizeExpertSettings.setValue(aiPreferences.getCustomizeExpertSettings());

Expand Down Expand Up @@ -302,9 +302,9 @@ public void storeSettings() {
aiPreferences.setMistralAiChatModel(mistralAiChatModel.get() == null ? "" : mistralAiChatModel.get());
aiPreferences.setHuggingFaceChatModel(huggingFaceChatModel.get() == null ? "" : huggingFaceChatModel.get());

aiPreferences.setOpenAiApiToken(openAiApiToken.get() == null ? "" : openAiApiToken.get());
aiPreferences.setMistralAiApiToken(mistralAiApiToken.get() == null ? "" : mistralAiApiToken.get());
aiPreferences.setHuggingFaceApiToken(huggingFaceApiToken.get() == null ? "" : huggingFaceApiToken.get());
aiPreferences.setOpenAiApiKey(openAiApiKey.get() == null ? "" : openAiApiKey.get());
aiPreferences.setMistralAiApiKey(mistralAiApiKey.get() == null ? "" : mistralAiApiKey.get());
aiPreferences.setHuggingFaceApiKey(huggingFaceApiKey.get() == null ? "" : huggingFaceApiKey.get());

aiPreferences.setCustomizeExpertSettings(customizeExpertSettings.get());

Expand Down Expand Up @@ -355,7 +355,7 @@ public boolean validateSettings() {
public boolean validateBasicSettings() {
List<Validator> validators = List.of(
chatModelValidator,
apiTokenValidator
apiKeyValidator
);

return validators.stream().map(Validator::getValidationStatus).allMatch(ValidationStatus::isValid);
Expand Down Expand Up @@ -400,8 +400,8 @@ public StringProperty selectedChatModelProperty() {
return currentChatModel;
}

public StringProperty apiTokenProperty() {
return currentApiToken;
public StringProperty apiKeyProperty() {
return currentApiKey;
}

public BooleanProperty customizeExpertSettingsProperty() {
Expand Down Expand Up @@ -461,7 +461,7 @@ public BooleanProperty disableExpertSettingsProperty() {
}

public ValidationStatus getApiTokenValidationStatus() {
return apiTokenValidator.getValidationStatus();
return apiKeyValidator.getValidationStatus();
}

public ValidationStatus getChatModelValidationStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public JabRefChatLanguageModel(AiPreferences aiPreferences) {
* and using {@link org.jabref.logic.ai.chathistory.BibDatabaseChatHistoryManager}, where messages are stored in {@link MVStore}.
*/
private void rebuild() {
if (!aiPreferences.getEnableAi() || aiPreferences.getSelectedApiToken().isEmpty()) {
if (!aiPreferences.getEnableAi() || aiPreferences.getSelectedApiKey().isEmpty()) {
langchainChatModel = Optional.empty();
return;
}
Expand All @@ -69,7 +69,7 @@ private void rebuild() {
case MISTRAL_AI -> {
langchainChatModel = Optional.of(MistralAiChatModel
.builder()
.apiKey(aiPreferences.getSelectedApiToken())
.apiKey(aiPreferences.getSelectedApiKey())
.modelName(aiPreferences.getSelectedChatModel())
.temperature(aiPreferences.getTemperature())
.baseUrl(aiPreferences.getSelectedApiBaseUrl())
Expand All @@ -83,7 +83,7 @@ private void rebuild() {
// NOTE: {@link HuggingFaceChatModel} doesn't support API base url :(
langchainChatModel = Optional.of(HuggingFaceChatModel
.builder()
.accessToken(aiPreferences.getSelectedApiToken())
.accessToken(aiPreferences.getSelectedApiKey())
.modelId(aiPreferences.getSelectedChatModel())
.temperature(aiPreferences.getTemperature())
.timeout(Duration.ofMinutes(2))
Expand Down Expand Up @@ -116,7 +116,7 @@ public Response<AiMessage> generate(List<ChatMessage> list) {
if (langchainChatModel.isEmpty()) {
if (!aiPreferences.getEnableAi()) {
throw new RuntimeException(Localization.lang("In order to use AI chat, you need to enable chatting with attached PDF files in JabRef preferences (AI tab)."));
} else if (aiPreferences.getSelectedApiToken().isEmpty()) {
} else if (aiPreferences.getSelectedApiKey().isEmpty()) {
throw new RuntimeException(Localization.lang("In order to use AI chat, set OpenAI API key inside JabRef preferences (AI tab)."));
} else {
throw new RuntimeException(Localization.lang("Unable to chat with AI."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public JvmOpenAiChatLanguageModel(AiPreferences aiPreferences, HttpClient httpCl
this.aiPreferences = aiPreferences;

OpenAI openAI = OpenAI
.newBuilder(aiPreferences.getSelectedApiToken())
.newBuilder(aiPreferences.getSelectedApiKey())
.httpClient(httpClient)
.baseUrl(aiPreferences.getSelectedApiBaseUrl())
.build();
Expand Down
Loading

0 comments on commit 27f941b

Please sign in to comment.