|
118 | 118 | import javax.swing.text.Position; |
119 | 119 | import javax.swing.tree.TreePath; |
120 | 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; |
125 | 121 | import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory; |
126 | 122 | import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea; |
127 | 123 | import org.fife.ui.rsyntaxtextarea.Theme; |
@@ -219,8 +215,7 @@ public class TextEditor extends JFrame implements ActionListener, |
219 | 215 | gotoLine, makeJar, makeJarWithSource, removeUnusedImports, sortImports, |
220 | 216 | removeTrailingWhitespace, findNext, findPrevious, openHelp, addImport, |
221 | 217 | nextError, previousError, openHelpWithoutFrames, nextTab, |
222 | | - previousTab, runSelection, extractSourceJar, askChatGPTtoGenerateCode, |
223 | | - openSourceForClass, |
| 218 | + previousTab, runSelection, extractSourceJar, openSourceForClass, |
224 | 219 | //openSourceForMenuItem, // this never had an actionListener!?? |
225 | 220 | openMacroFunctions, decreaseFontSize, increaseFontSize, chooseFontSize, |
226 | 221 | chooseTabSize, gitGrep, replaceTabsWithSpaces, |
@@ -518,10 +513,6 @@ public TextEditor(final Context context) { |
518 | 513 | //openSourceForMenuItem = addToMenu(toolsMenu, "Open Java File for Menu Item...", 0, 0); |
519 | 514 | //openSourceForMenuItem.setMnemonic(KeyEvent.VK_M); |
520 | 515 |
|
521 | | - GuiUtils.addMenubarSeparator(toolsMenu, "chatGPT"); |
522 | | - askChatGPTtoGenerateCode = addToMenu(toolsMenu, "Ask chatGPT...", 0, 0); |
523 | | - |
524 | | - |
525 | 516 | addScritpEditorMacroCommands(toolsMenu); |
526 | 517 | mbar.add(toolsMenu); |
527 | 518 |
|
@@ -1626,7 +1617,6 @@ else if (source == chooseTabSize) { |
1626 | 1617 | } |
1627 | 1618 | else if (source == openClassOrPackageHelp) openClassOrPackageHelp(null); |
1628 | 1619 | else if (source == extractSourceJar) extractSourceJar(); |
1629 | | - else if (source == askChatGPTtoGenerateCode) askChatGPTtoGenerateCode(); |
1630 | 1620 | else if (source == openSourceForClass) { |
1631 | 1621 | final String className = getSelectedClassNameOrAsk("Class (fully qualified name):", "Which Class?"); |
1632 | 1622 | if (className != null) { |
@@ -3243,98 +3233,6 @@ public void extractSourceJar() { |
3243 | 3233 | if (file != null) extractSourceJar(file); |
3244 | 3234 | } |
3245 | 3235 |
|
3246 | | - public void askChatGPTtoGenerateCode() { |
3247 | | - SwingUtilities.invokeLater(() -> { |
3248 | | - |
3249 | | - setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
3250 | | - |
3251 | | - // setup default prompt |
3252 | | - String prompt = |
3253 | | - promptPrefix() |
3254 | | - .replace("{programming_language}", getCurrentLanguage().getLanguageName() ) |
3255 | | - .replace("{custom_prompt}", getTextArea().getSelectedText()); |
3256 | | - |
3257 | | - String answer = askChatGPT(prompt); |
3258 | | - |
3259 | | - if (answer.contains("```")) { |
3260 | | - // clean answer by removing blabla outside the code block |
3261 | | - answer = answer.replace("```java", "```"); |
3262 | | - answer = answer.replace("```javascript", "```"); |
3263 | | - answer = answer.replace("```python", "```"); |
3264 | | - answer = answer.replace("```jython", "```"); |
3265 | | - answer = answer.replace("```macro", "```"); |
3266 | | - answer = answer.replace("```groovy", "```"); |
3267 | | - |
3268 | | - String[] temp = answer.split("```"); |
3269 | | - answer = temp[1]; |
3270 | | - } |
3271 | | - |
3272 | | - //getTextArea().insert(answer, getTextArea().getCaretPosition()); |
3273 | | - getTextArea().replaceSelection(answer + "\n"); |
3274 | | - setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); |
3275 | | - }); |
3276 | | - } |
3277 | | - |
3278 | | - private String askChatGPT(String text) { |
3279 | | - // Modified from: https://github.com/TheoKanning/openai-java/blob/main/example/src/main/java/example/OpenAiApiFunctionsExample.java |
3280 | | - String token = apiKey(); |
3281 | | - |
3282 | | - OpenAiService service = new OpenAiService(token); |
3283 | | - |
3284 | | - List<ChatMessage> messages = new ArrayList<>(); |
3285 | | - ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), text); |
3286 | | - messages.add(userMessage); |
3287 | | - |
3288 | | - ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest |
3289 | | - .builder() |
3290 | | - .model(modelName()) |
3291 | | - .messages(messages).build(); |
3292 | | - |
3293 | | - ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage(); |
3294 | | - messages.add(responseMessage); |
3295 | | - |
3296 | | - String result = responseMessage.getContent(); |
3297 | | - System.out.println(result); |
3298 | | - return result; |
3299 | | - } |
3300 | | - |
3301 | | - private String apiKey() { |
3302 | | - if (optionsService != null) { |
3303 | | - final OpenAIOptions openAIOptions = |
3304 | | - optionsService.getOptions(OpenAIOptions.class); |
3305 | | - if (openAIOptions != null) { |
3306 | | - final String key = openAIOptions.getOpenAIKey(); |
3307 | | - if (key != null && !key.isEmpty()) return key; |
3308 | | - } |
3309 | | - } |
3310 | | - return System.getenv("OPENAI_API_KEY"); |
3311 | | - } |
3312 | | - |
3313 | | - private String modelName() { |
3314 | | - if (optionsService != null) { |
3315 | | - final OpenAIOptions openAIOptions = |
3316 | | - optionsService.getOptions(OpenAIOptions.class); |
3317 | | - if (openAIOptions != null) { |
3318 | | - final String key = openAIOptions.getModelName(); |
3319 | | - if (key != null && !key.isEmpty()) return key; |
3320 | | - } |
3321 | | - } |
3322 | | - return null; |
3323 | | - } |
3324 | | - |
3325 | | - private String promptPrefix() { |
3326 | | - if (optionsService != null) { |
3327 | | - final OpenAIOptions openAIOptions = |
3328 | | - optionsService.getOptions(OpenAIOptions.class); |
3329 | | - if (openAIOptions != null) { |
3330 | | - final String promptPrefix = openAIOptions.getPromptPrefix(); |
3331 | | - if (promptPrefix != null && !promptPrefix.isEmpty()) return promptPrefix; |
3332 | | - } |
3333 | | - } |
3334 | | - return ""; |
3335 | | - } |
3336 | | - |
3337 | | - |
3338 | 3236 | public void extractSourceJar(final File file) { |
3339 | 3237 | try { |
3340 | 3238 | final FileFunctions functions = new FileFunctions(this); |
|
0 commit comments