Skip to content

Commit dbde7df

Browse files
authored
Merge pull request #2229 from aahill/oyd-qs
updating C# code
2 parents 49e5483 + d510f17 commit dbde7df

File tree

2 files changed

+22
-90
lines changed

2 files changed

+22
-90
lines changed

articles/ai-services/openai/includes/use-your-data-dotnet.md

Lines changed: 21 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: travisw
55
ms.author: travisw
66
ms.service: azure-ai-openai
77
ms.topic: include
8-
ms.date: 03/07/2024
8+
ms.date: 01/09/2025
99
---
1010

1111
[!INCLUDE [Set up required variables](./use-your-data-common-variables.md)]
@@ -14,14 +14,12 @@ ms.date: 03/07/2024
1414

1515
From the project directory, open the *Program.cs* file and replace its contents with the following code:
1616

17-
### Without response streaming
18-
1917
```csharp
20-
using Azure;
18+
using System;
2119
using Azure.AI.OpenAI;
20+
using System.ClientModel;
2221
using Azure.AI.OpenAI.Chat;
2322
using OpenAI.Chat;
24-
using System.Text.Json;
2523
using static System.Environment;
2624

2725
string azureOpenAIEndpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
@@ -31,36 +29,38 @@ string searchEndpoint = GetEnvironmentVariable("AZURE_AI_SEARCH_ENDPOINT");
3129
string searchKey = GetEnvironmentVariable("AZURE_AI_SEARCH_API_KEY");
3230
string searchIndex = GetEnvironmentVariable("AZURE_AI_SEARCH_INDEX");
3331

34-
#pragma warning disable AOAI001
3532
AzureOpenAIClient azureClient = new(
36-
new Uri(azureOpenAIEndpoint),
37-
new AzureKeyCredential(azureOpenAIKey));
33+
new Uri(azureOpenAIEndpoint),
34+
new ApiKeyCredential(azureOpenAIKey));
3835
ChatClient chatClient = azureClient.GetChatClient(deploymentName);
3936

37+
// Extension methods to use data sources with options are subject to SDK surface changes. Suppress the
38+
// warning to acknowledge and this and use the subject-to-change AddDataSource method.
39+
#pragma warning disable AOAI001
40+
4041
ChatCompletionOptions options = new();
4142
options.AddDataSource(new AzureSearchChatDataSource()
4243
{
43-
Endpoint = new Uri(searchEndpoint),
44-
IndexName = searchIndex,
45-
Authentication = DataSourceAuthentication.FromApiKey(searchKey),
44+
Endpoint = new Uri(searchEndpoint),
45+
IndexName = searchIndex,
46+
Authentication = DataSourceAuthentication.FromApiKey(searchKey),
4647
});
4748

4849
ChatCompletion completion = chatClient.CompleteChat(
49-
[
50-
new UserChatMessage("What are my available health plans?"),
51-
], options);
52-
53-
Console.WriteLine(completion.Content[0].Text);
50+
[
51+
new UserChatMessage("What health plans are available?"),
52+
],
53+
options);
5454

55-
AzureChatMessageContext onYourDataContext = completion.GetAzureMessageContext();
55+
ChatMessageContext onYourDataContext = completion.GetMessageContext();
5656

5757
if (onYourDataContext?.Intent is not null)
5858
{
59-
Console.WriteLine($"Intent: {onYourDataContext.Intent}");
59+
Console.WriteLine($"Intent: {onYourDataContext.Intent}");
6060
}
61-
foreach (AzureChatCitation citation in onYourDataContext?.Citations ?? [])
61+
foreach (ChatCitation citation in onYourDataContext?.Citations ?? [])
6262
{
63-
Console.WriteLine($"Citation: {citation.Content}");
63+
Console.WriteLine($"Citation: {citation.Content}");
6464
}
6565
```
6666

@@ -86,72 +86,4 @@ Thank you for your interest in the Contoso electronics plan and benefit packages
8686
learn more about the various options available to you...// Omitted for brevity
8787
```
8888

89-
This will wait until the model has generated its entire response before printing the results. Alternatively, if you want to asynchronously stream the response and print the results, you can replace the contents of *Program.cs* with the code in the next example.
90-
91-
### Async with streaming
92-
93-
```csharp
94-
using Azure;
95-
using Azure.AI.OpenAI;
96-
using Azure.AI.OpenAI.Chat;
97-
using OpenAI.Chat;
98-
using static System.Environment;
99-
100-
string azureOpenAIEndpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
101-
string azureOpenAIKey = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
102-
string deploymentName = GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_ID");
103-
string searchEndpoint = GetEnvironmentVariable("AZURE_AI_SEARCH_ENDPOINT");
104-
string searchKey = GetEnvironmentVariable("AZURE_AI_SEARCH_API_KEY");
105-
string searchIndex = GetEnvironmentVariable("AZURE_AI_SEARCH_INDEX");
106-
107-
#pragma warning disable AOAI001
108-
109-
AzureOpenAIClient azureClient = new(
110-
new Uri(azureOpenAIEndpoint),
111-
new AzureKeyCredential(azureOpenAIKey));
112-
ChatClient chatClient = azureClient.GetChatClient(deploymentName);
113-
114-
ChatCompletionOptions options = new();
115-
options.AddDataSource(new AzureSearchChatDataSource()
116-
{
117-
Endpoint = new Uri(searchEndpoint),
118-
IndexName = searchIndex,
119-
Authentication = DataSourceAuthentication.FromApiKey(searchKey),
120-
});
121-
122-
var chatUpdates = chatClient.CompleteChatStreamingAsync(
123-
[
124-
new UserChatMessage("What are my available health plans?"),
125-
], options);
126-
127-
AzureChatMessageContext onYourDataContext = null;
128-
await foreach (var chatUpdate in chatUpdates)
129-
{
130-
if (chatUpdate.Role.HasValue)
131-
{
132-
Console.WriteLine($"{chatUpdate.Role}: ");
133-
}
134-
135-
foreach (var contentPart in chatUpdate.ContentUpdate)
136-
{
137-
Console.Write(contentPart.Text);
138-
}
139-
140-
if (onYourDataContext == null)
141-
{
142-
onYourDataContext = chatUpdate.GetAzureMessageContext();
143-
}
144-
}
145-
146-
Console.WriteLine();
147-
if (onYourDataContext?.Intent is not null)
148-
{
149-
Console.WriteLine($"Intent: {onYourDataContext.Intent}");
150-
}
151-
foreach (AzureChatCitation citation in onYourDataContext?.Citations ?? [])
152-
{
153-
Console.Write($"Citation: {citation.Content}");
154-
}
155-
```
156-
157-
89+
This will wait until the model has generated its entire response before printing the results.

articles/ai-services/openai/use-your-data-quickstart.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.custom: devx-track-dotnet, devx-track-extended-java, devx-track-js, devx-trac
88
ms.topic: quickstart
99
author: aahill
1010
ms.author: aahi
11-
ms.date: 10/22/2024
11+
ms.date: 01/09/2025
1212
recommendations: false
1313
zone_pivot_groups: openai-use-your-data
1414
---

0 commit comments

Comments
 (0)