@@ -10,23 +10,32 @@ private Main() {}
10
10
11
11
public static void main (String [] args ) {
12
12
OpenAIClient client = OpenAIOkHttpClient .fromEnv ();
13
- CompletionCreateParams completionCreateParams = CompletionCreateParams .builder ()
14
- .model (CompletionCreateParams .Model .GPT_3_5_TURBO_INSTRUCT )
13
+ ChatCompletionCreateParams completionCreateParams = ChatCompletionCreateParams .builder ()
14
+ .model (ChatCompletionCreateParams .Model .GPT_3_5_TURBO )
15
15
.maxTokens (1024 )
16
- .prompt ("Tell me a story about building the best SDK!" )
16
+ .addMessage (ChatCompletionMessageParam .ofChatCompletionUserMessageParam (
17
+ ChatCompletionUserMessageParam .builder ()
18
+ .role (ChatCompletionUserMessageParam .Role .USER )
19
+ .content (ChatCompletionUserMessageParam .Content .ofTextContent (
20
+ "Tell me a story about building the best SDK!"
21
+ ))
22
+ .build ()
23
+ ))
17
24
.build ();
18
25
19
26
// Non-streaming example
20
- client .completions ().create (completionCreateParams ).choices ()
21
- .forEach (choice -> System .out .println (choice .text ()));
27
+ client .chat ().completions ().create (completionCreateParams ).choices ().stream ()
28
+ .flatMap (choice -> choice .message ().content ().stream ())
29
+ .forEach (System .out ::println );
22
30
23
31
System .out .println ("\n -----------------------------------\n " );
24
32
25
33
// Streaming example
26
- try (StreamResponse <Completion > messageStreamResponse = client .completions ().createStreaming (completionCreateParams )) {
34
+ try (StreamResponse <ChatCompletionChunk > messageStreamResponse = client . chat () .completions ().createStreaming (completionCreateParams )) {
27
35
messageStreamResponse .stream ()
28
36
.flatMap (completion -> completion .choices ().stream ())
29
- .forEach (choice -> System .out .print (choice .text ()));
37
+ .flatMap (choice -> choice .delta ().content ().stream ())
38
+ .forEach (System .out ::print );
30
39
} catch (Exception e ) {
31
40
System .out .println (e .getMessage ());
32
41
}
0 commit comments