-
Notifications
You must be signed in to change notification settings - Fork 2k
/
GetCompletionsStreamSample.java
52 lines (45 loc) · 2.27 KB
/
GetCompletionsStreamSample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.ai.openai.usage;
import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.Completions;
import com.azure.ai.openai.models.CompletionsOptions;
import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.util.Configuration;
import com.azure.core.util.IterableStream;
import java.util.ArrayList;
import java.util.List;
/**
* Sample demonstrates how to get completions as a stream for the provided input prompts. Completions support a wide variety of
* tasks and generate text that continues from or "completes" provided prompt data.
*/
public class GetCompletionsStreamSample {
/**
* Runs the sample algorithm and demonstrates how to get completions for the provided input prompts.
* Completions support a wide variety of tasks and generate text that continues from or "completes" provided
* prompt data.
*
* @param args Unused. Arguments to the program.
*/
public static void main(String[] args) {
String azureOpenaiKey = Configuration.getGlobalConfiguration().get("AZURE_OPENAI_KEY");
String endpoint = Configuration.getGlobalConfiguration().get("AZURE_OPENAI_ENDPOINT");
String deploymentOrModelId = "{azure-open-ai-deployment-model-id}";
OpenAIClient client = new OpenAIClientBuilder()
.endpoint(endpoint)
.credential(new AzureKeyCredential(azureOpenaiKey))
.buildClient();
List<String> prompt = new ArrayList<>();
prompt.add("Why did the eagles not carry Frodo Baggins to Mordor?");
IterableStream<Completions> completionsStream = client.getCompletionsStream(deploymentOrModelId,
new CompletionsOptions(prompt).setMaxTokens(1000).setStream(true));
completionsStream
.stream()
// Remove .skip(1) when using Non-Azure OpenAI API
// Note: the first chat completions can be ignored when using Azure OpenAI service which is a known service bug.
// TODO: remove .skip(1) when service fix the issue.
.skip(1)
.forEach(completions -> System.out.print(completions.getChoices().get(0).getText()));
}
}