diff --git a/pom.xml b/pom.xml index 9e0ba0f..9d7f2b5 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,24 @@ 3.2.0 + + + + com.theokanning.openai-gpt3-java + api + 0.12.0 + + + com.theokanning.openai-gpt3-java + client + 0.12.0 + + + com.theokanning.openai-gpt3-java + service + 0.12.0 + + @@ -112,4 +130,28 @@ + + + + + + + + + com.fasterxml.jackson.core + jackson-annotations + 2.10.1 + + + + + + org.reactivestreams + reactive-streams + 1.0.0 + + + + + \ No newline at end of file diff --git a/src/main/java/info/istlab/Zemi01/chatgpt/ChatGPTExample.java b/src/main/java/info/istlab/Zemi01/chatgpt/ChatGPTExample.java new file mode 100644 index 0000000..ac71c9a --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/chatgpt/ChatGPTExample.java @@ -0,0 +1,31 @@ +package info.istlab.Zemi01.chatgpt; + +import java.io.IOException; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +public class ChatGPTExample { + public static void main(String[] args) throws IOException { + String token = System.getenv("OPENAI_TOKEN"); + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, + "{\"model\": \"gpt-3.5-turbo\", \"messages\": [{\"role\": \"user\", \"content\": \"chatgptをJavaから使用する方法を教えてください。\"}], \"temperature\": 0.7 }" + ); + Request request = new Request.Builder() + // .url("https://api.openai.com/v1/models") + .url("https://api.openai.com/v1/chat/completions") + .method("POST", body) + .addHeader("Content-Type", "application/json") + .addHeader("OpenAI-Organization", "org-Fm8WLrwieG7Z3n0HU3PVvT95") + .addHeader("Authorization", "Bearer "+token) + .build(); + Response response = client.newCall(request).execute(); + System.out.println(response.body().string()); + } +} diff --git a/src/main/java/info/istlab/Zemi01/chatgpt/OpenAiApiExample.java b/src/main/java/info/istlab/Zemi01/chatgpt/OpenAiApiExample.java new file mode 100644 index 0000000..e09f137 --- /dev/null +++ b/src/main/java/info/istlab/Zemi01/chatgpt/OpenAiApiExample.java @@ -0,0 +1,57 @@ +package info.istlab.Zemi01.chatgpt; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import com.theokanning.openai.completion.chat.ChatCompletionRequest; +import com.theokanning.openai.completion.chat.ChatMessage; +import com.theokanning.openai.completion.chat.ChatMessageRole; +import com.theokanning.openai.service.OpenAiService; + +class OpenAiApiExample { + public static void main(String... args) { + String token = System.getenv("OPENAI_TOKEN"); //環境変数で設定 + // System.out.println(token); + // System.exit(0); + OpenAiService service = new OpenAiService(token); + + // System.out.println("\nCreating completion..."); + // CompletionRequest completionRequest = CompletionRequest.builder() + // .model("ada") + // .prompt("Somebody once told me the world is gonna roll me") + // .echo(true) + // .user("testing") + // .n(3) + // .build(); + // service.createCompletion(completionRequest).getChoices().forEach(System.out::println); + + // System.out.println("\nCreating Image..."); + // CreateImageRequest request = CreateImageRequest.builder() + // .prompt("A cow breakdancing with a turtle") + // .build(); + + // System.out.println("\nImage is located at:"); + // System.out.println(service.createImage(request).getData().get(0).getUrl()); + + System.out.println("Streaming chat completion..."); + final List messages = new ArrayList<>(); + final ChatMessage userMessage = new ChatMessage(ChatMessageRole.USER.value(), "創造的な活動をするには、普段どんなことに注意したらよいでしょうか?"); + messages.add(userMessage); + ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest + .builder() + .model("gpt-3.5-turbo") + .messages(messages) + .n(1) + .maxTokens(50) + .logitBias(new HashMap<>()) + .build(); + + service.streamChatCompletion(chatCompletionRequest) + .doOnError(Throwable::printStackTrace) + .blockingForEach(t -> System.out.print(t.getChoices().get(0).getMessage().getContent())); + // .blockingForEach(System.out::println); + + service.shutdownExecutor(); + } +} \ No newline at end of file diff --git a/src/main/java/info/istlab/Zemi01/numeric/NormalDistribution.java b/src/main/java/info/istlab/Zemi01/numeric/NormalDistribution.java index a39e1fc..5a95624 100644 --- a/src/main/java/info/istlab/Zemi01/numeric/NormalDistribution.java +++ b/src/main/java/info/istlab/Zemi01/numeric/NormalDistribution.java @@ -111,8 +111,8 @@ points = new ArrayList(); Random rand = new Random(); for (int i = 0; i < 7; i++) { - int randx = rand.nextInt(-40, 40); - int randy = rand.nextInt(-10,0); + int randx = rand.nextInt(80)-40; + int randy = rand.nextInt(10)*-1; points.add(new Point2D.Double(randx, randy)); } } diff --git a/src/main/java/info/istlab/Zemi01/numeric/WelchTTestGUI.java b/src/main/java/info/istlab/Zemi01/numeric/WelchTTestGUI.java index 1e92700..9748674 100644 --- a/src/main/java/info/istlab/Zemi01/numeric/WelchTTestGUI.java +++ b/src/main/java/info/istlab/Zemi01/numeric/WelchTTestGUI.java @@ -311,8 +311,8 @@ Random rand = new Random(); for (int i = 0; i < 7; i++) { - int randx = rand.nextInt(-40 + offsetx, 40 + offsetx); - int randy = rand.nextInt(-10 + offsety, offsety); + int randx = rand.nextInt(80) - 40 + offsetx; + int randy = rand.nextInt(10) * -1 + offsety; points.add(new Point2D.Double(randx, randy)); } updateNormDist(); @@ -386,12 +386,14 @@ (int) (-wtest.getHeight() / 2 + 56 - wtest.offset.getY())); } else { - g2d.drawString("μ = " + String.format("%6.4f", xavg), (int) (-wtest.getWidth() / 2 + 10 - wtest.offset.getX()), + g2d.drawString("μ = " + String.format("%6.4f", xavg), + (int) (-wtest.getWidth() / 2 + 10 - wtest.offset.getX()), (int) (-wtest.getHeight() / 2 + 20 - wtest.offset.getY())); g2d.drawString("σ = " + String.format("%6.4f", Math.sqrt(xvarp)), (int) (-wtest.getWidth() / 2 + 10 - wtest.offset.getX()), (int) (-wtest.getHeight() / 2 + 38 - wtest.offset.getY())); - g2d.drawString("σ^2 = " + String.format("%6.4f", xvarp), (int) (-wtest.getWidth() / 2 + 10 - wtest.offset.getX()), + g2d.drawString("σ^2 = " + String.format("%6.4f", xvarp), + (int) (-wtest.getWidth() / 2 + 10 - wtest.offset.getX()), (int) (-wtest.getHeight() / 2 + 56 - wtest.offset.getY())); }