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

Ollama-compatible AI-assisted code generation #71

Draft
wants to merge 2 commits into
base: chatgpt-claude
Choose a base branch
from
Draft
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
22 changes: 22 additions & 0 deletions src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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/chat/completions";

@Parameter(label = "Anthropic Model name")
private String anthropicModelName = "claude-3-5-sonnet-20240620";

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}

}
33 changes: 32 additions & 1 deletion src/main/java/org/scijava/ui/swing/script/TextEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 );


Expand Down Expand Up @@ -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?");
Expand Down Expand Up @@ -3243,6 +3245,9 @@ public void extractSourceJar() {
public void askChatGPTtoGenerateCode() {
askLLMServiceProviderToGenerateCode("openai");
}
public void askOllamatoGenerateCode() {
askLLMServiceProviderToGenerateCode("ollama");
}
public void askClaudetoGenerateCode() {
askLLMServiceProviderToGenerateCode("anthropic");
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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 =
Expand Down