Skip to content

Commit 15c0a2c

Browse files
authored
OpenAI library updates (#50548)
* Updated to OpenAI libraries
1 parent 7c97d76 commit 15c0a2c

File tree

9 files changed

+56
-26
lines changed

9 files changed

+56
-26
lines changed

docs/azure/sdk/dependency-injection.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ To register and configure service clients from an [`Azure.`-prefixed package](pa
2626
dotnet add package Azure.Identity
2727
```
2828
29-
For demonstration purposes, the sample code in this article uses the Key Vault Secrets, Blob Storage, Service Bus, and Azure OpenAI libraries. Install the following packages to follow along:
29+
For demonstration purposes, the sample code in this article uses the Key Vault Secrets, Blob Storage, Service Bus, and OpenAI libraries. Install the following packages to follow along:
3030
3131
```dotnetcli
3232
dotnet add package Azure.Security.KeyVault.Secrets
3333
dotnet add package Azure.Storage.Blobs
3434
dotnet add package Azure.Messaging.ServiceBus
35-
dotnet add package Azure.AI.OpenAI
35+
dotnet add package OpenAI
3636
```
3737

3838
## Register clients and subclients
@@ -43,15 +43,15 @@ In the *Program.cs* file, invoke the <xref:Microsoft.Extensions.Azure.AzureClien
4343

4444
### [WebApplicationBuilder](#tab/web-app-builder)
4545

46-
:::code language="csharp" source="snippets/dependency-injection/WebApplicationBuilder/Program.cs" id="snippet_WebApplicationBuilder" highlight="9-39":::
46+
:::code language="csharp" source="snippets/dependency-injection/WebApplicationBuilder/Program.cs" id="snippet_WebApplicationBuilder" highlight="11-48":::
4747

4848
### [HostApplicationBuilder](#tab/host-app-builder)
4949

50-
:::code language="csharp" source="snippets/dependency-injection/HostApplicationBuilder/Program.cs" highlight="11-40":::
50+
:::code language="csharp" source="snippets/dependency-injection/HostApplicationBuilder/Program.cs" highlight="13-50":::
5151

5252
### [HostBuilder](#tab/host-builder)
5353

54-
:::code language="csharp" source="snippets/dependency-injection/HostBuilder/Program.cs" id="snippet_HostBuilder" highlight="10-39":::
54+
:::code language="csharp" source="snippets/dependency-injection/HostBuilder/Program.cs" id="snippet_HostBuilder" highlight="12-49":::
5555

5656
---
5757

@@ -60,7 +60,7 @@ In the preceding code:
6060
* Key Vault Secrets, Blob Storage, and Service Bus clients are registered using the <xref:Microsoft.Extensions.Azure.SecretClientBuilderExtensions.AddSecretClient%2A>, <xref:Microsoft.Extensions.Azure.BlobClientBuilderExtensions.AddBlobServiceClient%2A> and <xref:Microsoft.Extensions.Azure.ServiceBusClientBuilderExtensions.AddServiceBusClientWithNamespace%2A>, respectively. The `Uri`- and `string`-typed arguments are passed. To avoid specifying these URLs explicitly, see the [Store configuration separately from code](#store-configuration-separately-from-code) section.
6161
* Each registered client automatically uses <xref:Azure.Identity.DefaultAzureCredential> for `TokenCredential` unless you configure a different type of credential (for example, using `WithCredential`).
6262
* Service Bus subclients are registered for each queue on the service using the subclient and corresponding options types. The queue names for the subclients are retrieved using a separate method outside of the service registration because the `GetQueuesAsync` method must be run asynchronously.
63-
* An Azure OpenAI client is registered using a custom client factory via the <xref:Microsoft.Extensions.Azure.AzureClientFactoryBuilder.AddClient%2A> method, which provides control over how a client instance is created. Custom client factories are useful in the following cases:
63+
* A `ResponsesClient` is registered using a custom client factory via the <xref:Microsoft.Extensions.Azure.AzureClientFactoryBuilder.AddClient%2A> method, which provides control over how a client instance is created. Custom client factories are useful in the following cases:
6464
* You need to use other dependencies during the client construction.
6565
* A registration extension method doesn't exist for the service client you want to register.
6666

docs/azure/sdk/snippets/authentication/Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
</PropertyGroup>
55
<ItemGroup>
66
<!-- Azure SDK packages -->
7-
<PackageVersion Include="Azure.AI.OpenAI" Version="2.1.0" />
7+
<PackageVersion Include="OpenAI" Version="2.8.0" />
88
<PackageVersion Include="Microsoft.Extensions.AI" Version="9.3.0-preview.1.25114.11" />
99
<PackageVersion Include="Microsoft.Extensions.AI.OpenAI" Version="9.3.0-preview.1.25114.11" />
1010
<PackageVersion Include="Microsoft.Extensions.Azure" Version="1.13.1" />

docs/azure/sdk/snippets/dependency-injection/Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
44
</PropertyGroup>
55
<ItemGroup>
6-
<PackageVersion Include="Azure.Identity" Version="1.17.1" />
76
<PackageVersion Include="Microsoft.Identity.Client" Version="4.67.2" />
87
<PackageVersion Include="Azure.Security.KeyVault.Secrets" Version="4.6.0" />
98
<PackageVersion Include="Azure.Messaging.ServiceBus" Version="7.17.5" />
@@ -12,6 +11,7 @@
1211
<PackageVersion Include="Microsoft.AspNetCore.OpenApi" Version="8.0.8" />
1312
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
1413
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.7.3" />
15-
<PackageVersion Include="Azure.AI.OpenAI" Version="2.1.0" />
14+
<PackageVersion Include="Azure.Identity" Version="1.17.1" />
15+
<PackageVersion Include="OpenAI" Version="2.8.0" />
1616
</ItemGroup>
1717
</Project>

docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/HostApplicationBuilder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
<PackageReference Include="Azure.Storage.Blobs" />
1515
<PackageReference Include="Microsoft.Extensions.Azure" />
1616
<PackageReference Include="Microsoft.Extensions.Hosting" />
17-
<PackageReference Include="Azure.AI.OpenAI" />
17+
<PackageReference Include="OpenAI" />
1818
</ItemGroup>
1919
</Project>

docs/azure/sdk/snippets/dependency-injection/HostApplicationBuilder/Program.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using Azure.Messaging.ServiceBus.Administration;
44
using Microsoft.Extensions.Azure;
55
using Microsoft.Extensions.Hosting;
6-
using Azure.AI.OpenAI;
6+
using OpenAI;
7+
using OpenAI.Responses;
8+
using System.ClientModel.Primitives;
79

810
IHost host = Host.CreateDefaultBuilder(args)
911
.ConfigureServices(services =>
@@ -33,10 +35,18 @@
3335
}).WithName(queueName);
3436
}
3537

38+
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
39+
?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is required.");
40+
3641
// Register a custom client factory
37-
clientBuilder.AddClient<AzureOpenAIClient, AzureOpenAIClientOptions>(
38-
(options, credential, _) => new AzureOpenAIClient(
39-
new Uri("<url_here>"), credential, options));
42+
#pragma warning disable OPENAI001 // Type is for evaluation purposes and is subject to change in future updates.
43+
clientBuilder.AddClient<ResponsesClient, OpenAIClientOptions>(
44+
(options, credential, _) => new ResponsesClient(
45+
"<deployment_name>",
46+
new BearerTokenPolicy(credential, "https://ai.azure.com/.default"),
47+
new OpenAIClientOptions { Endpoint = new Uri($"{endpoint}/openai/v1/") }
48+
));
49+
#pragma warning restore OPENAI001
4050
});
4151
}).Build();
4252

docs/azure/sdk/snippets/dependency-injection/HostBuilder/HostBuilder.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="Azure.Identity" />
1211
<PackageReference Include="Azure.Messaging.ServiceBus" />
1312
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
1413
<PackageReference Include="Azure.Storage.Blobs" />
1514
<PackageReference Include="Microsoft.Extensions.Azure" />
1615
<PackageReference Include="Microsoft.Extensions.Hosting" />
17-
<PackageReference Include="Azure.AI.OpenAI" />
16+
<PackageReference Include="Azure.Identity" />
17+
<PackageReference Include="OpenAI" />
1818
</ItemGroup>
1919
</Project>

docs/azure/sdk/snippets/dependency-injection/HostBuilder/Program.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using Azure.Messaging.ServiceBus;
44
using Azure.Messaging.ServiceBus.Administration;
55
using Microsoft.Extensions.Azure;
6-
using Azure.AI.OpenAI;
6+
using OpenAI;
7+
using OpenAI.Responses;
8+
using System.ClientModel.Primitives;
79

810
IHost host = Host.CreateDefaultBuilder(args)
911
.ConfigureServices(services =>
@@ -33,10 +35,18 @@
3335
}).WithName(queueName);
3436
}
3537

38+
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
39+
?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is required.");
40+
3641
// Register a custom client factory
37-
clientBuilder.AddClient<AzureOpenAIClient, AzureOpenAIClientOptions>(
38-
(options, credential, _) => new AzureOpenAIClient(
39-
new Uri("<url_here>"), credential, options));
42+
#pragma warning disable OPENAI001 // Type is for evaluation purposes and is subject to change in future updates.
43+
clientBuilder.AddClient<ResponsesClient, OpenAIClientOptions>(
44+
(options, credential, _) => new ResponsesClient(
45+
"<deployment_name>",
46+
new BearerTokenPolicy(credential, "https://ai.azure.com/.default"),
47+
new OpenAIClientOptions { Endpoint = new Uri($"{endpoint}/openai/v1/") }
48+
));
49+
#pragma warning restore OPENAI001
4050
});
4151
}).Build();
4252

docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/Program.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
using Azure.Messaging.ServiceBus;
44
using Azure.Messaging.ServiceBus.Administration;
55
using Microsoft.Extensions.Azure;
6-
using Azure.AI.OpenAI;
6+
using OpenAI;
7+
using OpenAI.Responses;
8+
using System.ClientModel.Primitives;
79

810
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
911

@@ -32,10 +34,18 @@
3234
}).WithName(queueName);
3335
}
3436

37+
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
38+
?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is required.");
39+
3540
// Register a custom client factory
36-
clientBuilder.AddClient<AzureOpenAIClient, AzureOpenAIClientOptions>(
37-
(options, credential, _) => new AzureOpenAIClient(
38-
new Uri("<url_here>"), credential, options));
41+
#pragma warning disable OPENAI001 // Type is for evaluation purposes and is subject to change in future updates.
42+
clientBuilder.AddClient<ResponsesClient, OpenAIClientOptions>(
43+
(options, credential, _) => new ResponsesClient(
44+
"<deployment_name>",
45+
new BearerTokenPolicy(credential, "https://ai.azure.com/.default"),
46+
new OpenAIClientOptions { Endpoint = new Uri($"{endpoint}/openai/v1/") }
47+
));
48+
#pragma warning restore OPENAI001
3949
});
4050

4151
WebApplication app = builder.Build();

docs/azure/sdk/snippets/dependency-injection/WebApplicationBuilder/WebApplicationBuilder.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Azure.Identity" />
1110
<PackageReference Include="Azure.Security.KeyVault.Secrets" />
1211
<PackageReference Include="Azure.Storage.Blobs" />
1312
<PackageReference Include="Azure.Messaging.ServiceBus" />
1413
<PackageReference Include="Microsoft.Extensions.Azure" />
1514
<PackageReference Include="Microsoft.AspNetCore.OpenApi" />
1615
<PackageReference Include="Swashbuckle.AspNetCore" />
17-
<PackageReference Include="Azure.AI.OpenAI" />
16+
<PackageReference Include="Azure.Identity" />
17+
<PackageReference Include="OpenAI" />
1818
</ItemGroup>
1919

2020
</Project>

0 commit comments

Comments
 (0)