-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExampleChatGPT.java
150 lines (120 loc) · 6.44 KB
/
ExampleChatGPT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package examples;
import io.github.jetkai.openai.api.CreateChatCompletion;
import io.github.jetkai.openai.api.data.completion.chat.ChatCompletionData;
import io.github.jetkai.openai.api.data.completion.chat.message.ChatCompletionMessageData;
import io.github.jetkai.openai.net.OpenAIEndpoints;
import io.github.jetkai.openai.openai.OpenAI;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
/**
* ExampleChatGPT
*
* @author <a href="https://github.com/jetkai">Kai</a>
* @version 1.1.2
* {@code - 07/03/2023}
* @since 1.0.0
* {@code - 05/03/2023}
*
* <p>
* Note - This is just a test class, it is recommended to import this project as a library
* <p>
* You can get a free API key from <a href="https://platform.openai.com/account/api-keys">here</a>
*/
final class ExampleChatGPT {
//This is a List that will store all our conversation history - This includes our chat history and the AI's
private final List<ChatCompletionMessageData> messageHistory = new ArrayList<>();
public static void main(String[] args) {
ExampleChatGPT gpt = new ExampleChatGPT();
//The first message that we want to send
String message1 = "Hello ChatGPT!";
//The second message that we want to send
String message2 = "What was the first thing I said?";
//Response 1 from ChatGPT
String response1 = gpt.exampleBuilders(message1);
System.out.println("Sent: " + message1);
System.out.println("Response: " + response1);
//Response 2 from ChatGPT
String response2 = gpt.exampleBuilders(message2);
System.out.println("Sent: " + message2);
System.out.println("Response: " + response2);
}
private String exampleBuilders(String message) {
//Create the Message Data object with the message we wish to send
ChatCompletionMessageData messageData = ChatCompletionMessageData.builder()
.setRole("user")
.setContent(message)
.build();
//Store the message that we want to send, to the MessageHistory List
messageHistory.add(messageData);
//Build the data structure which contains the message history and model information
ChatCompletionData completionData = ChatCompletionData.builder()
.setModel("gpt-3.5-turbo")
.setMessages(messageHistory)
.build();
//Sends the request to OpenAI's endpoint & parses the response data
//Instead of openAI.sendRequest(); you can initialize the request
//for the class manually - openAI.createChatCompletion().initialize();
OpenAI openAI = OpenAI.builder()
.setApiKey(System.getenv("OPEN_AI_API_KEY"))
.createChatCompletion(completionData)
.build()
.sendRequest();
//Check to see if there is a valid response from OpenAI
//You can also call Optional<CreateChatCompletion> createChatCompletion = openAI.chatCompletion();
CreateChatCompletion createChatCompletion = openAI.getChatCompletion();
//Store chat response from AI, this allows the AI to see the full history of our chat
//Including both our messages and the AI's messages
messageHistory.addAll(createChatCompletion.asChatResponseDataList());
//Parse the response back as plain-text
return createChatCompletion.asText();
}
private String exampleInstanceOrEnum(String message) {
//Create the Message Data object with the message we wish to send
ChatCompletionMessageData messageData = ChatCompletionMessageData.create(message);
//Store the message that we want to send, to the MessageHistory List
messageHistory.add(messageData);
//Build the data structure which contains the message history and model information
ChatCompletionData completionData = ChatCompletionData.create(messageHistory);
//Sends the request to OpenAI's endpoint & parses the response data
OpenAI openAI = OpenAI.builder()
.setApiKey(System.getenv("OPEN_AI_API_KEY"))
.build();
//Create instance which will return as the API class that we specified in the enum or .class
//CreateChatCompletion createChatCompletion = openAI.createInstance(CreateChatCompletion.class, completionData);
CreateChatCompletion createChatCompletion = openAI.createInstance(
OpenAIEndpoints.CREATE_CHAT_COMPLETION, completionData
);
//Store chat response from AI, this allows the AI to see the full history of our chat
//Including both our messages and the AI's messages
messageHistory.addAll(createChatCompletion.asChatResponseDataList());
//Parse the response back as plain-text
return createChatCompletion.asText();
}
private String exampleHttpClientProxy(String message) {
//Create the Message Data object with the message we wish to send
ChatCompletionMessageData messageData = ChatCompletionMessageData.create(message);
//Store the message that we want to send, to the MessageHistory List
messageHistory.add(messageData);
//Build the data structure which contains the message history and model information
ChatCompletionData completionData = ChatCompletionData.create(messageHistory);
//Sends the request to OpenAI's endpoint & parses the response data
//Instead of openAI.sendRequest(); you can initialize the request for the class manually
//openAI.createChatCompletion().initialize();
OpenAI openAI = OpenAI.builder()
.setApiKey(System.getenv("OPEN_AI_API_KEY"))
.setProxy("1.0.205.87", 8080)
.setTimeout(Duration.ofSeconds(10))
.build();
//Create instance which will return as the API class that we specified in the enum or .class
//CreateChatCompletion createChatCompletion = openAI.createInstance(CreateChatCompletion.class, completionData);
CreateChatCompletion createChatCompletion = openAI.createInstance(
OpenAIEndpoints.CREATE_CHAT_COMPLETION, completionData
);
//Store chat response from AI, this allows the AI to see the full history of our chat
//Including both our messages and the AI's messages
messageHistory.addAll(createChatCompletion.asChatResponseDataList());
//Parse the response back as plain-text
return createChatCompletion.asText();
}
}