-
Notifications
You must be signed in to change notification settings - Fork 2k
/
GetCompletionsSample.java
54 lines (45 loc) · 2.29 KB
/
GetCompletionsSample.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
53
54
// 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.Choice;
import com.azure.ai.openai.models.Completions;
import com.azure.ai.openai.models.CompletionsOptions;
import com.azure.ai.openai.models.CompletionsUsage;
import com.azure.core.credential.AzureKeyCredential;
import java.util.ArrayList;
import java.util.List;
/**
* Sample 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.
*/
public class GetCompletionsSample {
/**
* 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 = "{azure-open-ai-key}";
String endpoint = "{azure-open-ai-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?");
Completions completions = client.getCompletions(deploymentOrModelId, new CompletionsOptions(prompt));
System.out.printf("Model ID=%s is created at %s.%n", completions.getId(), completions.getCreatedAt());
for (Choice choice : completions.getChoices()) {
System.out.printf("Index: %d, Text: %s.%n", choice.getIndex(), choice.getText());
}
CompletionsUsage usage = completions.getUsage();
System.out.printf("Usage: number of prompt token is %d, "
+ "number of completion token is %d, and number of total tokens in request and response is %d.%n",
usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
}
}