|
1 | 1 | # OpenAI For Java
|
2 |
| -[](https://codecov.io/gh/jetkai/openai-for-java) |
| 2 | + |
| 3 | + |
3 | 4 | 
|
4 |
| - |
5 |
| -###### OpenAI API Reference -> https://platform.openai.com/docs/api-reference/ |
6 |
| -###### OpenAI API Key -> https://platform.openai.com/account/api-keys |
| 5 | + |
| 6 | + |
| 7 | +[](https://codecov.io/gh/jetkai/openai-for-java) |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +> Note, this project is under active development. If there are any suggestions you may have, feel free to let me know. 😊 |
| 12 | +> |
| 13 | +> You can use this library as part of your project & integrate it into your app. |
| 14 | +> |
| 15 | +> OpenAI is **free-to-use**, but you will require an API key. You can get this for free by clicking [**here**](https://platform.openai.com/account/api-keys). |
| 16 | +
|
| 17 | +```java |
| 18 | + OpenAI openAI = OpenAI.builder() |
| 19 | + .setApiKey("YOUR_API_KEY_HERE") //Required |
| 20 | + .setOrganization("YOUR_ORGANIZATION_HERE") //Optional |
| 21 | + .createChatCompletion(chatMessageHistoryHere) //Optional (ChatGPT) |
| 22 | + .setTimeout(Duration.ofSeconds(10)) //Optional |
| 23 | + .setProxy(proxy) //Optional |
| 24 | + .build() //Creates our OpenAI class from our OpenAI.Builder |
| 25 | + .sendRequest(); //Optional - Sends request to OpenAI |
| 26 | +``` |
| 27 | + |
| 28 | +--- |
| 29 | + |
| 30 | +## Minimum Requirements |
| 31 | + |
| 32 | +- Java 11 [JDK](https://developers.redhat.com/content-gateway/file/openjdk/11.0.18/java-11-openjdk-11.0.18.10-1.windows.redhat.x86_64.msi) |
| 33 | +- An OpenAI [API Key](https://platform.openai.com/account/api-keys) |
7 | 34 |
|
8 | 35 | ## Release `1.1.0`
|
9 | 36 |
|
| 37 | +> The dependencies I am using for this project are [`jackson-databind`](https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind) for JSON parsing, [`junit-jupiter`](https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api) for testing and [`jacoco`](https://mvnrepository.com/artifact/org.jacoco/jacoco-maven-plugin) for reporting. |
| 38 | +
|
10 | 39 | ```xml
|
11 | 40 | <dependency>
|
12 | 41 | <groupId>io.github.jetkai</groupId>
|
|
15 | 44 | </dependency>
|
16 | 45 | ```
|
17 | 46 |
|
18 |
| -#### Binary: |
19 |
| -- [openai.jar](https://github.com/jetkai/openai-for-java/releases/download/1.1.0/openai.jar) `(with dependencies)` |
20 |
| -- [openai-excldeps.jar](https://github.com/jetkai/openai-for-java/releases/download/1.1.0/openai-excldeps.jar) `(excluding dependencies)` |
| 47 | +| Binary `(Recommended)` | Binary `(Excluding Dependencies)` | JavaDoc | Sources | |
| 48 | +|---------------------------------------------------------------------------------------------- |------------------------------------------------------------------------------------------------------- |------------------------------------------------------------------------------------------------------ |------------------------------------------------------------------------------------------------------ | |
| 49 | +| > [Download](https://github.com/jetkai/openai-for-java/releases/download/1.1.0/openai.jar) < | > [Download](https://github.com/jetkai/openai-for-java/releases/download/1.1.0/openai-excldeps.jar) < | > [Download](https://github.com/jetkai/openai-for-java/releases/download/1.1.0/openai-javadoc.jar) < | > [Download](https://github.com/jetkai/openai-for-java/releases/download/1.1.0/openai-sources.jar) < | |
21 | 50 |
|
22 |
| -#### Other: |
23 |
| -- [openai-sources.jar](https://github.com/jetkai/openai-for-java/releases/download/1.1.0/openai-sources.jar) |
24 |
| -- [openai-javadoc.jar](https://github.com/jetkai/openai-for-java/releases/download/1.1.0/openai-javadoc.jar) |
| 51 | +## Available API(s) `March 2023` |
25 | 52 |
|
26 |
| -## Minimum Requirements |
27 |
| -- JDK 11 |
28 |
| -- An OpenAI [API Key](https://platform.openai.com/account/api-keys) |
| 53 | +✅ ChatGPT 3.5 (Latest) - `using CreateChatCompletion` |
| 54 | + |
| 55 | +✅ Spell Checking - `using CreateCompletion` |
| 56 | + |
| 57 | +✅ Transcriptions (From Audio File) - `using CreateTranscription` |
| 58 | + |
| 59 | +✅ Translations (From Text or Audio File) - `using CreateTranslation` / `CreateTranscriptionTranslation` |
| 60 | + |
| 61 | +✅ AI Image Creation / Editing - `using CreateImage` / `CreateImageEdit` / `CreateImageVariation` |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +###### [OpenAI API Reference](https://platform.openai.com/docs/api-reference/) |
29 | 66 |
|
30 |
| -## Available API(s) `March 2023` |
31 | 67 | - [ListModel](https://platform.openai.com/docs/api-reference/models/retrieve) -> `https://api.openai.com/v1/models`
|
32 | 68 | - [ListModels](https://platform.openai.com/docs/api-reference/models/list) -> `https://api.openai.com/v1/models/{model}`
|
33 | 69 | - [CreateCompletion](https://platform.openai.com/docs/api-reference/completions/create) -> `https://api.openai.com/v1/completions`
|
|
38 | 74 | - [CreateImageVariation](https://platform.openai.com/docs/api-reference/images/create-variation) -> `https://api.openai.com/v1/images/variations`
|
39 | 75 | - [CreateEmbeddings](https://platform.openai.com/docs/api-reference/embeddings/create) -> `https://api.openai.com/v1/embeddings`
|
40 | 76 | - [CreateTranscription](https://platform.openai.com/docs/api-reference/audio/create) -> `https://api.openai.com/v1/audio/transcriptions`
|
41 |
| -- [CreateTranslation](https://platform.openai.com/docs/api-reference/audio/create) -> `https://api.openai.com/v1/audio/translations` |
| 77 | +- [CreateTranscriptionAndTranslation](https://platform.openai.com/docs/api-reference/audio/create) -> `https://api.openai.com/v1/audio/translations` |
| 78 | +- [CreateTranslation](https://platform.openai.com/docs/api-reference/audio/create) -> `https://api.openai.com/v1/completions` |
| 79 | + |
| 80 | +--- |
42 | 81 |
|
43 | 82 | # Source Code (Examples)
|
44 | 83 |
|
45 | 84 | ## [ChatGPT-3.5 Turbo](https://github.com/jetkai/openai-for-java/blob/main/src/main/java/examples/ExampleChatGPT.java)
|
| 85 | + |
46 | 86 | Scenario: Say "Hello" to the AI and ask the AI to respond back with the last thing I said.
|
| 87 | + |
47 | 88 | ```java
|
48 |
| - private String exampleBuilders(String message) { |
| 89 | + public static void main(String[] args) { |
| 90 | + ExampleChatGPT gpt = new ExampleChatGPT(); |
| 91 | + |
| 92 | + //The first message that we want to send |
| 93 | + String message1 = "Hello ChatGPT!"; |
| 94 | + //The second message that we want to send |
| 95 | + String message2 = "What was the first thing I said?"; |
| 96 | + |
| 97 | + //Response 1 from ChatGPT |
| 98 | + String response1 = gpt.exampleBuilders(message1); |
| 99 | + System.out.println("Sent: " + message1); |
| 100 | + System.out.println("Response: " + response1); |
| 101 | + |
| 102 | + //Response 2 from ChatGPT |
| 103 | + String response2 = gpt.exampleBuilders(message2); |
| 104 | + System.out.println("Sent: " + message2); |
| 105 | + System.out.println("Response: " + response2); |
| 106 | + } |
| 107 | + |
| 108 | + private String exampleBuilders(String message) { |
49 | 109 | //Create the Message Data object with the message we wish to send
|
50 | 110 | ChatCompletionMessageData messageData = ChatCompletionMessageData.builder()
|
51 | 111 | .setRole("user")
|
|
81 | 141 | //Parse the response back as plain-text & replace \n & ascii characters (Šťŕĭńġ -> String)
|
82 | 142 | return createChatCompletion.asNormalizedText();
|
83 | 143 | }
|
84 |
| - |
85 |
| - private String exampleHttpClientProxy(String message) { |
86 |
| - ProxySelector proxySelector = ProxySelector.of(new InetSocketAddress("1.0.205.87", 8080)); |
87 |
| - |
88 |
| - HttpClient httpClient = HttpClient.newBuilder() |
89 |
| - .version(HttpClient.Version.HTTP_2) |
90 |
| - .followRedirects(HttpClient.Redirect.ALWAYS) |
91 |
| - .proxy(proxySelector) |
92 |
| - .connectTimeout(Duration.ofSeconds(10)) //10 seconds timeout |
93 |
| - .build(); |
94 |
| - |
95 |
| - //Create the Message Data object with the message we wish to send |
96 |
| - ChatCompletionMessageData messageData = ChatCompletionMessageData.create(message); |
97 |
| - |
98 |
| - //Store the message that we want to send, to the MessageHistory List |
99 |
| - messageHistory.add(messageData); |
100 |
| - |
101 |
| - //Build the data structure which contains the message history and model information |
102 |
| - ChatCompletionData completionData = ChatCompletionData.create(messageHistory); |
103 |
| - |
104 |
| - //Sends the request to OpenAI's endpoint & parses the response data |
105 |
| - //Instead of openAI.sendRequest(); you can initialize the request for the class manually |
106 |
| - //openAI.createChatCompletion().initialize(); |
107 |
| - OpenAI openAI = OpenAI.builder() |
108 |
| - .setApiKey(System.getenv("OPEN_AI_API_KEY")) |
109 |
| - .setHttpClient(httpClient) |
110 |
| - .build(); |
111 |
| - |
112 |
| - //Create instance which will return as the API class that we specified in the enum or .class |
113 |
| - //CreateChatCompletion createChatCompletion = openAI.createInstance(CreateChatCompletion.class, completionData); |
114 |
| - CreateChatCompletion createChatCompletion = openAI.createInstance( |
115 |
| - OpenAIEndpoints.CREATE_CHAT_COMPLETION, completionData |
116 |
| - ); |
117 |
| - |
118 |
| - //Store chat response from AI, this allows the AI to see the full history of our chat |
119 |
| - //Including both our messages and the AI's messages |
120 |
| - messageHistory.addAll(createChatCompletion.asChatResponseDataList()); |
121 |
| - |
122 |
| - //Parse the response back as plain-text & replace \n & ascii characters (Šťŕĭńġ -> String) |
123 |
| - return createChatCompletion.asNormalizedText(); |
124 |
| - } |
125 | 144 | ```
|
126 | 145 |
|
127 | 146 | ## [AI Image Creation](https://github.com/jetkai/openai-for-java/blob/main/src/main/java/examples/ExampleImageCreation.java)
|
| 147 | + |
128 | 148 | Scenario: Have an AI create a red panda with glasses, and have this drawn as a cartoon.
|
| 149 | + |
129 | 150 | ```java
|
130 | 151 | public static void main(String[] args) throws IOException {
|
131 | 152 | //Initialize ExampleImageCreation class
|
|
177 | 198 | ```
|
178 | 199 |
|
179 | 200 | ## [Spelling Correction](https://github.com/jetkai/openai-for-java/blob/main/src/main/java/examples/ExampleSpellingCorrection.java)
|
| 201 | + |
180 | 202 | Scenario: Correct the spelling within the sentence "Wha dai of the wek is it?".
|
| 203 | + |
181 | 204 | ```java
|
182 | 205 | public static void main(String[] args) {
|
183 | 206 | //Initialize ExampleSpellingCorrection class
|
|
216 | 239 | ```
|
217 | 240 |
|
218 | 241 | ## [Translate Language](https://github.com/jetkai/openai-for-java/blob/main/src/main/java/examples/ExampleTranslation.java)
|
| 242 | + |
219 | 243 | Scenario: Translate "Hello, how are you today?" from English to French.
|
| 244 | + |
220 | 245 | ```java
|
221 | 246 | public static void main(String[] args) {
|
222 | 247 | //Initialize ExampleTranslation class
|
|
255 | 280 | ```
|
256 | 281 |
|
257 | 282 | ## [Audio Transcript](https://github.com/jetkai/openai-for-java/blob/main/src/main/java/examples/ExampleTranscriptionFromAudioFile.java)
|
| 283 | + |
258 | 284 | Scenario: Create a transcript from an audio file.
|
259 | 285 |
|
260 | 286 | ```java
|
|
296 | 322 | ```
|
297 | 323 |
|
298 | 324 | ## [Audio Transcript & Translate](https://github.com/jetkai/openai-for-java/blob/main/src/main/java/examples/ExampleTranslationFromAudioFile.java)
|
| 325 | + |
299 | 326 | Scenario: Create a transcript from an audio file, then translate the transcript from English to French.
|
300 |
| - |
| 327 | + |
301 | 328 | ```java
|
302 | 329 | public static void main(String[] args) throws URISyntaxException {
|
303 | 330 | //Initialize ExampleTranslationFromAudioFile class
|
|
348 | 375 | ```
|
349 | 376 |
|
350 | 377 | ## [List Model Information](https://github.com/jetkai/openai-for-java/blob/main/src/main/java/examples/ExampleListModel.java)
|
| 378 | + |
351 | 379 | Scenario: Get information about a specific model.
|
352 | 380 |
|
353 | 381 | ```java
|
|
382 | 410 | ```
|
383 | 411 |
|
384 | 412 | ## [List All Models](https://github.com/jetkai/openai-for-java/blob/main/src/main/java/examples/ExampleListModels.java)
|
| 413 | + |
385 | 414 | Scenario: List all the models available from OpenAI.
|
| 415 | + |
386 | 416 | ```java
|
387 | 417 | public static void main(String[] args) {
|
388 | 418 | //Initialize ExampleListModels class
|
|
561 | 591 | assertFalse(json.isEmpty());
|
562 | 592 | }
|
563 | 593 | ```
|
564 |
| - |
| 594 | + |
565 | 595 | ### CreateImageVariation
|
566 | 596 |
|
567 | 597 | ```java
|
|
683 | 713 | assertNotNull(json);
|
684 | 714 | assertFalse(json.isEmpty());
|
685 | 715 | }
|
686 |
| - ``` |
| 716 | +``` |
0 commit comments