-
Notifications
You must be signed in to change notification settings - Fork 11
AI assisted Script Editiing: removed dependencies, refactored OpenAI code, added anthropic Claude #70
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
Open
Open
AI assisted Script Editiing: removed dependencies, refactored OpenAI code, added anthropic Claude #70
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
src/main/java/org/scijava/ui/swing/script/ClaudeApiClient.java
This file contains hidden or 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 hidden or 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
src/main/java/org/scijava/ui/swing/script/OpenAIClient.java
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.