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

Fixes OpenAI reset/incorrect API Key and release 1.6 #4

Merged
merged 3 commits into from
Sep 25, 2024
Merged
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: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.jeddict</groupId>
<artifactId>jeddict-ai</artifactId>
<version>1.5</version>
<version>1.6</version>
<packaging>nbm</packaging>
<name>Jeddict AI Assistant</name>
<description>Jeddict AI Assistant is a powerful and intuitive plugin designed for Apache NetBeans IDE.
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/io/github/jeddict/ai/JeddictChatModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.json.JSONArray;
import org.json.JSONObject;

public class JeddictChatModel {

private final OpenAiChatModel aiChatModel;
private int cachedClassDatasLength = -1; // Cache the length of classDatas
PreferencesManager preferencesManager = PreferencesManager.getInstance();

public JeddictChatModel() {
PreferencesManager preferencesManager = PreferencesManager.getInstance();
aiChatModel = OpenAiChatModel.builder()
.apiKey(preferencesManager.getApiKey())
.modelName(preferencesManager.getModelName())
Expand All @@ -62,10 +67,27 @@ private String generate(String prompt) {
errorMessage = jsonObject.getJSONObject("error").getString("message");
}
}
JOptionPane.showMessageDialog(null,
"AI assistance failed to generate the requested response: " + errorMessage,
"Error in AI Assistance",
JOptionPane.ERROR_MESSAGE);
if (errorMessage != null
&& errorMessage.toLowerCase().contains("incorrect api key")) {
JTextField apiKeyField = new JTextField(20);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); // Set layout to BoxLayout

panel.add(new JLabel("Incorrect API key. Please enter a new key:"));
panel.add(Box.createVerticalStrut(10)); // Add space between label and text field
panel.add(apiKeyField);

int option = JOptionPane.showConfirmDialog(null, panel,
"API Key Required", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
preferencesManager.setApiKey(apiKeyField.getText().trim());
}
} else {
JOptionPane.showMessageDialog(null,
"AI assistance failed to generate the requested response: " + errorMessage,
"Error in AI Assistance",
JOptionPane.ERROR_MESSAGE);
}
}
return null;
}
Expand Down Expand Up @@ -472,6 +494,9 @@ public List<String> suggestAnnotations(String classDatas, String classContent, S
}

public List<Snippet> parseJsonToSnippets(String jsonResponse) {
if(jsonResponse == null) {
return Collections.EMPTY_LIST;
}
List<Snippet> snippets = new ArrayList<>();

// Parse the JSON response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ public static PreferencesManager getInstance() {
}

public void clearApiKey() {
preferences.put(API_KEY_PREFERENCES, null);
preferences.remove(API_KEY_PREFERENCES);
}

public void setApiKey(String key) {
preferences.put(API_KEY_PREFERENCES, key);
}

public String getApiKey() {
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/github/jeddict/ai/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public static String convertToCapitalized(String input) {
}

public static String removeCodeBlockMarkers(String input) {
// Check if the input starts and ends with the markers
if(input == null) {
return null;
}
input = input.trim();
if (input.startsWith("```java") && input.endsWith("```")) {
String content = input.substring(7); // Remove ```java\n (7 characters)
Expand Down
Loading