From 098d56d88d86d18e2728eae157c9ac335e8e19f8 Mon Sep 17 00:00:00 2001 From: Robert Haase Date: Sat, 24 Aug 2024 16:42:24 +0200 Subject: [PATCH 1/2] support for ollama-based AI-assisted coding --- .../ui/swing/script/LLMServicesOptions.java | 22 +++++++++++++ .../scijava/ui/swing/script/TextEditor.java | 33 ++++++++++++++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java b/src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java index 6073412..638fd1f 100644 --- a/src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java +++ b/src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java @@ -50,6 +50,12 @@ public class LLMServicesOptions extends OptionsPlugin { @Parameter(label = "OpenAI Model name") private String openAIModelName = "gpt-4o-2024-08-06"; + @Parameter(label = "Ollama Model name") + private String ollamaModelName = "codegemma:7b-instruct"; + + @Parameter(label = "Ollama Url") + private String ollamaUrl = "http://localhost:11434/v1"; + @Parameter(label = "Anthropic Model name") private String anthropicModelName = "claude-3-5-sonnet-20240620"; @@ -93,6 +99,15 @@ public void setOpenAIModelName(final String openAIModelName) { this.openAIModelName = openAIModelName; } + public String getOllamaModelName() { + return ollamaModelName; + } + + public void setOllamaModelName(final String ollamaModelName) { + this.ollamaModelName = ollamaModelName; + } + + public String getAnthropidModelName() { return anthropicModelName; } @@ -101,5 +116,12 @@ public void setAnthropidModelName(final String anthropicModelName) { this.anthropicModelName = anthropicModelName; } + public String getOllamaUrl() { + return ollamaUrl; + } + + public void setOllamaUrl(final String ollamaUrl) { + this.ollamaUrl = ollamaUrl; + } } diff --git a/src/main/java/org/scijava/ui/swing/script/TextEditor.java b/src/main/java/org/scijava/ui/swing/script/TextEditor.java index a2cff1a..a304ef6 100644 --- a/src/main/java/org/scijava/ui/swing/script/TextEditor.java +++ b/src/main/java/org/scijava/ui/swing/script/TextEditor.java @@ -215,7 +215,7 @@ public class TextEditor extends JFrame implements ActionListener, removeTrailingWhitespace, findNext, findPrevious, openHelp, addImport, nextError, previousError, openHelpWithoutFrames, nextTab, previousTab, runSelection, extractSourceJar, askChatGPTtoGenerateCode, - askClaudetoGenerateCode, openSourceForClass, + askOllamatoGenerateCode, askClaudetoGenerateCode, openSourceForClass, //openSourceForMenuItem, // this never had an actionListener!?? openMacroFunctions, decreaseFontSize, increaseFontSize, chooseFontSize, chooseTabSize, gitGrep, replaceTabsWithSpaces, @@ -515,6 +515,7 @@ public TextEditor(final Context context) { GuiUtils.addMenubarSeparator(toolsMenu, "AI assistance"); askChatGPTtoGenerateCode = addToMenu(toolsMenu, "Ask OpenAI's chatGPT...", 0, 0); + askOllamatoGenerateCode = addToMenu(toolsMenu, "Ask Ollama...", 0, 0 ); askClaudetoGenerateCode = addToMenu(toolsMenu, "Ask Anthropic's Claude...", 0, 0 ); @@ -1623,6 +1624,7 @@ else if (source == chooseTabSize) { else if (source == openClassOrPackageHelp) openClassOrPackageHelp(null); else if (source == extractSourceJar) extractSourceJar(); else if (source == askChatGPTtoGenerateCode) askChatGPTtoGenerateCode(); + else if (source == askOllamatoGenerateCode) askOllamatoGenerateCode(); else if (source == askClaudetoGenerateCode) askClaudetoGenerateCode(); else if (source == openSourceForClass) { final String className = getSelectedClassNameOrAsk("Class (fully qualified name):", "Which Class?"); @@ -3243,6 +3245,9 @@ public void extractSourceJar() { public void askChatGPTtoGenerateCode() { askLLMServiceProviderToGenerateCode("openai"); } + public void askOllamatoGenerateCode() { + askLLMServiceProviderToGenerateCode("ollama"); + } public void askClaudetoGenerateCode() { askLLMServiceProviderToGenerateCode("anthropic"); } @@ -3268,6 +3273,8 @@ public void askLLMServiceProviderToGenerateCode(String service_provider) { try { if (service_provider.equals("openai")) { answer = new OpenAIClient().prompt(prompt, openAIModelName(), openaiApiKey(), null); + } else if (service_provider.equals("ollama")) { + answer = new OpenAIClient().prompt(prompt, ollamaModelName(), null, ollamaUrl()); } else if (service_provider.equals("anthropic")) { answer = new ClaudeApiClient().prompt(prompt, anthropicModelName(), anthropicApiKey(), null); } else { @@ -3334,6 +3341,30 @@ private String openAIModelName() { return null; } + private String ollamaModelName() { + if (optionsService != null) { + final LLMServicesOptions llmOptions = + optionsService.getOptions(LLMServicesOptions.class); + if (llmOptions != null) { + final String key = llmOptions.getOllamaModelName(); + if (key != null && !key.isEmpty()) return key; + } + } + return null; + } + + private String ollamaUrl() { + if (optionsService != null) { + final LLMServicesOptions llmOptions = + optionsService.getOptions(LLMServicesOptions.class); + if (llmOptions != null) { + final String key = llmOptions.getOllamaUrl(); + if (key != null && !key.isEmpty()) return key; + } + } + return null; + } + private String anthropicModelName() { if (optionsService != null) { final LLMServicesOptions llmOptions = From 0bb97a2e56804758c63043f210330c659f8f0296 Mon Sep 17 00:00:00 2001 From: Robert Haase Date: Wed, 28 Aug 2024 14:19:57 +0200 Subject: [PATCH 2/2] Correctex default ollama url --- .../java/org/scijava/ui/swing/script/LLMServicesOptions.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java b/src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java index 638fd1f..b6a8df2 100644 --- a/src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java +++ b/src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java @@ -54,7 +54,7 @@ public class LLMServicesOptions extends OptionsPlugin { private String ollamaModelName = "codegemma:7b-instruct"; @Parameter(label = "Ollama Url") - private String ollamaUrl = "http://localhost:11434/v1"; + private String ollamaUrl = "http://localhost:11434/v1/chat/completions"; @Parameter(label = "Anthropic Model name") private String anthropicModelName = "claude-3-5-sonnet-20240620";