|
106 | 106 |
|
107 | 107 | import javax.script.ScriptEngine;
|
108 | 108 | import javax.script.ScriptException;
|
109 |
| -import javax.swing.AbstractAction; |
110 |
| -import javax.swing.AbstractButton; |
111 |
| -import javax.swing.BoxLayout; |
112 |
| -import javax.swing.ButtonGroup; |
113 |
| -import javax.swing.JButton; |
114 |
| -import javax.swing.JCheckBoxMenuItem; |
115 |
| -import javax.swing.JDialog; |
116 |
| -import javax.swing.JFrame; |
117 |
| -import javax.swing.JLabel; |
118 |
| -import javax.swing.JMenu; |
119 |
| -import javax.swing.JMenuBar; |
120 |
| -import javax.swing.JMenuItem; |
121 |
| -import javax.swing.JOptionPane; |
122 |
| -import javax.swing.JPanel; |
123 |
| -import javax.swing.JPopupMenu; |
124 |
| -import javax.swing.JRadioButtonMenuItem; |
125 |
| -import javax.swing.JScrollPane; |
126 |
| -import javax.swing.JSplitPane; |
127 |
| -import javax.swing.JTabbedPane; |
128 |
| -import javax.swing.JTextArea; |
129 |
| -import javax.swing.JTextField; |
130 |
| -import javax.swing.JTextPane; |
131 |
| -import javax.swing.JTree; |
132 |
| -import javax.swing.KeyStroke; |
133 |
| -import javax.swing.SwingUtilities; |
134 |
| -import javax.swing.UIManager; |
| 109 | +import javax.swing.*; |
| 110 | +import javax.swing.border.BevelBorder; |
135 | 111 | import javax.swing.event.ChangeEvent;
|
136 | 112 | import javax.swing.event.ChangeListener;
|
137 | 113 | import javax.swing.event.DocumentEvent;
|
|
142 | 118 | import javax.swing.text.Position;
|
143 | 119 | import javax.swing.tree.TreePath;
|
144 | 120 |
|
| 121 | +import com.theokanning.openai.completion.chat.ChatCompletionRequest; |
| 122 | +import com.theokanning.openai.completion.chat.ChatMessage; |
| 123 | +import com.theokanning.openai.completion.chat.ChatMessageRole; |
| 124 | +import com.theokanning.openai.service.OpenAiService; |
145 | 125 | import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
|
146 | 126 | import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
|
147 | 127 | import org.fife.ui.rsyntaxtextarea.Theme;
|
@@ -238,7 +218,7 @@ public class TextEditor extends JFrame implements ActionListener,
|
238 | 218 | gotoLine, makeJar, makeJarWithSource, removeUnusedImports, sortImports,
|
239 | 219 | removeTrailingWhitespace, findNext, findPrevious, openHelp, addImport,
|
240 | 220 | nextError, previousError, openHelpWithoutFrames, nextTab,
|
241 |
| - previousTab, runSelection, extractSourceJar, |
| 221 | + previousTab, runSelection, extractSourceJar, askChatGPTtoGenerateCode, |
242 | 222 | openSourceForClass,
|
243 | 223 | //openSourceForMenuItem, // this never had an actionListener!??
|
244 | 224 | openMacroFunctions, decreaseFontSize, increaseFontSize, chooseFontSize,
|
@@ -534,6 +514,11 @@ public TextEditor(final Context context) {
|
534 | 514 | openSourceForClass.setMnemonic(KeyEvent.VK_J);
|
535 | 515 | //openSourceForMenuItem = addToMenu(toolsMenu, "Open Java File for Menu Item...", 0, 0);
|
536 | 516 | //openSourceForMenuItem.setMnemonic(KeyEvent.VK_M);
|
| 517 | + |
| 518 | + GuiUtils.addMenubarSeparator(toolsMenu, "chatGPT"); |
| 519 | + askChatGPTtoGenerateCode = addToMenu(toolsMenu, "Ask chatGPT...", 0, 0); |
| 520 | + |
| 521 | + |
537 | 522 | addScritpEditorMacroCommands(toolsMenu);
|
538 | 523 | mbar.add(toolsMenu);
|
539 | 524 |
|
@@ -1638,6 +1623,7 @@ else if (source == chooseTabSize) {
|
1638 | 1623 | }
|
1639 | 1624 | else if (source == openClassOrPackageHelp) openClassOrPackageHelp(null);
|
1640 | 1625 | else if (source == extractSourceJar) extractSourceJar();
|
| 1626 | + else if (source == askChatGPTtoGenerateCode) askChatGPTtoGenerateCode(); |
1641 | 1627 | else if (source == openSourceForClass) {
|
1642 | 1628 | final String className = getSelectedClassNameOrAsk("Class (fully qualified name):", "Which Class?");
|
1643 | 1629 | if (className != null) {
|
@@ -3254,6 +3240,60 @@ public void extractSourceJar() {
|
3254 | 3240 | if (file != null) extractSourceJar(file);
|
3255 | 3241 | }
|
3256 | 3242 |
|
| 3243 | + public void askChatGPTtoGenerateCode() { |
| 3244 | + SwingUtilities.invokeLater(() -> { |
| 3245 | + |
| 3246 | + setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
| 3247 | + |
| 3248 | + // setup default prompt |
| 3249 | + String prompt = "Write concise and high quality code in " + getCurrentLanguage().getLanguageName() + " for ImageJ/Fiji.\n" + |
| 3250 | + "The code should do the following:\n" + |
| 3251 | + getTextArea().getSelectedText(); |
| 3252 | + |
| 3253 | + String answer = askChatGPT(prompt); |
| 3254 | + |
| 3255 | + if (answer.contains("```")) { |
| 3256 | + // clean answer by removing blabla outside the code block |
| 3257 | + answer = answer.replace("```java", "```"); |
| 3258 | + answer = answer.replace("```javascript", "```"); |
| 3259 | + answer = answer.replace("```python", "```"); |
| 3260 | + answer = answer.replace("```jython", "```"); |
| 3261 | + answer = answer.replace("```macro", "```"); |
| 3262 | + answer = answer.replace("```groovy", "```"); |
| 3263 | + |
| 3264 | + String[] temp = answer.split("```"); |
| 3265 | + answer = temp[1]; |
| 3266 | + } |
| 3267 | + |
| 3268 | + //getTextArea().insert(answer, getTextArea().getCaretPosition()); |
| 3269 | + getTextArea().replaceSelection(answer); |
| 3270 | + setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
| 3271 | + }); |
| 3272 | + } |
| 3273 | + |
| 3274 | + private String askChatGPT(String text) { |
| 3275 | + // Modified from: https://github.com/TheoKanning/openai-java/blob/main/example/src/main/java/example/OpenAiApiFunctionsExample.java |
| 3276 | + String token = System.getenv("OPENAI_API_KEY"); |
| 3277 | + |
| 3278 | + OpenAiService service = new OpenAiService(token); |
| 3279 | + |
| 3280 | + List<ChatMessage> messages = new ArrayList<>(); |
| 3281 | + ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), text); |
| 3282 | + messages.add(userMessage); |
| 3283 | + |
| 3284 | + ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest |
| 3285 | + .builder() |
| 3286 | + .model("gpt-3.5-turbo-0613") |
| 3287 | + .messages(messages).build(); |
| 3288 | + |
| 3289 | + ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage(); |
| 3290 | + messages.add(responseMessage); |
| 3291 | + |
| 3292 | + String result = responseMessage.getContent(); |
| 3293 | + System.out.println(result); |
| 3294 | + return result; |
| 3295 | + } |
| 3296 | + |
3257 | 3297 | public void extractSourceJar(final File file) {
|
3258 | 3298 | try {
|
3259 | 3299 | final FileFunctions functions = new FileFunctions(this);
|
|
0 commit comments