Skip to content

Commit d57a9a5

Browse files
authored
Merge pull request #68 from scijava/chatgpt2
make OpenAI API key optional and prompt prefix, model name configurable
2 parents c5e501b + 7dd7aba commit d57a9a5

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

src/main/java/org/scijava/ui/swing/script/OpenAIOptions.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,42 @@
4141
@Plugin(type = OptionsPlugin.class, menuPath = "Edit>Options>OpenAI...")
4242
public class OpenAIOptions extends OptionsPlugin {
4343

44-
@Parameter(label = "OpenAI key")
44+
@Parameter(label = "OpenAI key", required = false)
4545
private String openAIKey;
4646

47+
@Parameter(label = "OpenAI Model name")
48+
private String modelName = "gpt-3.5-turbo-0613";
49+
50+
@Parameter(label = "Prompt prefix (added before custom prompt)", style = "text area")
51+
private String promptPrefix = "Write code in {programming_language}.\n" +
52+
"Write concise and high quality code for ImageJ/Fiji.\n" +
53+
"Put minimal comments explaining what the code does.\n" +
54+
"The code should do the following:\n" +
55+
"{custom_prompt}";
56+
4757
public String getOpenAIKey() {
4858
return openAIKey;
4959
}
5060

5161
public void setOpenAIKey(final String openAIKey) {
5262
this.openAIKey = openAIKey;
5363
}
64+
65+
public String getPromptPrefix() {
66+
return promptPrefix;
67+
}
68+
69+
public void setPromptPrefix(final String promptPrefix) {
70+
this.promptPrefix = promptPrefix;
71+
}
72+
73+
public String getModelName() {
74+
return modelName;
75+
}
76+
77+
public void setModelName(final String modelName) {
78+
this.modelName = modelName;
79+
}
80+
81+
5482
}

src/main/java/org/scijava/ui/swing/script/TextEditor.java

+29-6
Original file line numberDiff line numberDiff line change
@@ -3250,11 +3250,9 @@ public void askChatGPTtoGenerateCode() {
32503250

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

32593257
String answer = askChatGPT(prompt);
32603258

@@ -3289,7 +3287,7 @@ private String askChatGPT(String text) {
32893287

32903288
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest
32913289
.builder()
3292-
.model("gpt-3.5-turbo-0613")
3290+
.model(modelName())
32933291
.messages(messages).build();
32943292

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

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+
33153338
public void extractSourceJar(final File file) {
33163339
try {
33173340
final FileFunctions functions = new FileFunctions(this);

0 commit comments

Comments
 (0)