Skip to content

Commit dd18ea4

Browse files
committed
Add Edit>Options>OpenAI... for setting the key
1 parent 0047719 commit dd18ea4

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* #%L
3+
* Script Editor and Interpreter for SciJava script languages.
4+
* %%
5+
* Copyright (C) 2009 - 2023 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ui.swing.script;
31+
32+
import org.scijava.options.OptionsPlugin;
33+
import org.scijava.plugin.Parameter;
34+
import org.scijava.plugin.Plugin;
35+
36+
/**
37+
* Runs the Edit::Options::OpenAI dialog.
38+
*
39+
* @author Curtis Rueden
40+
*/
41+
@Plugin(type = OptionsPlugin.class, menuPath = "Edit>Options>OpenAI...")
42+
public class OpenAIOptions extends OptionsPlugin {
43+
44+
@Parameter(label = "OpenAI key")
45+
private String openAIKey;
46+
47+
public String getOpenAIKey() {
48+
return openAIKey;
49+
}
50+
51+
public void setOpenAIKey(final String openAIKey) {
52+
this.openAIKey = openAIKey;
53+
}
54+
}

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
import org.scijava.log.LogService;
143143
import org.scijava.module.ModuleException;
144144
import org.scijava.module.ModuleService;
145+
import org.scijava.options.OptionsService;
145146
import org.scijava.platform.PlatformService;
146147
import org.scijava.plugin.Parameter;
147148
import org.scijava.plugin.PluginInfo;
@@ -276,6 +277,8 @@ public class TextEditor extends JFrame implements ActionListener,
276277
private AppService appService;
277278
@Parameter
278279
private BatchService batchService;
280+
@Parameter(required = false)
281+
private OptionsService optionsService;
279282

280283
private Map<ScriptLanguage, JRadioButtonMenuItem> languageMenuItems;
281284
private JRadioButtonMenuItem noneLanguageItem;
@@ -3276,7 +3279,7 @@ public void askChatGPTtoGenerateCode() {
32763279

32773280
private String askChatGPT(String text) {
32783281
// Modified from: https://github.com/TheoKanning/openai-java/blob/main/example/src/main/java/example/OpenAiApiFunctionsExample.java
3279-
String token = System.getenv("OPENAI_API_KEY");
3282+
String token = apiKey();
32803283

32813284
OpenAiService service = new OpenAiService(token);
32823285

@@ -3297,6 +3300,18 @@ private String askChatGPT(String text) {
32973300
return result;
32983301
}
32993302

3303+
private String apiKey() {
3304+
if (optionsService != null) {
3305+
final OpenAIOptions openAIOptions =
3306+
optionsService.getOptions(OpenAIOptions.class);
3307+
if (openAIOptions != null) {
3308+
final String key = openAIOptions.getOpenAIKey();
3309+
if (key != null && !key.isEmpty()) return key;
3310+
}
3311+
}
3312+
return System.getenv("OPENAI_API_KEY");
3313+
}
3314+
33003315
public void extractSourceJar(final File file) {
33013316
try {
33023317
final FileFunctions functions = new FileFunctions(this);

0 commit comments

Comments
 (0)