Skip to content

Commit 49c3283

Browse files
committed
add stream (realtime token access) tests
1 parent 2659687 commit 49c3283

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import com.cjcrafter.openai.chat.*;
2+
import io.github.cdimascio.dotenv.Dotenv;
3+
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.Scanner;
7+
8+
public class JavaChatStreamTest {
9+
10+
public static void main(String[] args) {
11+
Scanner scan = new Scanner(System.in);
12+
String key = Dotenv.load().get("OPENAI_TOKEN");
13+
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+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import com.cjcrafter.openai.chat.ChatBot
2+
import com.cjcrafter.openai.chat.ChatMessage.Companion.toSystemMessage
3+
import com.cjcrafter.openai.chat.ChatMessage.Companion.toUserMessage
4+
import com.cjcrafter.openai.chat.ChatRequest
5+
import io.github.cdimascio.dotenv.dotenv
6+
import java.util.*
7+
8+
fun main(args: Array<String>) {
9+
val scan = Scanner(System.`in`)
10+
val key = dotenv()["OPENAI_TOKEN"]
11+
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)
17+
18+
while (true) {
19+
println("Enter text below:\n")
20+
val input = scan.nextLine()
21+
22+
// Generate a response, and print it to the user.
23+
messages.add(input.toUserMessage())
24+
bot.streamResponseKotlin(request) {
25+
print(choices[0].delta)
26+
27+
if (choices[0].finishReason != null) {
28+
messages.add(choices[0].message)
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)