1
+ import com .cjcrafter .openai .OpenAI ;
2
+ import com .cjcrafter .openai .chat .ChatMessage ;
3
+ import com .cjcrafter .openai .chat .ChatRequest ;
4
+ import com .cjcrafter .openai .chat .ChatResponse ;
5
+ import com .cjcrafter .openai .completions .CompletionRequest ;
6
+ import com .cjcrafter .openai .exception .OpenAIError ;
7
+ import io .github .cdimascio .dotenv .Dotenv ;
8
+
9
+ import java .util .ArrayList ;
10
+ import java .util .Collections ;
11
+ import java .util .List ;
12
+ import java .util .Scanner ;
13
+
14
+ public class JavaTest {
15
+
16
+ // Colors for pretty formatting
17
+ public static final String RESET = "\033 [0m" ;
18
+ public static final String BLACK = "\033 [0;30m" ;
19
+ public static final String RED = "\033 [0;31m" ;
20
+ public static final String GREEN = "\033 [0;32m" ;
21
+ public static final String YELLOW = "\033 [0;33m" ;
22
+ public static final String BLUE = "\033 [0;34m" ;
23
+ public static final String PURPLE = "\033 [0;35m" ;
24
+ public static final String CYAN = "\033 [0;36m" ;
25
+ public static final String WHITE = "\033 [0;37m" ;
26
+
27
+ public static void main (String [] args ) throws OpenAIError {
28
+ Scanner scanner = new Scanner (System .in );
29
+
30
+ // Print out the menu of options
31
+ System .out .println (GREEN + "Please select one of the options below by typing a number." );
32
+ System .out .println ();
33
+ System .out .println (GREEN + " 1. Completion (create, sync)" );
34
+ System .out .println (GREEN + " 2. Completion (stream, sync)" );
35
+ System .out .println (GREEN + " 3. Completion (create, async)" );
36
+ System .out .println (GREEN + " 4. Completion (stream, async)" );
37
+ System .out .println (GREEN + " 5. Chat (create, sync)" );
38
+ System .out .println (GREEN + " 6. Chat (stream, sync)" );
39
+ System .out .println (GREEN + " 7. Chat (create, async)" );
40
+ System .out .println (GREEN + " 8. Chat (stream, async)" );
41
+ System .out .println ();
42
+
43
+ // Determine which method to call
44
+ switch (scanner .nextLine ()) {
45
+ case "1" :
46
+ doCompletion (false , false );
47
+ break ;
48
+ case "2" :
49
+ doCompletion (true , false );
50
+ break ;
51
+ case "3" :
52
+ doCompletion (false , true );
53
+ break ;
54
+ case "4" :
55
+ doCompletion (true , true );
56
+ break ;
57
+ case "5" :
58
+ doChat (false , false );
59
+ break ;
60
+ case "6" :
61
+ doChat (true , false );
62
+ break ;
63
+ case "7" :
64
+ doChat (false , true );
65
+ break ;
66
+ case "8" :
67
+ doChat (true , true );
68
+ break ;
69
+ default :
70
+ System .err .println ("Invalid option" );
71
+ break ;
72
+ }
73
+ }
74
+
75
+ public static void doCompletion (boolean stream , boolean async ) throws OpenAIError {
76
+ Scanner scan = new Scanner (System .in );
77
+ System .out .println (YELLOW + "Enter completion: " );
78
+ String input = scan .nextLine ();
79
+
80
+ // CompletionRequest contains the data we sent to the OpenAI API. We use
81
+ // 128 tokens, so we have a bit of a delay before the response (for testing).
82
+ CompletionRequest request = CompletionRequest .builder ()
83
+ .model ("davinci" )
84
+ .prompt (input )
85
+ .maxTokens (128 ).build ();
86
+
87
+ // Loads the API key from the .env file in the root directory.
88
+ String key = Dotenv .load ().get ("OPENAI_TOKEN" );
89
+ OpenAI openai = new OpenAI (key );
90
+
91
+ System .out .println (RESET + "Generating Response" + PURPLE );
92
+ if (stream ) {
93
+ if (async )
94
+ openai .streamCompletionAsync (request , response -> System .out .print (response .get (0 ).getText ()));
95
+ else
96
+ openai .streamCompletion (request , response -> System .out .print (response .get (0 ).getText ()));
97
+ } else {
98
+ if (async )
99
+ openai .createCompletionAsync (request , response -> System .out .println (response .get (0 ).getText ()));
100
+ else
101
+ System .out .println (openai .createCompletion (request ).get (0 ).getText ());
102
+ }
103
+
104
+ System .out .println (CYAN + " !!! Code has finished executing. Wait for async code to complete." + RESET );
105
+ }
106
+
107
+ public static void doChat (boolean stream , boolean async ) throws OpenAIError {
108
+ Scanner scan = new Scanner (System .in );
109
+
110
+ // This is the prompt that the bot will refer back to for every message.
111
+ 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." );
112
+
113
+ // Use a mutable (modifiable) list! Always! You should be reusing the
114
+ // ChatRequest variable, so in order for a conversation to continue
115
+ // you need to be able to modify the list.
116
+ List <ChatMessage > messages = new ArrayList <>(Collections .singletonList (prompt ));
117
+
118
+ // ChatRequest is the request we send to OpenAI API. You can modify the
119
+ // model, temperature, maxTokens, etc. This should be saved, so you can
120
+ // reuse it for a conversation.
121
+ ChatRequest request = ChatRequest .builder ()
122
+ .model ("gpt-3.5-turbo" )
123
+ .messages (messages ).build ();
124
+
125
+ // Loads the API key from the .env file in the root directory.
126
+ String key = Dotenv .load ().get ("OPENAI_TOKEN" );
127
+ OpenAI openai = new OpenAI (key );
128
+
129
+ // The conversation lasts until the user quits the program
130
+ while (true ) {
131
+
132
+ // Prompt the user to enter a response
133
+ System .out .println (YELLOW + "Enter text below:\n \n " );
134
+ String input = scan .nextLine ();
135
+
136
+ // Add the newest user message to the conversation
137
+ messages .add (ChatMessage .toUserMessage (input ));
138
+
139
+ System .out .println (RESET + "Generating Response" + PURPLE );
140
+ if (stream ) {
141
+ if (async ) {
142
+ openai .streamChatCompletionAsync (request , response -> {
143
+ System .out .print (response .get (0 ).getDelta ());
144
+ if (response .get (0 ).isFinished ())
145
+ messages .add (response .get (0 ).getMessage ());
146
+ });
147
+ } else {
148
+ openai .streamChatCompletion (request , response -> {
149
+ System .out .print (response .get (0 ).getDelta ());
150
+ if (response .get (0 ).isFinished ())
151
+ messages .add (response .get (0 ).getMessage ());
152
+ });
153
+ }
154
+ } else {
155
+ if (async ) {
156
+ openai .createChatCompletionAsync (request , response -> {
157
+ System .out .println (response .get (0 ).getMessage ().getContent ());
158
+ messages .add (response .get (0 ).getMessage ());
159
+ });
160
+ } else {
161
+ ChatResponse response = openai .createChatCompletion (request );
162
+ System .out .println (response .get (0 ).getMessage ().getContent ());
163
+ messages .add (response .get (0 ).getMessage ());
164
+ }
165
+ }
166
+
167
+ System .out .println (CYAN + " !!! Code has finished executing. Wait for async code to complete." );
168
+ }
169
+ }
170
+ }
0 commit comments