Skip to content

Commit

Permalink
Fixes OpenAI reset/incorrect API Key and release 1.6 (#4)
Browse files Browse the repository at this point in the history
Fixes OpenAI reset/incorrect API Key and release 1.6
  • Loading branch information
jShiwaniGupta authored Sep 25, 2024
1 parent 05aa78f commit a961972
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
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

0 comments on commit a961972

Please sign in to comment.