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

make OpenAI API key optional and prompt prefix, model name configurable #68

Merged
merged 1 commit into from
Nov 28, 2023
Merged
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
30 changes: 29 additions & 1 deletion src/main/java/org/scijava/ui/swing/script/OpenAIOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,42 @@
@Plugin(type = OptionsPlugin.class, menuPath = "Edit>Options>OpenAI...")
public class OpenAIOptions extends OptionsPlugin {

@Parameter(label = "OpenAI key")
@Parameter(label = "OpenAI key", required = false)
private String openAIKey;

@Parameter(label = "OpenAI Model name")
private String modelName = "gpt-3.5-turbo-0613";

@Parameter(label = "Prompt prefix (added before custom prompt)", style = "text area")
private String promptPrefix = "Write code in {programming_language}.\n" +
"Write concise and high quality code for ImageJ/Fiji.\n" +
"Put minimal comments explaining what the code does.\n" +
"The code should do the following:\n" +
"{custom_prompt}";

public String getOpenAIKey() {
return openAIKey;
}

public void setOpenAIKey(final String openAIKey) {
this.openAIKey = openAIKey;
}

public String getPromptPrefix() {
return promptPrefix;
}

public void setPromptPrefix(final String promptPrefix) {
this.promptPrefix = promptPrefix;
}

public String getModelName() {
return modelName;
}

public void setModelName(final String modelName) {
this.modelName = modelName;
}


}
35 changes: 29 additions & 6 deletions src/main/java/org/scijava/ui/swing/script/TextEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -3250,11 +3250,9 @@ public void askChatGPTtoGenerateCode() {

// setup default prompt
String prompt =
"Write code in " + getCurrentLanguage().getLanguageName() + ".\n" +
"Write concise and high quality code for ImageJ/Fiji.\n" +
"Put minimal comments explaining what the code does.\n" +
"The code should do the following:\n" +
getTextArea().getSelectedText();
promptPrefix()
.replace("{programming_language}", getCurrentLanguage().getLanguageName() )
.replace("{custom_prompt}", getTextArea().getSelectedText());

String answer = askChatGPT(prompt);

Expand Down Expand Up @@ -3289,7 +3287,7 @@ private String askChatGPT(String text) {

ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
.builder()
.model("gpt-3.5-turbo-0613")
.model(modelName())
.messages(messages).build();

ChatMessage responseMessage = service.createChatCompletion(chatCompletionRequest).getChoices().get(0).getMessage();
Expand All @@ -3312,6 +3310,31 @@ private String apiKey() {
return System.getenv("OPENAI_API_KEY");
}

private String modelName() {
if (optionsService != null) {
final OpenAIOptions openAIOptions =
optionsService.getOptions(OpenAIOptions.class);
if (openAIOptions != null) {
final String key = openAIOptions.getModelName();
if (key != null && !key.isEmpty()) return key;
}
}
return null;
}

private String promptPrefix() {
if (optionsService != null) {
final OpenAIOptions openAIOptions =
optionsService.getOptions(OpenAIOptions.class);
if (openAIOptions != null) {
final String promptPrefix = openAIOptions.getPromptPrefix();
if (promptPrefix != null && !promptPrefix.isEmpty()) return promptPrefix;
}
}
return "";
}


public void extractSourceJar(final File file) {
try {
final FileFunctions functions = new FileFunctions(this);
Expand Down
Loading