-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removed dependencies, refactored OpenAI code, addend anthropic Claude
- Loading branch information
1 parent
b682698
commit 23a611b
Showing
5 changed files
with
261 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
src/main/java/org/scijava/ui/swing/script/ClaudeApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.scijava.ui.swing.script; | ||
import org.json.JSONObject; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class ClaudeApiClient { | ||
|
||
public static String prompt(String prompt, String model, String apikey, String apiurl) throws IOException { | ||
|
||
if (apiurl == null) { | ||
apiurl = "https://api.anthropic.com/v1/messages"; | ||
} | ||
if (apikey == null) { | ||
apikey = System.getenv("ANTHROPIC_API_KEY"); | ||
} | ||
|
||
System.out.println("API KEY:" + apikey); | ||
|
||
URL url = new URL(apiurl); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("POST"); | ||
connection.setRequestProperty("x-api-key", apikey); | ||
connection.setRequestProperty("anthropic-version", "2023-06-01"); | ||
connection.setRequestProperty("content-type", "application/json"); | ||
connection.setDoOutput(true); | ||
|
||
JSONObject requestBody = new JSONObject(); | ||
requestBody.put("model", model); | ||
requestBody.put("max_tokens", 8192); | ||
requestBody.put("messages", new JSONObject[]{ | ||
new JSONObject().put("role", "user").put("content", prompt) | ||
}); | ||
|
||
|
||
// Send request | ||
try (OutputStream os = connection.getOutputStream()) { | ||
byte[] input = requestBody.toString().getBytes("utf-8"); | ||
os.write(input, 0, input.length); | ||
} | ||
|
||
int responseCode = connection.getResponseCode(); | ||
|
||
StringBuilder response = new StringBuilder(); | ||
try (BufferedReader br = new BufferedReader( | ||
new InputStreamReader( | ||
responseCode >= 400 ? connection.getErrorStream() : connection.getInputStream(), | ||
StandardCharsets.UTF_8 | ||
) | ||
)) { | ||
String responseLine; | ||
while ((responseLine = br.readLine()) != null) { | ||
response.append(responseLine.trim()); | ||
} | ||
} | ||
|
||
//System.out.println("Response Code: " + responseCode); | ||
//System.out.println("Full response: " + response.toString()); | ||
|
||
if (responseCode >= 400) { | ||
return "Error: " + response.toString(); | ||
} | ||
|
||
try { | ||
JSONObject jsonObject = new JSONObject(response.toString()); | ||
String content = jsonObject.getJSONArray("content").getJSONObject(0).getString("text"); | ||
return content; | ||
} catch (Exception e) { | ||
System.out.println("Error parsing JSON: " + e.getMessage()); | ||
return null; | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
String input = "Hello, Claude! How are you today?"; | ||
String response = prompt(input, "claude-3-5-sonnet-20240620", null, null); | ||
System.out.println("Claude's response: " + response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/org/scijava/ui/swing/script/OpenAIClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.scijava.ui.swing.script; | ||
import org.json.JSONObject; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public class OpenAIClient { | ||
|
||
public static void main(String[] args) { | ||
String prompt = "Hello, GPT-4!"; | ||
try { | ||
String response = prompt(prompt, "gpt-4o-2024-08-06", null, null); | ||
System.out.println("GPT-4 Response: " + response); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public static String prompt(String prompt, String model, String apikey, String apiurl) throws Exception { | ||
|
||
if (apiurl == null) { | ||
apiurl = "https://api.openai.com/v1/chat/completions"; | ||
} | ||
if (apikey == null) { | ||
apikey = System.getenv("OPENAI_API_KEY"); | ||
} | ||
|
||
URL url = new URL(apiurl); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("POST"); | ||
connection.setRequestProperty("Authorization", "Bearer " + apikey); | ||
connection.setRequestProperty("Content-Type", "application/json"); | ||
connection.setDoOutput(true); | ||
|
||
// Create JSON request body | ||
JSONObject requestBody = new JSONObject(); | ||
requestBody.put("model", model); | ||
requestBody.put("messages", new JSONObject[]{ | ||
new JSONObject().put("role", "user").put("content", prompt) | ||
}); | ||
|
||
System.out.println(connection); | ||
System.out.println(requestBody); | ||
|
||
// Send request | ||
try (OutputStream os = connection.getOutputStream()) { | ||
byte[] input = requestBody.toString().getBytes("utf-8"); | ||
os.write(input, 0, input.length); | ||
} | ||
|
||
// Read response | ||
StringBuilder response = new StringBuilder(); | ||
try (BufferedReader br = new BufferedReader( | ||
new InputStreamReader(connection.getInputStream(), "utf-8"))) { | ||
String responseLine; | ||
while ((responseLine = br.readLine()) != null) { | ||
response.append(responseLine.trim()); | ||
} | ||
} | ||
|
||
// Parse JSON response | ||
JSONObject jsonResponse = new JSONObject(response.toString()); | ||
return jsonResponse.getJSONArray("choices").getJSONObject(0).getJSONObject("message").getString("content"); | ||
} | ||
} |
Oops, something went wrong.