Skip to content

Commit 23a611b

Browse files
committed
removed dependencies, refactored OpenAI code, addend anthropic Claude
1 parent b682698 commit 23a611b

5 files changed

Lines changed: 261 additions & 66 deletions

File tree

pom.xml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,15 +209,11 @@
209209
</dependency>
210210

211211
<dependency>
212-
<groupId>com.theokanning.openai-gpt3-java</groupId>
213-
<artifactId>client</artifactId>
214-
<version>0.14.0</version>
215-
</dependency>
216-
<dependency>
217-
<groupId>com.theokanning.openai-gpt3-java</groupId>
218-
<artifactId>service</artifactId>
219-
<version>0.14.0</version>
212+
<groupId>org.json</groupId>
213+
<artifactId>json</artifactId>
214+
<version>20230227</version>
220215
</dependency>
216+
221217
</dependencies>
222218

223219
<repositories>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.scijava.ui.swing.script;
2+
import org.json.JSONObject;
3+
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.io.OutputStream;
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
10+
import java.nio.charset.StandardCharsets;
11+
12+
public class ClaudeApiClient {
13+
14+
public static String prompt(String prompt, String model, String apikey, String apiurl) throws IOException {
15+
16+
if (apiurl == null) {
17+
apiurl = "https://api.anthropic.com/v1/messages";
18+
}
19+
if (apikey == null) {
20+
apikey = System.getenv("ANTHROPIC_API_KEY");
21+
}
22+
23+
System.out.println("API KEY:" + apikey);
24+
25+
URL url = new URL(apiurl);
26+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
27+
connection.setRequestMethod("POST");
28+
connection.setRequestProperty("x-api-key", apikey);
29+
connection.setRequestProperty("anthropic-version", "2023-06-01");
30+
connection.setRequestProperty("content-type", "application/json");
31+
connection.setDoOutput(true);
32+
33+
JSONObject requestBody = new JSONObject();
34+
requestBody.put("model", model);
35+
requestBody.put("max_tokens", 8192);
36+
requestBody.put("messages", new JSONObject[]{
37+
new JSONObject().put("role", "user").put("content", prompt)
38+
});
39+
40+
41+
// Send request
42+
try (OutputStream os = connection.getOutputStream()) {
43+
byte[] input = requestBody.toString().getBytes("utf-8");
44+
os.write(input, 0, input.length);
45+
}
46+
47+
int responseCode = connection.getResponseCode();
48+
49+
StringBuilder response = new StringBuilder();
50+
try (BufferedReader br = new BufferedReader(
51+
new InputStreamReader(
52+
responseCode >= 400 ? connection.getErrorStream() : connection.getInputStream(),
53+
StandardCharsets.UTF_8
54+
)
55+
)) {
56+
String responseLine;
57+
while ((responseLine = br.readLine()) != null) {
58+
response.append(responseLine.trim());
59+
}
60+
}
61+
62+
//System.out.println("Response Code: " + responseCode);
63+
//System.out.println("Full response: " + response.toString());
64+
65+
if (responseCode >= 400) {
66+
return "Error: " + response.toString();
67+
}
68+
69+
try {
70+
JSONObject jsonObject = new JSONObject(response.toString());
71+
String content = jsonObject.getJSONArray("content").getJSONObject(0).getString("text");
72+
return content;
73+
} catch (Exception e) {
74+
System.out.println("Error parsing JSON: " + e.getMessage());
75+
return null;
76+
}
77+
}
78+
79+
public static void main(String[] args) throws IOException {
80+
String input = "Hello, Claude! How are you today?";
81+
String response = prompt(input, "claude-3-5-sonnet-20240620", null, null);
82+
System.out.println("Claude's response: " + response);
83+
}
84+
}

