Skip to content

Commit 03f40c9

Browse files
committed
Update test examples to use new API
1 parent 5c33072 commit 03f40c9

File tree

4 files changed

+92
-48
lines changed

4 files changed

+92
-48
lines changed

src/test/java/JavaChatStreamTest.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,31 @@ public class JavaChatStreamTest {
1010

1111
public static void main(String[] args) {
1212
Scanner scan = new Scanner(System.in);
13-
String key = Dotenv.load().get("OPENAI_TOKEN");
1413

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);
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
22+
String key = Dotenv.load().get("OPENAI_TOKEN");
1923
OpenAI openai = new OpenAI(key);
2024

21-
while (true) {
22-
System.out.println("Enter text below:\n\n");
23-
String input = scan.nextLine();
25+
// Ask the user for input
26+
System.out.println("Enter text below:\n\n");
27+
String input = scan.nextLine();
2428

25-
// Generate a response, and print it to the user.
26-
messages.add(new ChatMessage(ChatUser.USER, input));
27-
openai.streamChatCompletion(request, message -> {
28-
System.out.print(message.get(0).getDelta());
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());
2933

30-
if (message.get(0).getFinishReason() != null) {
31-
messages.add(message.get(0).getMessage());
32-
}
33-
});
34-
}
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+
});
3539
}
3640
}

src/test/java/JavaChatTest.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,44 @@ public class JavaChatTest {
1111

1212
public static void main(String[] args) throws IOException {
1313
Scanner scan = new Scanner(System.in);
14-
String key = Dotenv.load().get("OPENAI_TOKEN");
1514

16-
// Create the initial prompt, we will reuse it later.
17-
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.";
18-
List<ChatMessage> messages = new ArrayList<>(List.of(new ChatMessage(ChatUser.SYSTEM, initialPrompt)));
19-
ChatRequest request = new ChatRequest("gpt-3.5-turbo", messages);
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));
22+
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");
2032
OpenAI openai = new OpenAI(key);
2133

34+
// The conversation lasts until the user quits the program
2235
while (true) {
36+
37+
// Prompt the user to enter a response
2338
System.out.println("Enter text below:\n\n");
2439
String input = scan.nextLine();
2540

26-
// Generate a response, and print it to the user.
27-
messages.add(new ChatMessage(ChatUser.USER, input));
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.
2846
ChatResponse response = openai.createChatCompletion(request);
2947
System.out.println("\n" + response.get(0).getMessage().getContent());
3048

31-
// 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.
3252
messages.add(response.get(0).getMessage());
3353
}
3454
}

src/test/kotlin/KotlinChatStreamTest.kt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
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"]
1618
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-
openai.streamChatCompletionKotlin(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: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
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())
18+
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"]
1626
val openai = OpenAI(key)
1727

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())
37+
38+
// Use the OpenAI API to generate a response to the current
39+
// conversation. Print the resulting message.
2440
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)