Skip to content

Commit f0423a7

Browse files
docs: swap example from .completions() to .chat().completions() (#20)
1 parent ad81119 commit f0423a7

File tree

1 file changed

+16
-7
lines changed
  • openai-java-example/src/main/java/com/openai/example

1 file changed

+16
-7
lines changed

openai-java-example/src/main/java/com/openai/example/Main.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,32 @@ private Main() {}
1010

1111
public static void main(String[] args) {
1212
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)
1515
.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+
))
1724
.build();
1825

1926
// 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);
2230

2331
System.out.println("\n-----------------------------------\n");
2432

2533
// Streaming example
26-
try (StreamResponse<Completion> messageStreamResponse = client.completions().createStreaming(completionCreateParams)) {
34+
try (StreamResponse<ChatCompletionChunk> messageStreamResponse = client.chat().completions().createStreaming(completionCreateParams)) {
2735
messageStreamResponse.stream()
2836
.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);
3039
} catch (Exception e) {
3140
System.out.println(e.getMessage());
3241
}

0 commit comments

Comments
 (0)