src/main/java/org/scijava/ui/swing/script/OpenAIOptions.java renamed to src/main/java/org/scijava/ui/swing/script/LLMServicesOptions.java

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,43 @@
3838
*
3939
* @author Curtis Rueden
4040
*/
41-
@Plugin(type = OptionsPlugin.class, menuPath = "Edit>Options>OpenAI...")
42-
public class OpenAIOptions extends OptionsPlugin {
41+
@Plugin(type = OptionsPlugin.class, menuPath = "Edit>Options>LLM Service Options...")
42+
public class LLMServicesOptions extends OptionsPlugin {
4343

44-
@Parameter(label = "OpenAI key", required = false)
45-
private String openAIKey;
44+
@Parameter(label = "OpenAI API key", required = false)
45+
private String openAIAPIKey;
46+
47+
@Parameter(label = "Anthropic API key", required = false)
48+
private String anthropicAPIKey;
4649

4750
@Parameter(label = "OpenAI Model name")
48-
private String modelName = "gpt-3.5-turbo-0613";
51+
private String openAIModelName = "gpt-4o-2024-08-06";
52+
53+
@Parameter(label = "Anthropic Model name")
54+
private String anthropicModelName = "claude-3-5-sonnet-20240620";
4955

5056
@Parameter(label = "Prompt prefix (added before custom prompt)", style = "text area")
51-
private String promptPrefix = "Write code in {programming_language}.\n" +
57+
private String promptPrefix =
58+
"You are an extremely talented Bio-image Analyst and programmer.\n" +
59+
"You write code in {programming_language}.\n" +
5260
"Write concise and high quality code for ImageJ/Fiji.\n" +
5361
"Put minimal comments explaining what the code does.\n" +
54-
"The code should do the following:\n" +
62+
"Your task is the following:\n" +
5563
"{custom_prompt}";
5664

57-
public String getOpenAIKey() {
58-
return openAIKey;
65+
public String getOpenAIAPIKey() {
66+
return openAIAPIKey;
5967
}
6068

61-
public void setOpenAIKey(final String openAIKey) {
62-
this.openAIKey = openAIKey;
69+
public String getAnthropicAPIKey() {
70+
return anthropicAPIKey;
71+
}
72+
73+
public void setOpenAIAPIKey(final String openAIAPIKey) {
74+
this.openAIAPIKey = openAIAPIKey;
75+
}
76+
public void setAnthropicAPIKey(final String anthropicAPIKey) {
77+
this.anthropicAPIKey = anthropicAPIKey;
6378
}
6479

6580
public String getPromptPrefix() {
@@ -70,12 +85,20 @@ public void setPromptPrefix(final String promptPrefix) {
7085
this.promptPrefix = promptPrefix;
7186
}
7287

73-
public String getModelName() {
74-
return modelName;
88+
public String getOpenAIModelName() {
89+
return openAIModelName;
90+
}
91+
92+
public void setOpenAIModelName(final String openAIModelName) {
93+
this.openAIModelName = openAIModelName;
94+
}
95+
96+
public String getAnthropidModelName() {
97+
return anthropicModelName;
7598
}
7699

77-
public void setModelName(final String modelName) {
78-
this.modelName = modelName;
100+
public void setAnthropidModelName(final String anthropicModelName) {
101+
this.anthropicModelName = anthropicModelName;
79102
}
80103

81104

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.scijava.ui.swing.script;
2+
import org.json.JSONObject;
3+
4+
import java.io.BufferedReader;
5+
import java.io.InputStreamReader;
6+
import java.io.OutputStream;
7+
import java.net.HttpURLConnection;
8+
import java.net.URL;
9+
10+
public class OpenAIClient {
11+
12+
public static void main(String[] args) {
13+
String prompt = "Hello, GPT-4!";
14+
try {
15+
String response = prompt(prompt, "gpt-4o-2024-08-06", null, null);
16+
System.out.println("GPT-4 Response: " + response);
17+
} catch (Exception e) {
18+
e.printStackTrace();
19+
}
20+
}
21+
22+
public static String prompt(String prompt, String model, String apikey, String apiurl) throws Exception {
23+
24+
if (apiurl == null) {
25+
apiurl = "https://api.openai.com/v1/chat/completions";
26+
}
27+
if (apikey == null) {
28+
apikey = System.getenv("OPENAI_API_KEY");
29+
}
30+
31+
URL url = new URL(apiurl);
32+
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
33+
connection.setRequestMethod("POST");
34+
connection.setRequestProperty("Authorization", "Bearer " + apikey);
35+
connection.setRequestProperty("Content-Type", "application/json");
36+
connection.setDoOutput(true);
37+
38+
// Create JSON request body
39+
JSONObject requestBody = new JSONObject();
40+
requestBody.put("model", model);
41+
requestBody.put("messages", new JSONObject[]{
42+
new JSONObject().put("role", "user").put("content", prompt)
43+
});
44+
45+
System.out.println(connection);
46+
System.out.println(requestBody);
47+
48+
// Send request
49+
try (OutputStream os = connection.getOutputStream()) {
50+
byte[] input = requestBody.toString().getBytes("utf-8");
51+
os.write(input, 0, input.length);
52+
}
53+
54+
// Read response
55+
StringBuilder response = new StringBuilder();
56+
try (BufferedReader br = new BufferedReader(
57+
new InputStreamReader(connection.getInputStream(), "utf-8"))) {
58+
String responseLine;
59+
while ((responseLine = br.readLine()) != null) {
60+
response.append(responseLine.trim());
61+
}
62+
}
63+
64+
// Parse JSON response
65+
JSONObject jsonResponse = new JSONObject(response.toString());
66+
return jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content");
67+
}
68+
}

0 commit comments

Comments
 (0)