openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
OpenAI.Responses/src/Custom/OpenAIHostBuilderExtensions.cs
40lines · modecode
| 1 | using Microsoft.Extensions.Hosting; |
| 2 | using System; |
| 3 | using System.ClientModel; |
| 4 | using System.ClientModel.Primitives; |
| 5 | using System.Diagnostics.CodeAnalysis; |
| 6 | |
| 7 | namespace OpenAI.Responses; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Extension methods for adding OpenAI clients with configuration and DI support following System.ClientModel pattern. |
| 11 | /// See: https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/core/System.ClientModel/src/docs/ConfigurationAndDependencyInjection.md |
| 12 | /// </summary> |
| 13 | [Experimental("SCME0002")] |
| 14 | public static class OpenAIHostBuilderExtensions |
| 15 | { |
| 16 | public static IClientBuilder AddResponsesClient( |
| 17 | this IHostApplicationBuilder builder, |
| 18 | string sectionName) |
| 19 | { |
| 20 | if (builder is null) |
| 21 | { |
| 22 | throw new ArgumentNullException(nameof(builder)); |
| 23 | } |
| 24 | |
| 25 | return builder.AddClient<ResponsesClient, ResponsesClientSettings>(sectionName); |
| 26 | } |
| 27 | |
| 28 | public static IClientBuilder AddKeyedResponsesClient( |
| 29 | this IHostApplicationBuilder builder, |
| 30 | string serviceKey, |
| 31 | string sectionName) |
| 32 | { |
| 33 | if (builder is null) |
| 34 | { |
| 35 | throw new ArgumentNullException(nameof(builder)); |
| 36 | } |
| 37 | |
| 38 | return builder.AddKeyedClient<ResponsesClient, ResponsesClientSettings>(serviceKey, sectionName); |
| 39 | } |
| 40 | } |
| 41 | |