Skip to content

Commit 015d245

Browse files
committed
Merge branch 'master' into exception-wrappers
2 parents 6ba9483 + 03f40c9 commit 015d245

File tree

6 files changed

+115
-68
lines changed

6 files changed

+115
-68
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import com.github.breadmoirai.githubreleaseplugin.GithubReleaseTask
22

33
group = "com.cjcrafter"
4-
version = "1.2.1"
4+
version = "1.2.2"
55

66
plugins {
77
`java-library`

src/main/kotlin/com/cjcrafter/openai/OpenAI.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ class OpenAI @JvmOverloads constructor(
6565
* @throws IllegalArgumentException If the input arguments are invalid.
6666
*/
6767
@Throws(IOException::class)
68-
fun generateResponse(request: ChatRequest): ChatResponse {
68+
fun createChatCompletion(request: ChatRequest): ChatResponse {
6969
request.stream = false // use streamResponse for stream=true
7070
val httpRequest = buildRequest(request)
7171

7272
// Save the JsonObject to check for errors
73-
var rootObject: JsonObject? = null
73+
var rootObject: JsonObject?
7474
try {
7575
client.newCall(httpRequest).execute().use { response ->
7676

@@ -86,7 +86,7 @@ class OpenAI @JvmOverloads constructor(
8686
}
8787

8888
/**
89-
* This is a helper method that calls [streamResponse], which lets you use
89+
* This is a helper method that calls [streamChatCompletion], which lets you use
9090
* the generated tokens in real time (As ChatGPT generates them).
9191
*
9292
* This method does not block the thread. Method calls to [onResponse] are
@@ -112,8 +112,8 @@ class OpenAI @JvmOverloads constructor(
112112
* @param onResponse The method to call for each chunk.
113113
* @since 1.2.0
114114
*/
115-
fun streamResponseKotlin(request: ChatRequest, onResponse: ChatResponseChunk.() -> Unit) {
116-
streamResponse(request, { it.onResponse() })
115+
fun streamChatCompletionKotlin(request: ChatRequest, onResponse: ChatResponseChunk.() -> Unit) {
116+
streamChatCompletion(request, { it.onResponse() })
117117
}
118118

119119
/**
@@ -122,7 +122,7 @@ class OpenAI @JvmOverloads constructor(
122122
* to update the user without long delays between their input and OpenAI's
123123
* response.
124124
*
125-
* For *"simpler"* calls, you can use [generateResponse] which will block
125+
* For *"simpler"* calls, you can use [createChatCompletion] which will block
126126
* the thread until the entire response is generated.
127127
*
128128
* Instead of using the [ChatResponse], this method uses [ChatResponseChunk].
@@ -137,16 +137,17 @@ class OpenAI @JvmOverloads constructor(
137137
* @param onResponse The method to call for each chunk.
138138
* @param onFailure The method to call if the HTTP fails. This method will
139139
* not be called if OpenAI returns an error.
140-
* @see generateResponse
141-
* @see streamResponseKotlin
140+
* @see createChatCompletion
141+
* @see streamChatCompletionKotlin
142142
* @since 1.2.0
143143
*/
144144
@JvmOverloads
145-
fun streamResponse(
145+
fun streamChatCompletion(
146146
request: ChatRequest,
147147
onResponse: Consumer<ChatResponseChunk>, // use Consumer instead of Kotlin for better Java syntax
148148
onFailure: Consumer<IOException> = Consumer { it.printStackTrace() }
149149
) {
150+
@Suppress("DEPRECATION")
150151
request.stream = true // use requestResponse for stream=false
151152
val httpRequest = buildRequest(request)
152153

src/test/java/JavaChatStreamTest.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import com.cjcrafter.openai.OpenAI;
12
import com.cjcrafter.openai.chat.*;
23
import io.github.cdimascio.dotenv.Dotenv;
34

@@ -9,27 +10,31 @@ public class JavaChatStreamTest {
910

1011
public static void main(String[] args) {
1112
Scanner scan = new Scanner(System.in);
13+
14+
// Prepare the ChatRequest
15+
ChatMessage prompt = ChatMessage.toSystemMessage("Be as unhelpful as possible");
16+
List<ChatMessage> messages = new ArrayList<>(List.of(prompt));
17+
ChatRequest request = ChatRequest.builder()
18+
.model("gpt-3.5-turbo")
19+
.messages(messages).build();
20+
21+
// Load TOKEN from .env file
1222
String key = Dotenv.load().get("OPENAI_TOKEN");
23+
OpenAI openai = new OpenAI(key);
24+
25+
// Ask the user for input
26+
System.out.println("Enter text below:\n\n");
27+
String input = scan.nextLine();
28+
29+
// Stream the response. Print out each 'delta' (new tokens)
30+
messages.add(new ChatMessage(ChatUser.USER, input));
31+
openai.streamChatCompletion(request, message -> {
32+
System.out.print(message.get(0).getDelta());
1333

14-
// Create the initial prompt, we will reuse it later.
15-
String initialPrompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database.";
16-
List<ChatMessage> messages = new ArrayList<>(List.of(new ChatMessage(ChatUser.SYSTEM, initialPrompt)));
17-
ChatRequest request = new ChatRequest("gpt-3.5-turbo", messages);
18-
ChatBot bot = new ChatBot(key);
19-
20-
while (true) {
21-
System.out.println("Enter text below:\n\n");
22-
String input = scan.nextLine();
23-
24-
// Generate a response, and print it to the user.
25-
messages.add(new ChatMessage(ChatUser.USER, input));
26-
bot.streamResponse(request, message -> {
27-
System.out.print(message.get(0).getDelta());
28-
29-
if (message.get(0).getFinishReason() != null) {
30-
messages.add(message.get(0).getMessage());
31-
}
32-
});
33-
}
34+
// Once the message is complete, we should save the message to our
35+
// conversation (In case you want to generate more responses).
36+
if (message.get(0).getFinishReason() != null)
37+
messages.add(message.get(0).getMessage());
38+
});
3439
}
3540
}

src/test/java/JavaChatTest.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import com.cjcrafter.openai.OpenAI;
12
import com.cjcrafter.openai.chat.*;
23
import io.github.cdimascio.dotenv.Dotenv;
34

@@ -10,24 +11,44 @@ public class JavaChatTest {
1011

1112
public static void main(String[] args) throws IOException {
1213
Scanner scan = new Scanner(System.in);
13-
String key = Dotenv.load().get("OPENAI_TOKEN");
1414

15-
// Create the initial prompt, we will reuse it later.
16-
String initialPrompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database.";
17-
List<ChatMessage> messages = new ArrayList<>(List.of(new ChatMessage(ChatUser.SYSTEM, initialPrompt)));
18-
ChatRequest request = new ChatRequest("gpt-3.5-turbo", messages);
19-
ChatBot bot = new ChatBot(key);
15+
// This is the prompt that the bot will refer back to for every message.
16+
ChatMessage prompt = ChatMessage.toSystemMessage("You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database.");
17+
18+
// Use a mutable (modifiable) list! Always! You should be reusing the
19+
// ChatRequest variable, so in order for a conversation to continue
20+
// you need to be able to modify the list.
21+
List<ChatMessage> messages = new ArrayList<>(List.of(prompt));
2022

23+
// ChatRequest is the request we send to OpenAI API. You can modify the
24+
// model, temperature, maxTokens, etc. This should be saved, so you can
25+
// reuse it for a conversation.
26+
ChatRequest request = ChatRequest.builder()
27+
.model("gpt-3.5-turbo")
28+
.messages(messages).build();
29+
30+
// Loads the API key from the .env file in the root directory.
31+
String key = Dotenv.load().get("OPENAI_TOKEN");
32+
OpenAI openai = new OpenAI(key);
33+
34+
// The conversation lasts until the user quits the program
2135
while (true) {
36+
37+
// Prompt the user to enter a response
2238
System.out.println("Enter text below:\n\n");
2339
String input = scan.nextLine();
2440

25-
// Generate a response, and print it to the user.
26-
messages.add(new ChatMessage(ChatUser.USER, input));
27-
ChatResponse response = bot.generateResponse(request);
41+
// Add the newest user message to the conversation
42+
messages.add(ChatMessage.toUserMessage(input));
43+
44+
// Use the OpenAI API to generate a response to the current
45+
// conversation. Print the resulting message.
46+
ChatResponse response = openai.createChatCompletion(request);
2847
System.out.println("\n" + response.get(0).getMessage().getContent());
2948

30-
// Save the generated message to the bot's conversational memory
49+
// Save the generated message to the conversational memory. It is
50+
// crucial to save this message, otherwise future requests will be
51+
// confused that there was no response.
3152
messages.add(response.get(0).getMessage());
3253
}
3354
}
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import com.cjcrafter.openai.chat.ChatBot
1+
import com.cjcrafter.openai.OpenAI
22
import com.cjcrafter.openai.chat.ChatMessage.Companion.toSystemMessage
33
import com.cjcrafter.openai.chat.ChatMessage.Companion.toUserMessage
44
import com.cjcrafter.openai.chat.ChatRequest
@@ -7,26 +7,28 @@ import java.util.*
77

88
fun main(args: Array<String>) {
99
val scan = Scanner(System.`in`)
10-
val key = dotenv()["OPENAI_TOKEN"]
1110

12-
// Create the initial prompt, we will reuse it later.
13-
val initialPrompt = "Follow the users instructions"
14-
val messages = mutableListOf(initialPrompt.toSystemMessage())
15-
val request = ChatRequest("gpt-3.5-turbo", messages)
16-
val bot = ChatBot(key)
11+
// Prepare the ChatRequest
12+
val prompt = "Be as unhelpful as possible"
13+
val messages = mutableListOf(prompt.toSystemMessage())
14+
val request = ChatRequest(model="gpt-3.5-turbo", messages=messages)
15+
16+
// Loads the API key from the .env file in the root directory.
17+
val key = dotenv()["OPENAI_TOKEN"]
18+
val openai = OpenAI(key)
1719

18-
while (true) {
19-
println("Enter text below:\n")
20-
val input = scan.nextLine()
20+
// Ask the user for input
21+
println("Enter text below:\n")
22+
val input = scan.nextLine()
2123

22-
// Generate a response, and print it to the user.
23-
messages.add(input.toUserMessage())
24-
bot.streamResponseKotlin(request) {
25-
print(choices[0].delta)
24+
// Generate a response, and print it to the user.
25+
messages.add(input.toUserMessage())
26+
openai.streamChatCompletionKotlin(request) {
27+
print(choices[0].delta)
2628

27-
if (choices[0].finishReason != null) {
28-
messages.add(choices[0].message)
29-
}
30-
}
29+
// Once the message is complete, we should save the message to our
30+
// conversation (In case you want to generate more responses).
31+
if (choices[0].finishReason != null)
32+
messages.add(choices[0].message)
3133
}
3234
}

src/test/kotlin/KotlinChatTest.kt

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import com.cjcrafter.openai.chat.ChatBot
1+
import com.cjcrafter.openai.OpenAI
22
import com.cjcrafter.openai.chat.ChatMessage.Companion.toSystemMessage
33
import com.cjcrafter.openai.chat.ChatMessage.Companion.toUserMessage
44
import com.cjcrafter.openai.chat.ChatRequest
@@ -7,24 +7,42 @@ import java.util.*
77

88
fun main(args: Array<String>) {
99
val scan = Scanner(System.`in`)
10-
val key = dotenv()["OPENAI_TOKEN"]
1110

12-
// Create the initial prompt, we will reuse it later.
13-
val initialPrompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database."
14-
val messages = mutableListOf(initialPrompt.toSystemMessage())
15-
val request = ChatRequest("gpt-3.5-turbo", messages)
16-
val bot = ChatBot(key)
11+
// This is the prompt that the bot will refer back to for every message.
12+
val prompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database."
13+
14+
// Use a mutable (modifiable) list! Always! You should be reusing the
15+
// ChatRequest variable, so in order for a conversation to continue you
16+
// need to be able to modify the list.
17+
val messages = mutableListOf(prompt.toSystemMessage())
1718

19+
// ChatRequest is the request we send to OpenAI API. You can modify the
20+
// model, temperature, maxTokens, etc. This should be saved, so you can
21+
// reuse it for a conversation.
22+
val request = ChatRequest(model="gpt-3.5-turbo", messages=messages)
23+
24+
// Loads the API key from the .env file in the root directory.
25+
val key = dotenv()["OPENAI_TOKEN"]
26+
val openai = OpenAI(key)
27+
28+
// The conversation lasts until the user quits the program
1829
while (true) {
30+
31+
// Prompt the user to enter a response
1932
println("Enter text below:\n")
2033
val input = scan.nextLine()
2134

22-
// Generate a response, and print it to the user.
35+
// Add the newest user message to the conversation
2336
messages.add(input.toUserMessage())
24-
val response = bot.generateResponse(request)
37+
38+
// Use the OpenAI API to generate a response to the current
39+
// conversation. Print the resulting message.
40+
val response = openai.createChatCompletion(request)
2541
println("\n${response[0].message.content}\n")
2642

27-
// Save the generated message to the bot's conversational memory
43+
// Save the generated message to the conversational memory. It is
44+
// crucial to save this message, otherwise future requests will be
45+
// confused that there was no response.
2846
messages.add(response[0].message)
2947
}
3048
}

0 commit comments

Comments
 (0)