Skip to content

Commit 76ffdbb

Browse files
authored
Usage sample of BYOD with ChatCompletions (Azure#36511)
* sample of completions with BYOD * add comments with links to info
1 parent b3d59a7 commit 76ffdbb

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package com.azure.ai.openai.usage;
5+
6+
import com.azure.ai.openai.OpenAIClient;
7+
import com.azure.ai.openai.OpenAIClientBuilder;
8+
import com.azure.ai.openai.models.AzureChatExtensionConfiguration;
9+
import com.azure.ai.openai.models.AzureChatExtensionType;
10+
import com.azure.ai.openai.models.AzureCognitiveSearchChatExtensionConfiguration;
11+
import com.azure.ai.openai.models.ChatChoice;
12+
import com.azure.ai.openai.models.ChatCompletions;
13+
import com.azure.ai.openai.models.ChatCompletionsOptions;
14+
import com.azure.ai.openai.models.ChatMessage;
15+
import com.azure.ai.openai.models.ChatRole;
16+
import com.azure.core.credential.AzureKeyCredential;
17+
import com.azure.core.util.BinaryData;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.List;
22+
23+
/**
24+
* Sample demonstrates the "Azure OpenAI on your data" feature. Prerequisites and guides
25+
* for this feature can be found at:
26+
* https://learn.microsoft.com/azure/ai-services/openai/use-your-data-quickstart?tabs=command-line&pivots=programming-language-studio
27+
*/
28+
public class ChatCompletionsWithYourData {
29+
/**
30+
* Runs the sample and demonstrates configuration of Azure Cognitive Search as a data source.
31+
*
32+
* @param args Unused. Arguments to the program.
33+
*/
34+
public static void main(String[] args){
35+
String azureOpenaiKey = "{azure-open-ai-key}";
36+
String endpoint = "{azure-open-ai-endpoint}";
37+
String deploymentOrModelId = "{azure-open-ai-deployment-model-id}";
38+
39+
OpenAIClient client = new OpenAIClientBuilder()
40+
.endpoint(endpoint)
41+
.credential(new AzureKeyCredential(azureOpenaiKey))
42+
.buildClient();
43+
44+
List<ChatMessage> chatMessages = new ArrayList<>();
45+
chatMessages.add(new ChatMessage(ChatRole.USER, "How many of our customers are using the latest version of our SDK?"));
46+
47+
ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions(chatMessages);
48+
49+
// These configuration values come from your Azure Cognitive Search resource. Using Azure
50+
// Cognitive Search as a data source is briefly described in the "Azure OpenAI on
51+
// your data" quickstart, linked above. A more detailed guide can be found here:
52+
// https://learn.microsoft.com/azure/search/search-get-started-portal
53+
54+
// Your Azure Cognitive Search endpoint, admin key, and index name
55+
String azureSearchEndpoint = "{azure-cognitive-search-endpoint}";
56+
String azureSearchAdminKey = "{azure-cognitive-search-key}";
57+
String azureSearchIndexName = "{azure-cognitive-search-index-name}";
58+
59+
AzureCognitiveSearchChatExtensionConfiguration cognitiveSearchConfiguration =
60+
new AzureCognitiveSearchChatExtensionConfiguration(
61+
azureSearchEndpoint,
62+
azureSearchAdminKey,
63+
azureSearchIndexName
64+
);
65+
66+
AzureChatExtensionConfiguration extensionConfiguration =
67+
new AzureChatExtensionConfiguration(
68+
AzureChatExtensionType.AZURE_COGNITIVE_SEARCH,
69+
BinaryData.fromObject(cognitiveSearchConfiguration));
70+
71+
chatCompletionsOptions.setDataSources(Arrays.asList(extensionConfiguration));
72+
73+
ChatCompletions chatCompletions = client.getChatCompletions(deploymentOrModelId, chatCompletionsOptions);
74+
75+
for (ChatChoice choice : chatCompletions.getChoices()) {
76+
ChatMessage message = choice.getMessage();
77+
System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
78+
System.out.println("Message:");
79+
System.out.println(message.getContent());
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)