Skip to content

Commit fee1936

Browse files
committed
examples added within the "example" directory, redid the translate/transcription classes
1 parent 7e2225e commit fee1936

33 files changed

+1122
-665
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package examples;
2+
3+
import io.github.jetkai.openai.OpenAI;
4+
import io.github.jetkai.openai.api.CreateChatCompletion;
5+
import io.github.jetkai.openai.api.data.completion.chat.ChatCompletionData;
6+
import io.github.jetkai.openai.api.data.completion.chat.ChatCompletionMessageData;
7+
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
/**
12+
* ExampleChatGPT
13+
*
14+
* @author <a href="https://github.com/jetkai">Kai</a>
15+
* @version 1.0.0
16+
* {@code - 05/03/2023}
17+
* @since 1.0.0
18+
* {@code - 05/03/2023}
19+
*
20+
* <p>
21+
* Note - This is just a test class, it is recommended to import this project as a library
22+
* and then call OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
23+
* </p>
24+
*/
25+
public class ExampleChatGPT {
26+
27+
/*
28+
* You can get a free API key from https://platform.openai.com/account/api-keys
29+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY");
30+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
31+
*/
32+
private final OpenAI openAI = new OpenAI(System.getenv("OPEN_AI_API_KEY"));
33+
34+
//This is a List that will store all our conversation history
35+
//This includes our chat history and the AI's
36+
private final List<ChatCompletionMessageData> messageHistory = new ArrayList<>();
37+
38+
public static void main(String[] args) {
39+
ExampleChatGPT gpt = new ExampleChatGPT();
40+
41+
//The first message that we want to send
42+
String message1 = "Hello ChatGPT!";
43+
//The second message that we want to send
44+
String message2 = "What was the last thing I just said?";
45+
46+
//Response 1 from ChatGPT
47+
String response1 = gpt.communicate(message1);
48+
System.out.println("Sent: " + message1);
49+
System.out.println("Response: " + response1);
50+
51+
//Response 2 from ChatGPT
52+
String response2 = gpt.communicate(message2);
53+
System.out.println("Sent: " + message2);
54+
System.out.println("Response: " + response2);
55+
}
56+
57+
private String communicate(String message) {
58+
//Create the Message Data object with the message we wish to send
59+
ChatCompletionMessageData messageData = ChatCompletionMessageData.create(message);
60+
61+
//Store the message that we want to send, to the MessageHistory List
62+
messageHistory.add(messageData);
63+
64+
//Send the request to OpenAI, along with the MessageHistory data
65+
ChatCompletionData completionData = ChatCompletionData.create(messageHistory);
66+
67+
//Send the request to OpenAI, along with the MessageHistory data
68+
CreateChatCompletion response = openAI.createChatCompletion(completionData);
69+
70+
//Store chat response from AI, this allows the AI to see the full history of our chat
71+
//Including both our messages and the AI's messages
72+
messageHistory.addAll(response.asChatResponseDataList());
73+
74+
//Replace \n & ascii characters (Šťŕĭńġ -> String)
75+
return response.asNormalizedText();
76+
}
77+
78+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package examples;
2+
3+
import io.github.jetkai.openai.OpenAI;
4+
import io.github.jetkai.openai.api.GetModel;
5+
6+
/**
7+
* ExampleGetModel
8+
*
9+
* @author <a href="https://github.com/jetkai">Kai</a>
10+
* @version 1.0.0
11+
* {@code - 05/03/2023}
12+
* @since 1.0.0
13+
* {@code - 05/03/2023}
14+
*
15+
* <p>
16+
* Note - This is just a test class, it is recommended to import this project as a library
17+
* and then call OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
18+
* </p>
19+
*/
20+
public class ExampleGetModel {
21+
22+
/*
23+
* You can get a free API key from https://platform.openai.com/account/api-keys
24+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY");
25+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
26+
*/
27+
private final OpenAI openAI = new OpenAI(System.getenv("OPEN_AI_API_KEY"));
28+
29+
public static void main(String[] args) {
30+
//Initialize ExampleGetModel class
31+
ExampleGetModel getModel = new ExampleGetModel();
32+
33+
String modelName = "davinci";
34+
35+
//Send request to API - response as JSON string
36+
String modelJson = getModel.communicate(modelName);
37+
38+
//Print out the model information in JSON
39+
System.out.println(modelJson);
40+
}
41+
42+
private String communicate(String modelName) {
43+
//Call the GetModels API from OpenAI & create instance
44+
GetModel getModels = openAI.getModel(modelName);
45+
46+
//Return model as a JSON string
47+
return getModels.asJson();
48+
}
49+
50+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package examples;
2+
3+
import io.github.jetkai.openai.OpenAI;
4+
import io.github.jetkai.openai.api.GetModels;
5+
import io.github.jetkai.openai.api.data.model.ModelData;
6+
7+
import java.util.List;
8+
9+
/**
10+
* ExampleGetModels
11+
*
12+
* @author <a href="https://github.com/jetkai">Kai</a>
13+
* @version 1.0.0
14+
* {@code - 05/03/2023}
15+
* @since 1.0.0
16+
* {@code - 05/03/2023}
17+
*
18+
* <p>
19+
* Note - This is just a test class, it is recommended to import this project as a library
20+
* and then call OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
21+
* </p>
22+
*/
23+
public class ExampleGetModels {
24+
25+
/*
26+
* You can get a free API key from https://platform.openai.com/account/api-keys
27+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY");
28+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
29+
*/
30+
private final OpenAI openAI = new OpenAI(System.getenv("OPEN_AI_API_KEY"));
31+
32+
public static void main(String[] args) {
33+
//Initialize ExampleGetModels class
34+
ExampleGetModels getModels = new ExampleGetModels();
35+
36+
//Send request to API and deserialize response as List<ModalData>
37+
List<ModelData> models = getModels.communicate();
38+
39+
//Print out the names of all the available models, to the console
40+
models.stream().map(ModelData::getId).forEach(System.out::println);
41+
}
42+
43+
private List<ModelData> communicate() {
44+
//Call the GetModels API from OpenAI & create instance
45+
GetModels getModels = openAI.getModels();
46+
47+
//Return models as a data structure list, so that we can get the name from each model
48+
return getModels.asDataList();
49+
}
50+
51+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package examples;
2+
3+
import io.github.jetkai.openai.OpenAI;
4+
import io.github.jetkai.openai.api.CreateImage;
5+
import io.github.jetkai.openai.api.data.image.ImageData;
6+
7+
import java.awt.*;
8+
import java.io.IOException;
9+
import java.net.URI;
10+
11+
/**
12+
* ExampleImageCreation
13+
*
14+
* @author <a href="https://github.com/jetkai">Kai</a>
15+
* @version 1.0.0
16+
* {@code - 05/03/2023}
17+
* @since 1.0.0
18+
* {@code - 05/03/2023}
19+
*
20+
* <p>
21+
* Note - This is just a test class, it is recommended to import this project as a library
22+
* and then call OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
23+
* </p>
24+
*/
25+
public class ExampleImageCreation {
26+
27+
/*
28+
* You can get a free API key from https://platform.openai.com/account/api-keys
29+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY");
30+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
31+
*/
32+
private final OpenAI openAI = new OpenAI(System.getenv("OPEN_AI_API_KEY"));
33+
34+
public static void main(String[] args) throws IOException {
35+
//Initialize ExampleImageCreation class
36+
ExampleImageCreation imageCreation = new ExampleImageCreation();
37+
38+
//A text description of the desired image(s). The maximum length is 1000 characters
39+
String description = "A picture of a red panda with glasses, drawn as a cartoon.";
40+
41+
//The number of images to generate. Must be between 1 and 10.
42+
int amount = 1;
43+
44+
//The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024
45+
String size = "1024x1024";
46+
47+
URI[] imageLinks = imageCreation.communicate(description, amount, size);
48+
49+
for(URI imageLink : imageLinks) {
50+
System.out.println("Opening URI ["+imageLink+"] in the web browser.");
51+
Desktop.getDesktop().browse(imageLink);
52+
}
53+
}
54+
55+
private URI[] communicate(String imageDescription, int numberOfImages, String size) {
56+
//Alternatively can use ImageData.create(imageDescription, numberOfImages, size);
57+
ImageData imageData = ImageData.create()
58+
//A text description of the desired image(s). The maximum length is 1000 characters
59+
.setPrompt(imageDescription)
60+
//The number of images to generate. Must be between 1 and 10.
61+
.setN(numberOfImages)
62+
//The size of the generated images. Must be one of 256x256, 512x512, or 1024x1024
63+
.setSize(size);
64+
65+
//Call the CreateImage API from OpenAI & create instance
66+
CreateImage createImage = openAI.createImage(imageData);
67+
68+
//Gather the URI(s) from the API response
69+
return createImage.asUriArray();
70+
}
71+
72+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package examples;
2+
3+
import io.github.jetkai.openai.OpenAI;
4+
import io.github.jetkai.openai.api.CreateEdit;
5+
import io.github.jetkai.openai.api.data.edit.EditData;
6+
7+
/**
8+
* ExampleSpellingCorrection
9+
*
10+
* @author <a href="https://github.com/jetkai">Kai</a>
11+
* @version 1.0.0
12+
* {@code - 05/03/2023}
13+
* @since 1.0.0
14+
* {@code - 05/03/2023}
15+
*
16+
* <p>
17+
* Note - This is just a test class, it is recommended to import this project as a library
18+
* and then call OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
19+
* </p>
20+
*/
21+
public class ExampleSpellingCorrection {
22+
23+
/*
24+
* You can get a free API key from https://platform.openai.com/account/api-keys
25+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY");
26+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
27+
*/
28+
private final OpenAI openAI = new OpenAI(System.getenv("OPEN_AI_API_KEY"));
29+
30+
public static void main(String[] args) {
31+
//Initialize ExampleSpellingCorrection class
32+
ExampleSpellingCorrection getModel = new ExampleSpellingCorrection();
33+
34+
String spellingMistake = "Wha dai of the wek is it?";
35+
String instruction = "Fix the spelling mistakes";
36+
37+
//Send request to API - response as string
38+
String spellingCorrection = getModel.communicate(spellingMistake, instruction);
39+
40+
//Print out the mistake & correction
41+
System.out.println("Mistake: " + spellingMistake);
42+
System.out.println("Correction: " + spellingCorrection);
43+
}
44+
45+
private String communicate(String spellingMistake, String instruction) {
46+
//Call the GetModels API from OpenAI & create instance
47+
//You can also specify the model and use the following:
48+
//EditData.create(model, spellingMistake, instruction);
49+
EditData editData = EditData.create(spellingMistake, instruction);
50+
51+
//Call the CreateEdit API from OpenAI & create instance
52+
CreateEdit createEdit = openAI.createEdit(editData);
53+
54+
//Return model as a JSON string
55+
return createEdit.asText();
56+
}
57+
58+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package examples;
2+
3+
import io.github.jetkai.openai.OpenAI;
4+
import io.github.jetkai.openai.api.CreateTranscription;
5+
import io.github.jetkai.openai.api.data.audio.AudioData;
6+
7+
import java.net.URISyntaxException;
8+
import java.net.URL;
9+
import java.nio.file.Path;
10+
11+
/**
12+
* ExampleTranscriptionFromAudioFile
13+
*
14+
* @author <a href="https://github.com/jetkai">Kai</a>
15+
* @version 1.0.0
16+
* {@code - 05/03/2023}
17+
* @since 1.0.0
18+
* {@code - 05/03/2023}
19+
*
20+
* <p>
21+
* Note - This is just a test class, it is recommended to import this project as a library
22+
* and then call OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
23+
* </p>
24+
*/
25+
public class ExampleTranscriptionFromAudioFile {
26+
27+
/*
28+
* You can get a free API key from https://platform.openai.com/account/api-keys
29+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY");
30+
* private final OpenAI openAI = new OpenAI("YOUR_API_KEY", "YOUR_ORGANIZATION");
31+
*/
32+
private final OpenAI openAI = new OpenAI(System.getenv("OPEN_AI_API_KEY"));
33+
34+
public static void main(String[] args) throws URISyntaxException {
35+
//Initialize ExampleTranslationFromAudioFile class
36+
ExampleTranscriptionFromAudioFile transcriptAudioFile = new ExampleTranscriptionFromAudioFile();
37+
38+
//Example audio file that we are going to upload to OpenAI to be translated
39+
URL audioUrl = ExampleTranscriptionFromAudioFile.class.getResource("what-can-i-do.mp3");
40+
Path filePath = null;
41+
if (audioUrl != null) {
42+
filePath = Path.of(audioUrl.toURI());
43+
}
44+
45+
//Response from OpenAI with the translated string
46+
String response = transcriptAudioFile.communicate(filePath);
47+
48+
//Print the translation to the console
49+
System.out.println(response);
50+
}
51+
52+
private String communicate(Path filePath) {
53+
//AudioData, ready to send to the OpenAI API
54+
AudioData transcriptionData = AudioData.create(filePath);
55+
56+
//Call the CreateTranscription API from OpenAI & create instance
57+
CreateTranscription createTranslation = openAI.createTranscription(transcriptionData);
58+
59+
//Return as text, do not replace \n or ascii characters
60+
return createTranslation.asText();
61+
}
62+
63+
}

0 commit comments

Comments
 (0)