From ea3355e5eff71921c7b6e819dee3b9ea37feac4c Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Wed, 15 May 2024 22:14:28 +0200 Subject: [PATCH 01/12] Add AzureOpenAIConfig, OpenAIConfig and ConfigurationException --- .../Configuration/AzureOpenAIConfig.cs | 125 ++++++++++++++++++ .../Configuration/OpenAIConfig.cs | 75 +++++++++++ .../Configuration/ConfigurationException.cs | 23 ++++ 3 files changed, 223 insertions(+) create mode 100644 dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs create mode 100644 dotnet/src/Connectors/Connectors.OpenAI/Configuration/OpenAIConfig.cs create mode 100644 dotnet/src/SemanticKernel.Abstractions/Configuration/ConfigurationException.cs diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs new file mode 100644 index 000000000000..9cfbd32cec1b --- /dev/null +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs @@ -0,0 +1,125 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System; +using Azure.Core; + +namespace Microsoft.SemanticKernel; + +public class AzureOpenAIConfig +{ + private TokenCredential? _tokenCredential; + + public enum AuthTypes + { + Unknown = -1, + AzureIdentity, + APIKey, + ManualTokenCredential, + } + + public enum APITypes + { + Unknown = -1, + TextCompletion, + ChatCompletion, + ImageGeneration, + EmbeddingGeneration, + } + + /// + /// OpenAI API type, e.g. text completion, chat completion, image generation, etc. + /// + public APITypes APIType { get; set; } = APITypes.ChatCompletion; + + /// + /// Azure authentication type. + /// + public AuthTypes Auth { get; set; } + + /// + /// Azure OpenAI endpoint URL. + /// + public string Endpoint { get; set; } = string.Empty; + + /// + /// Azure OpenAI deployment name. + /// + public string Deployment { get; set; } = string.Empty; + + /// + /// The max number of tokens supported by model deployed. + /// + public int MaxTokenTotal { get; set; } = 8191; + + /// + /// API key, required if Auth == APIKey. + /// + public string APIKey { get; set; } = string.Empty; + + /// + /// The number of dimensions output embeddings should have. + /// Only supported in "text-embedding-3" and later models developed with + /// MRL, see https://arxiv.org/abs/2205.13147 + /// + public int? EmbeddingDimensions { get; set; } + + /// + /// Set credentials manually from code. + /// + /// Token credentials + public void SetCredential(TokenCredential credential) + { + this.Auth = AuthTypes.ManualTokenCredential; + this._tokenCredential = credential; + } + + /// + /// Fetch the credentials passed manually from code. + /// + public TokenCredential GetTokenCredential() + { + return this._tokenCredential + ?? throw new ConfigurationException($"Azure OpenAI: {nameof(this._tokenCredential)} not defined"); + } + + /// + /// Verify that the current state is valid. + /// + public void Validate() + { + if (this.Auth == AuthTypes.Unknown) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.Auth)} (authentication type) is not defined"); + } + + if (this.Auth == AuthTypes.APIKey && string.IsNullOrWhiteSpace(this.APIKey)) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.APIKey)} is empty"); + } + + if (string.IsNullOrWhiteSpace(this.Endpoint)) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.Endpoint)} is empty"); + } + + if (!this.Endpoint.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.Endpoint)} must start with https://"); + } + + if (string.IsNullOrWhiteSpace(this.Deployment)) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.Deployment)} (deployment name) is empty"); + } + + if (this.MaxTokenTotal < 1) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.MaxTokenTotal)} cannot be less than 1"); + } + + if (this.EmbeddingDimensions is < 1) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.EmbeddingDimensions)} cannot be less than 1"); + } + } +} diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/OpenAIConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/OpenAIConfig.cs new file mode 100644 index 000000000000..27ba37359b48 --- /dev/null +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/OpenAIConfig.cs @@ -0,0 +1,75 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Text.Json.Serialization; + +namespace Microsoft.SemanticKernel; + +/// +/// OpenAI settings. +/// +public class OpenAIConfig +{ + [JsonConverter(typeof(JsonStringEnumConverter))] + public enum TextGenerationTypes + { + Auto = 0, + TextCompletion, + Chat, + } + + /// + /// Model used for text generation. Chat models can be used too. + /// + public string TextModel { get; set; } = string.Empty; + + /// + /// The type of OpenAI completion to use, either Text (legacy) or Chat. + /// When using Auto, the client uses OpenAI model names to detect the correct protocol. + /// Most OpenAI models use Chat. If you're using a non-OpenAI model, you might want to set this manually. + /// + public TextGenerationTypes TextGenerationType { get; set; } = TextGenerationTypes.Auto; + + /// + /// Model used to embedding generation. + /// + public string EmbeddingModel { get; set; } = string.Empty; + + /// + /// OpenAI HTTP endpoint. You may need to override this to work with + /// OpenAI compatible services like LM Studio. + /// + public string Endpoint { get; set; } = "https://api.openai.com/v1"; + + /// + /// OpenAI API key. + /// + public string APIKey { get; set; } = string.Empty; + + /// + /// Optional OpenAI Organization ID. + /// + public string? OrgId { get; set; } = string.Empty; + + /// + /// The number of dimensions output embeddings should have. + /// Only supported in "text-embedding-3" and later models developed with + /// MRL, see https://arxiv.org/abs/2205.13147 + /// + public int? EmbeddingDimensions { get; set; } + + /// + /// Verify that the current state is valid. + /// + public void Validate() + { + if (string.IsNullOrWhiteSpace(this.APIKey)) + { + throw new ConfigurationException($"OpenAI: {nameof(this.APIKey)} is empty"); + } + + if (this.EmbeddingDimensions is < 1) + { + throw new ConfigurationException($"OpenAI: {nameof(this.EmbeddingDimensions)} cannot be less than 1"); + } + } +} diff --git a/dotnet/src/SemanticKernel.Abstractions/Configuration/ConfigurationException.cs b/dotnet/src/SemanticKernel.Abstractions/Configuration/ConfigurationException.cs new file mode 100644 index 000000000000..57b03ac42db8 --- /dev/null +++ b/dotnet/src/SemanticKernel.Abstractions/Configuration/ConfigurationException.cs @@ -0,0 +1,23 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System; + +namespace Microsoft.SemanticKernel; + +public class ConfigurationException : KernelException +{ + /// + public ConfigurationException() + { + } + + /// + public ConfigurationException(string? message) : base(message) + { + } + + /// + public ConfigurationException(string? message, Exception? innerException) : base(message, innerException) + { + } +} From 161f1f668d26beb912381832b209438ff13e4de2 Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Wed, 15 May 2024 22:15:50 +0200 Subject: [PATCH 02/12] Add extension functions which uses AzureOpenAI and OpenAI configuration Minor fixes --- .../Connectors.OpenAI.csproj | 1 + .../OpenAIServiceCollectionExtensions.cs | 1370 +++++++++++++++-- 2 files changed, 1242 insertions(+), 129 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj b/dotnet/src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj index f873d8d9cd29..63f00af1599f 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj +++ b/dotnet/src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj @@ -30,5 +30,6 @@ + diff --git a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs index 1dea76706e20..a4f05fdbed5d 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs @@ -6,6 +6,7 @@ using Azure; using Azure.AI.OpenAI; using Azure.Core; +using Azure.Identity; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel.AudioToText; @@ -36,7 +37,7 @@ public static class OpenAIServiceCollectionExtensions /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The HttpClient to use with this service. /// The same instance as . @@ -70,7 +71,7 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . public static IServiceCollection AddAzureOpenAITextGeneration( @@ -100,7 +101,7 @@ public static IServiceCollection AddAzureOpenAITextGeneration( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The HttpClient to use with this service. /// The same instance as . @@ -134,7 +135,7 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . public static IServiceCollection AddAzureOpenAITextGeneration( @@ -163,7 +164,7 @@ public static IServiceCollection AddAzureOpenAITextGeneration( /// The instance to augment. /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . public static IKernelBuilder AddAzureOpenAITextGeneration( @@ -192,7 +193,7 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( /// The instance to augment. /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . public static IServiceCollection AddAzureOpenAITextGeneration( @@ -213,6 +214,117 @@ public static IServiceCollection AddAzureOpenAITextGeneration( serviceProvider.GetService())); } + /// + /// Adds an Azure OpenAI text generation service with the specified configuration. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + public static IKernelBuilder AddAzureOpenAITextGeneration( + this IKernelBuilder builder, + AzureOpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + builder.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: new DefaultAzureCredential(), + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + builder.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.GetTokenCredential(), + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + builder.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return builder; + } + + /// + /// Adds an Azure OpenAI text generation service with the specified configuration. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + public static IServiceCollection AddAzureOpenAITextGeneration( + this IServiceCollection services, + AzureOpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + services.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: new DefaultAzureCredential(), + serviceId: serviceId, + modelId: config.Deployment); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + services.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.GetTokenCredential(), + serviceId: serviceId, + modelId: config.Deployment); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + services.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return services; + } + /// /// Adds an OpenAI text generation service with the specified configuration. /// @@ -220,7 +332,7 @@ public static IServiceCollection AddAzureOpenAITextGeneration( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . public static IKernelBuilder AddOpenAITextGeneration( @@ -253,7 +365,7 @@ public static IKernelBuilder AddOpenAITextGeneration( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . public static IServiceCollection AddOpenAITextGeneration( this IServiceCollection services, @@ -281,7 +393,7 @@ public static IServiceCollection AddOpenAITextGeneration( /// The instance to augment. /// OpenAI model name, see https://platform.openai.com/docs/models /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . public static IKernelBuilder AddOpenAITextGeneration( this IKernelBuilder builder, @@ -307,9 +419,10 @@ public static IKernelBuilder AddOpenAITextGeneration( /// The instance to augment. /// OpenAI model name, see https://platform.openai.com/docs/models /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . - public static IServiceCollection AddOpenAITextGeneration(this IServiceCollection services, + public static IServiceCollection AddOpenAITextGeneration( + this IServiceCollection services, string modelId, OpenAIClient? openAIClient = null, string? serviceId = null) @@ -324,6 +437,57 @@ public static IServiceCollection AddOpenAITextGeneration(this IServiceCollection serviceProvider.GetService())); } + /// + /// Adds an OpenAI text generation service with the specified configuration. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + public static IKernelBuilder AddOpenAITextGeneration( + this IKernelBuilder builder, + OpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + return builder.AddOpenAITextGeneration( + modelId: config.TextModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId, + httpClient: httpClient); + } + + /// + /// Adds an OpenAI text generation service with the specified configuration. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + public static IServiceCollection AddOpenAITextGeneration( + this IServiceCollection services, + OpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + return services.AddOpenAITextGeneration( + modelId: config.TextModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId); + } + #endregion #region Text Embedding @@ -335,7 +499,7 @@ public static IServiceCollection AddOpenAITextGeneration(this IServiceCollection /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The HttpClient to use with this service. /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. @@ -376,7 +540,7 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. /// The same instance as . @@ -413,7 +577,7 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The HttpClient to use with this service. /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. @@ -454,7 +618,7 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. /// The same instance as . @@ -490,7 +654,7 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( /// The instance to augment. /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. /// The same instance as . @@ -523,7 +687,7 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( /// The instance to augment. /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. /// The same instance as . @@ -548,6 +712,125 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( dimensions)); } + /// + /// Adds an Azure OpenAI text embeddings service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( + this IKernelBuilder builder, + AzureOpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + builder.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credential: new DefaultAzureCredential(), + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient, + dimensions: config.EmbeddingDimensions); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + builder.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credential: config.GetTokenCredential(), + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient, + dimensions: config.EmbeddingDimensions); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + builder.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient, + dimensions: config.EmbeddingDimensions); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return builder; + } + + /// + /// Adds an Azure OpenAI text embeddings service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( + this IServiceCollection services, + AzureOpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + services.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credential: new DefaultAzureCredential(), + serviceId: serviceId, + modelId: config.Deployment, + dimensions: config.EmbeddingDimensions); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + services.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credential: config.GetTokenCredential(), + serviceId: serviceId, + modelId: config.Deployment, + dimensions: config.EmbeddingDimensions); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + services.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment, + dimensions: config.EmbeddingDimensions); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return services; + } + /// /// Adds the OpenAI text embeddings service to the list. /// @@ -555,7 +838,7 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. /// The same instance as . @@ -592,7 +875,7 @@ public static IKernelBuilder AddOpenAITextEmbeddingGeneration( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. /// The same instance as . [Experimental("SKEXP0010")] @@ -624,7 +907,7 @@ public static IServiceCollection AddOpenAITextEmbeddingGeneration( /// The instance to augment. /// OpenAI model name, see https://platform.openai.com/docs/models /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. /// The same instance as . [Experimental("SKEXP0010")] @@ -654,11 +937,12 @@ public static IKernelBuilder AddOpenAITextEmbeddingGeneration( /// The instance to augment. /// The OpenAI model id. /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The number of dimensions the resulting output embeddings should have. Only supported in "text-embedding-3" and later models. /// The same instance as . [Experimental("SKEXP0010")] - public static IServiceCollection AddOpenAITextEmbeddingGeneration(this IServiceCollection services, + public static IServiceCollection AddOpenAITextEmbeddingGeneration( + this IServiceCollection services, string modelId, OpenAIClient? openAIClient = null, string? serviceId = null, @@ -675,6 +959,61 @@ public static IServiceCollection AddOpenAITextEmbeddingGeneration(this IServiceC dimensions)); } + /// + /// Adds the OpenAI text embeddings service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddOpenAITextEmbeddingGeneration( + this IKernelBuilder builder, + OpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + return builder.AddOpenAITextEmbeddingGeneration( + modelId: config.EmbeddingModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId, + httpClient: httpClient, + dimensions: config.EmbeddingDimensions); + } + + /// + /// Adds the OpenAI text embeddings service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddOpenAITextEmbeddingGeneration( + this IServiceCollection services, + OpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + return services.AddOpenAITextEmbeddingGeneration( + modelId: config.EmbeddingModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId, + dimensions: config.EmbeddingDimensions); + } + #endregion #region Chat Completion @@ -686,7 +1025,7 @@ public static IServiceCollection AddOpenAITextEmbeddingGeneration(this IServiceC /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The HttpClient to use with this service. /// The same instance as . @@ -727,7 +1066,7 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . public static IServiceCollection AddAzureOpenAIChatCompletion( @@ -766,7 +1105,7 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The HttpClient to use with this service. /// The same instance as . @@ -807,7 +1146,7 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . public static IServiceCollection AddAzureOpenAIChatCompletion( @@ -845,7 +1184,7 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( /// The instance to augment. /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . public static IKernelBuilder AddAzureOpenAIChatCompletion( @@ -873,7 +1212,7 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( /// The instance to augment. /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . public static IServiceCollection AddAzureOpenAIChatCompletion( @@ -955,6 +1294,120 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( return services; } + /// + /// Adds the Azure OpenAI chat completion with data service to the list. + /// + /// The instance. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + /// + /// More information: + /// + [Experimental("SKEXP0010")] + public static IKernelBuilder AddAzureOpenAIChatCompletion( + this IKernelBuilder builder, + AzureOpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + builder.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: new DefaultAzureCredential(), + serviceId: serviceId, + modelId: config.Deployment); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + builder.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.GetTokenCredential(), + serviceId: serviceId, + modelId: config.Deployment); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + builder.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return builder; + } + + /// + /// Adds the Azure OpenAI chat completion with data service to the list. + /// + /// The instance. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + /// + /// More information: + /// + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAIChatCompletion( + this IServiceCollection services, + AzureOpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + services.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: new DefaultAzureCredential(), + serviceId: serviceId, + modelId: config.Deployment); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + services.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.GetTokenCredential(), + serviceId: serviceId, + modelId: config.Deployment); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + services.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return services; + } + /// /// Adds the OpenAI chat completion service to the list. /// @@ -962,7 +1415,7 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . public static IKernelBuilder AddOpenAIChatCompletion( @@ -997,7 +1450,7 @@ public static IKernelBuilder AddOpenAIChatCompletion( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . public static IServiceCollection AddOpenAIChatCompletion( this IServiceCollection services, @@ -1027,9 +1480,9 @@ public static IServiceCollection AddOpenAIChatCompletion( /// Adds the OpenAI chat completion service to the list. /// /// The instance to augment. - /// OpenAI model id + /// OpenAI model id. /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . public static IKernelBuilder AddOpenAIChatCompletion( this IKernelBuilder builder, @@ -1053,11 +1506,12 @@ public static IKernelBuilder AddOpenAIChatCompletion( /// Adds the OpenAI chat completion service to the list. /// /// The instance to augment. - /// OpenAI model id + /// OpenAI model id. /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . - public static IServiceCollection AddOpenAIChatCompletion(this IServiceCollection services, + public static IServiceCollection AddOpenAIChatCompletion( + this IServiceCollection services, string modelId, OpenAIClient? openAIClient = null, string? serviceId = null) @@ -1082,7 +1536,7 @@ public static IServiceCollection AddOpenAIChatCompletion(this IServiceCollection /// A Custom Message API compatible endpoint. /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddOpenAIChatCompletion( @@ -1115,10 +1569,10 @@ public static IServiceCollection AddOpenAIChatCompletion( /// /// The instance to augment. /// OpenAI model name, see https://platform.openai.com/docs/models - /// Custom OpenAI Compatible Message API endpoint + /// Custom OpenAI Compatible Message API endpoint. /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] @@ -1148,36 +1602,89 @@ public static IKernelBuilder AddOpenAIChatCompletion( return builder; } - #endregion - - #region Images - /// - /// Add the Azure OpenAI Dall-E text to image service to the list + /// Adds the Custom OpenAI chat completion service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment name - /// Azure OpenAI deployment URL - /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// Model identifier - /// A local identifier for the given AI service - /// Azure OpenAI API version + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] - public static IServiceCollection AddAzureOpenAITextToImage( + public static IServiceCollection AddOpenAIChatCompletion( this IServiceCollection services, - string deploymentName, - string endpoint, - TokenCredential credentials, - string? modelId = null, - string? serviceId = null, - string? apiVersion = null) + OpenAIConfig config, + string? serviceId = null) { Verify.NotNull(services); - Verify.NotNullOrWhiteSpace(endpoint); - Verify.NotNull(credentials); + Verify.NotNull(config); - return services.AddKeyedSingleton(serviceId, (serviceProvider, _) => + config.Validate(); + + return services.AddOpenAIChatCompletion( + modelId: config.TextModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId); + } + + /// + /// Adds the Custom Endpoint OpenAI chat completion service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddOpenAIChatCompletion( + this IKernelBuilder builder, + OpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + return builder.AddOpenAIChatCompletion( + modelId: config.TextModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId, + httpClient: httpClient); + } + + #endregion + + #region Images + + /// + /// Add the Azure OpenAI Dall-E text to image service to the list. + /// + /// The instance to augment. + /// Azure OpenAI deployment name. + /// Azure OpenAI deployment URL. + /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. + /// Model identifier. + /// A local identifier for the given AI service. + /// Azure OpenAI API version. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAITextToImage( + this IServiceCollection services, + string deploymentName, + string endpoint, + TokenCredential credentials, + string? modelId = null, + string? serviceId = null, + string? apiVersion = null) + { + Verify.NotNull(services); + Verify.NotNullOrWhiteSpace(endpoint); + Verify.NotNull(credentials); + + return services.AddKeyedSingleton(serviceId, (serviceProvider, _) => new AzureOpenAITextToImageService( deploymentName, endpoint, @@ -1189,15 +1696,15 @@ public static IServiceCollection AddAzureOpenAITextToImage( } /// - /// Add the Azure OpenAI Dall-E text to image service to the list + /// Add the Azure OpenAI Dall-E text to image service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment name - /// Azure OpenAI deployment URL + /// Azure OpenAI deployment name. + /// Azure OpenAI deployment URL. /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// Model identifier - /// A local identifier for the given AI service - /// Azure OpenAI API version + /// Model identifier. + /// A local identifier for the given AI service. + /// Azure OpenAI API version. /// The same instance as . [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextToImage( @@ -1227,15 +1734,15 @@ public static IKernelBuilder AddAzureOpenAITextToImage( } /// - /// Add the Azure OpenAI Dall-E text to image service to the list + /// Add the Azure OpenAI Dall-E text to image service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment name - /// Azure OpenAI deployment URL - /// Azure OpenAI API key - /// Model identifier - /// A local identifier for the given AI service - /// Azure OpenAI API version + /// Azure OpenAI deployment name. + /// Azure OpenAI deployment URL. + /// Azure OpenAI API key. + /// Model identifier. + /// A local identifier for the given AI service. + /// Azure OpenAI API version. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] @@ -1267,15 +1774,15 @@ public static IKernelBuilder AddAzureOpenAITextToImage( } /// - /// Add the Azure OpenAI Dall-E text to image service to the list + /// Add the Azure OpenAI Dall-E text to image service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment name - /// Azure OpenAI deployment URL - /// Azure OpenAI API key - /// A local identifier for the given AI service - /// Model identifier - /// Maximum number of attempts to retrieve the text to image operation result. + /// Azure OpenAI deployment name. + /// Azure OpenAI deployment URL. + /// Azure OpenAI API key. + /// A local identifier for the given AI service. + /// Model identifier. + /// Azure OpenAI API version. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextToImage( @@ -1285,7 +1792,7 @@ public static IServiceCollection AddAzureOpenAITextToImage( string apiKey, string? serviceId = null, string? modelId = null, - int maxRetryCount = 5) + string? apiVersion = null) { Verify.NotNull(services); Verify.NotNullOrWhiteSpace(endpoint); @@ -1298,16 +1805,17 @@ public static IServiceCollection AddAzureOpenAITextToImage( apiKey, modelId, HttpClientProvider.GetHttpClient(serviceProvider), - serviceProvider.GetService())); + serviceProvider.GetService(), + apiVersion)); } /// - /// Add the OpenAI Dall-E text to image service to the list + /// Add the OpenAI Dall-E text to image service to the list. /// /// The instance to augment. /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] @@ -1332,15 +1840,16 @@ public static IKernelBuilder AddOpenAITextToImage( } /// - /// Add the OpenAI Dall-E text to image service to the list + /// Add the OpenAI Dall-E text to image service to the list. /// /// The instance to augment. /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] - public static IServiceCollection AddOpenAITextToImage(this IServiceCollection services, + public static IServiceCollection AddOpenAITextToImage( + this IServiceCollection services, string apiKey, string? orgId = null, string? serviceId = null) @@ -1357,13 +1866,64 @@ public static IServiceCollection AddOpenAITextToImage(this IServiceCollection se } /// - /// Add the OpenAI Dall-E text to image service to the list + /// Add the OpenAI Dall-E text to image service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddOpenAITextToImage( + this IKernelBuilder builder, + OpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + return builder.AddOpenAITextToImage( + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId, + httpClient: httpClient); + } + + /// + /// Add the OpenAI Dall-E text to image service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment name + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddOpenAITextToImage( + this IServiceCollection services, + OpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + return services.AddOpenAITextToImage( + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId); + } + + /// + /// Add the OpenAI Dall-E text to image service to the list. + /// + /// The instance to augment. + /// Azure OpenAI deployment name. /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// Model identifier - /// A local identifier for the given AI service + /// Model identifier. + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextToImage( @@ -1385,13 +1945,13 @@ public static IServiceCollection AddAzureOpenAITextToImage( } /// - /// Add the OpenAI Dall-E text to image service to the list + /// Add the OpenAI Dall-E text to image service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment name + /// Azure OpenAI deployment name. /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// Model identifier - /// A local identifier for the given AI service + /// Model identifier. + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextToImage( @@ -1414,17 +1974,138 @@ public static IKernelBuilder AddAzureOpenAITextToImage( return builder; } + /// + /// Add the OpenAI Dall-E text to image service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// Azure OpenAI API version. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAITextToImage( + this IServiceCollection services, + AzureOpenAIConfig config, + string? serviceId = null, + string? apiVersion = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + services.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: new DefaultAzureCredential(), + modelId: config.Deployment, + serviceId: serviceId, + apiVersion: apiVersion); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + services.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.GetTokenCredential(), + modelId: config.Deployment, + serviceId: serviceId, + apiVersion: apiVersion); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + services.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment, + apiVersion: apiVersion); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return services; + } + + /// + /// Add the OpenAI Dall-E text to image service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// Azure OpenAI API version. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddAzureOpenAITextToImage( + this IKernelBuilder builder, + AzureOpenAIConfig config, + string? serviceId = null, + string? apiVersion = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + builder.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: new DefaultAzureCredential(), + modelId: config.Deployment, + serviceId: serviceId, + apiVersion: apiVersion); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + builder.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.GetTokenCredential(), + modelId: config.Deployment, + serviceId: serviceId, + apiVersion: apiVersion); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + builder.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + modelId: config.Deployment, + serviceId: serviceId, + apiVersion: apiVersion, + httpClient: httpClient); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return builder; + } + #endregion #region Files /// - /// Add the OpenAI file service to the list + /// Add the OpenAI file service to the list. /// /// The instance to augment. /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] @@ -1449,12 +2130,12 @@ public static IKernelBuilder AddOpenAIFiles( } /// - /// Add the OpenAI file service to the list + /// Add the OpenAI file service to the list. /// /// The instance to augment. /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddOpenAIFiles( @@ -1477,14 +2158,65 @@ public static IServiceCollection AddOpenAIFiles( } /// - /// Add the OpenAI file service to the list + /// Add the OpenAI file service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddOpenAIFiles( + this IKernelBuilder builder, + OpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + return builder.AddOpenAIFiles( + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId, + httpClient: httpClient); + } + + /// + /// Add the OpenAI file service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddOpenAIFiles( + this IServiceCollection services, + OpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + return services.AddOpenAIFiles( + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId); + } + + /// + /// Add the OpenAI file service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment URL + /// Azure OpenAI deployment URL. /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. /// The API version to target. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] @@ -1513,14 +2245,14 @@ public static IKernelBuilder AddAzureOpenAIFiles( } /// - /// Add the OpenAI file service to the list + /// Add the OpenAI file service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment URL + /// Azure OpenAI deployment URL. /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. /// The API version to target. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAIFiles( @@ -1546,6 +2278,89 @@ public static IServiceCollection AddAzureOpenAIFiles( return services; } + /// + /// Add the OpenAI file service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. + /// The API version to target. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddAzureOpenAIFiles( + this IKernelBuilder builder, + AzureOpenAIConfig config, + string? orgId = null, + string? version = null, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.APIKey: + builder.AddAzureOpenAIFiles( + endpoint: config.Endpoint, + apiKey: config.APIKey, + orgId: orgId, + version: version, + serviceId: serviceId, + httpClient: httpClient); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return builder; + } + + /// + /// Add the OpenAI file service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. + /// The API version to target. + /// A local identifier for the given AI service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAIFiles( + this IServiceCollection services, + AzureOpenAIConfig config, + string? orgId = null, + string? version = null, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.APIKey: + services.AddAzureOpenAIFiles( + endpoint: config.Endpoint, + apiKey: config.APIKey, + orgId: orgId, + version: version, + serviceId: serviceId); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return services; + } + #endregion #region Text-to-Audio @@ -1554,11 +2369,11 @@ public static IServiceCollection AddAzureOpenAIFiles( /// Adds the Azure OpenAI text-to-audio service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment name - /// Azure OpenAI deployment URL - /// Azure OpenAI API key - /// A local identifier for the given AI service - /// Model identifier + /// Azure OpenAI deployment name. + /// Azure OpenAI deployment URL. + /// Azure OpenAI API key. + /// A local identifier for the given AI service. + /// Model identifier. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0001")] @@ -1592,11 +2407,11 @@ public static IKernelBuilder AddAzureOpenAITextToAudio( /// Adds the Azure OpenAI text-to-audio service to the list. /// /// The instance to augment. - /// Azure OpenAI deployment name - /// Azure OpenAI deployment URL - /// Azure OpenAI API key - /// A local identifier for the given AI service - /// Model identifier + /// Azure OpenAI deployment name. + /// Azure OpenAI deployment URL. + /// Azure OpenAI API key. + /// A local identifier for the given AI service. + /// Model identifier. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0001")] @@ -1620,10 +2435,88 @@ public static IServiceCollection AddAzureOpenAITextToAudio( endpoint, apiKey, modelId, - HttpClientProvider.GetHttpClient(serviceProvider), + HttpClientProvider.GetHttpClient(httpClient, serviceProvider), serviceProvider.GetService())); } + /// + /// Adds the Azure OpenAI text-to-audio service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0001")] + public static IKernelBuilder AddAzureOpenAITextToAudio( + this IKernelBuilder builder, + AzureOpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.APIKey: + builder.AddAzureOpenAITextToAudio( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return builder; + } + + /// + /// Adds the Azure OpenAI text-to-audio service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0001")] + public static IServiceCollection AddAzureOpenAITextToAudio( + this IServiceCollection services, + AzureOpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.APIKey: + services.AddAzureOpenAITextToAudio( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return services; + } + /// /// Adds the OpenAI text-to-audio service to the list. /// @@ -1631,7 +2524,7 @@ public static IServiceCollection AddAzureOpenAITextToAudio( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0001")] @@ -1665,7 +2558,7 @@ public static IKernelBuilder AddOpenAITextToAudio( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0001")] public static IServiceCollection AddOpenAITextToAudio( @@ -1688,6 +2581,59 @@ public static IServiceCollection AddOpenAITextToAudio( serviceProvider.GetService())); } + /// + /// Adds the OpenAI text-to-audio service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0001")] + public static IKernelBuilder AddOpenAITextToAudio( + this IKernelBuilder builder, + OpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + return builder.AddOpenAITextToAudio( + modelId: config.TextModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId, + httpClient: httpClient); + } + + /// + /// Adds the OpenAI text-to-audio service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + [Experimental("SKEXP0001")] + public static IServiceCollection AddOpenAITextToAudio( + this IServiceCollection services, + OpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + return services.AddOpenAITextToAudio( + modelId: config.TextModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId); + } + #endregion #region Audio-to-Text @@ -1699,7 +2645,7 @@ public static IServiceCollection AddOpenAITextToAudio( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The HttpClient to use with this service. /// The same instance as . @@ -1739,7 +2685,7 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Azure OpenAI API key, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . [Experimental("SKEXP0001")] @@ -1777,7 +2723,7 @@ public static IServiceCollection AddAzureOpenAIAudioToText( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The HttpClient to use with this service. /// The same instance as . @@ -1817,7 +2763,7 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// Azure OpenAI deployment URL, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// Token credentials, e.g. DefaultAzureCredential, ManagedIdentityCredential, EnvironmentCredential, etc. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . [Experimental("SKEXP0001")] @@ -1854,7 +2800,7 @@ public static IServiceCollection AddAzureOpenAIAudioToText( /// The instance to augment. /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . [Experimental("SKEXP0001")] @@ -1882,7 +2828,7 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( /// The instance to augment. /// Azure OpenAI deployment name, see https://learn.microsoft.com/azure/cognitive-services/openai/how-to/create-resource /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// Model identifier, see https://learn.microsoft.com/azure/cognitive-services/openai/quickstart /// The same instance as . [Experimental("SKEXP0001")] @@ -1904,6 +2850,119 @@ public static IServiceCollection AddAzureOpenAIAudioToText( return services; } + /// + /// Adds the Azure OpenAI audio-to-text service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0001")] + public static IKernelBuilder AddAzureOpenAIAudioToText( + this IKernelBuilder builder, + AzureOpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + builder.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: new DefaultAzureCredential(), + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + builder.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.GetTokenCredential(), + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + builder.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment, + httpClient: httpClient); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return builder; + } + + /// + /// Adds the Azure OpenAI audio-to-text service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + [Experimental("SKEXP0001")] + public static IServiceCollection AddAzureOpenAIAudioToText( + this IServiceCollection services, + AzureOpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + switch (config.Auth) + { + case AzureOpenAIConfig.AuthTypes.AzureIdentity: + services.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: new DefaultAzureCredential(), + serviceId: serviceId, + modelId: config.Deployment); + break; + + case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: + services.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.GetTokenCredential(), + serviceId: serviceId, + modelId: config.Deployment); + break; + + case AzureOpenAIConfig.AuthTypes.APIKey: + services.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.APIKey, + serviceId: serviceId, + modelId: config.Deployment); + break; + + default: + throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); + } + + return services; + } + /// /// Adds the OpenAI audio-to-text service to the list. /// @@ -1911,7 +2970,7 @@ public static IServiceCollection AddAzureOpenAIAudioToText( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0001")] @@ -1946,7 +3005,7 @@ public static IKernelBuilder AddOpenAIAudioToText( /// OpenAI model name, see https://platform.openai.com/docs/models /// OpenAI API key, see https://platform.openai.com/account/api-keys /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0001")] public static IServiceCollection AddOpenAIAudioToText( @@ -1976,9 +3035,9 @@ public static IServiceCollection AddOpenAIAudioToText( /// Adds the OpenAI audio-to-text service to the list. /// /// The instance to augment. - /// OpenAI model id + /// OpenAI model id. /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0001")] public static IKernelBuilder AddOpenAIAudioToText( @@ -2002,9 +3061,9 @@ public static IKernelBuilder AddOpenAIAudioToText( /// Adds the OpenAI audio-to-text service to the list. /// /// The instance to augment. - /// OpenAI model id + /// OpenAI model id. /// to use for the service. If null, one must be available in the service provider when this service is resolved. - /// A local identifier for the given AI service + /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0001")] public static IServiceCollection AddOpenAIAudioToText( @@ -2024,6 +3083,59 @@ public static IServiceCollection AddOpenAIAudioToText( return services; } + /// + /// Adds the OpenAI audio-to-text service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0001")] + public static IKernelBuilder AddOpenAIAudioToText( + this IKernelBuilder builder, + OpenAIConfig config, + string? serviceId = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + return builder.AddOpenAIAudioToText( + modelId: config.TextModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId, + httpClient: httpClient); + } + + /// + /// Adds the OpenAI audio-to-text service to the list. + /// + /// The instance to augment. + /// Required configuration for OpenAI. + /// A local identifier for the given AI service. + /// The same instance as . + [Experimental("SKEXP0001")] + public static IServiceCollection AddOpenAIAudioToText( + this IServiceCollection services, + OpenAIConfig config, + string? serviceId = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + return services.AddOpenAIAudioToText( + modelId: config.TextModel, + apiKey: config.APIKey, + orgId: config.OrgId, + serviceId: serviceId); + } + #endregion private static OpenAIClient CreateAzureOpenAIClient(string endpoint, AzureKeyCredential credentials, HttpClient? httpClient) => From ea725e5f41376880e9690fe1080d7b4bbece84b9 Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Thu, 16 May 2024 08:02:12 +0200 Subject: [PATCH 03/12] Add KernelConfig --- .../Configuration/KernelConfig.cs | 31 +++++++++++++++++++ .../SemanticKernel.Core.csproj | 2 ++ 2 files changed, 33 insertions(+) create mode 100644 dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs diff --git a/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs b/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs new file mode 100644 index 000000000000..7db21356c828 --- /dev/null +++ b/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs @@ -0,0 +1,31 @@ +// Copyright (c) Microsoft. All rights reserved. + +using System.Collections.Generic; +using Microsoft.Extensions.Configuration; + +namespace Microsoft.SemanticKernel; + +public class KernelConfig +{ + /// + /// Dependencies settings, e.g. credentials, endpoints, etc. + /// + public Dictionary> Services { get; set; } = new(); + + /// + /// Fetch a service configuration from the "Services" node + /// + /// Configuration instance + /// Service name + /// Root node name of the Kernel Memory config + /// Type of configuration to retrieve + /// Instance of T configuration class + public T GetServiceConfig(IConfiguration cfg, string serviceName, string root = "Kernel") + { + return cfg + .GetSection(root) + .GetSection("Services") + .GetSection(serviceName) + .Get() ?? throw new ConfigurationException($"The {serviceName} configuration is NULL"); + } +} diff --git a/dotnet/src/SemanticKernel.Core/SemanticKernel.Core.csproj b/dotnet/src/SemanticKernel.Core/SemanticKernel.Core.csproj index 7eeee98743d5..b8ba0247e9e0 100644 --- a/dotnet/src/SemanticKernel.Core/SemanticKernel.Core.csproj +++ b/dotnet/src/SemanticKernel.Core/SemanticKernel.Core.csproj @@ -29,6 +29,8 @@ + + From 8a3fba97393e190aba6b89f15dbb43f0a3d48abc Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Thu, 16 May 2024 09:25:10 +0200 Subject: [PATCH 04/12] Add missed HttpClient reference --- .../OpenAIServiceCollectionExtensions.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs index a4f05fdbed5d..23c45884f023 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs @@ -1300,6 +1300,7 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( /// The instance. /// Required configuration for Azure OpenAI. /// A local identifier for the given AI service. + /// The HttpClient to use with this service. /// The same instance as . /// /// More information: @@ -1308,7 +1309,8 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( public static IKernelBuilder AddAzureOpenAIChatCompletion( this IKernelBuilder builder, AzureOpenAIConfig config, - string? serviceId = null) + string? serviceId = null, + HttpClient? httpClient = null) { Verify.NotNull(builder); Verify.NotNull(config); @@ -1323,7 +1325,8 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( endpoint: config.Endpoint, credentials: new DefaultAzureCredential(), serviceId: serviceId, - modelId: config.Deployment); + modelId: config.Deployment, + httpClient: httpClient); break; case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: @@ -1332,7 +1335,8 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( endpoint: config.Endpoint, credentials: config.GetTokenCredential(), serviceId: serviceId, - modelId: config.Deployment); + modelId: config.Deployment, + httpClient: httpClient); break; case AzureOpenAIConfig.AuthTypes.APIKey: @@ -1341,7 +1345,8 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( endpoint: config.Endpoint, apiKey: config.APIKey, serviceId: serviceId, - modelId: config.Deployment); + modelId: config.Deployment, + httpClient: httpClient); break; default: From 6b87674df8eda1b17bbbb3d72c122535546d1aee Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Thu, 16 May 2024 11:45:08 +0200 Subject: [PATCH 05/12] Extend KernelConfig --- .../Configuration/KernelConfig.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs b/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs index 7db21356c828..c7cb874b586c 100644 --- a/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs +++ b/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs @@ -7,6 +7,20 @@ namespace Microsoft.SemanticKernel; public class KernelConfig { + public string TextGenerationType { get; set; } = string.Empty; + + public string EmbeddingGeneratorType { get; set; } = string.Empty; + + public string ChatCompletionType { get; set; } = string.Empty; + + public string TextToImageType { get; set; } = string.Empty; + + public string FilesType { get; set; } = string.Empty; + + public string TextToAudioType { get; set; } = string.Empty; + + public string AudioToTextType { get; set; } = string.Empty; + /// /// Dependencies settings, e.g. credentials, endpoints, etc. /// From 1a06eae76c26e2a3d48ffb2bab3312465a1ca566 Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Thu, 16 May 2024 11:56:24 +0200 Subject: [PATCH 06/12] Remove unnecessary property from AzureOpenAIConfig --- .../Configuration/AzureOpenAIConfig.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs index 9cfbd32cec1b..53aadfd8be12 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs @@ -46,11 +46,6 @@ public enum APITypes /// public string Deployment { get; set; } = string.Empty; - /// - /// The max number of tokens supported by model deployed. - /// - public int MaxTokenTotal { get; set; } = 8191; - /// /// API key, required if Auth == APIKey. /// @@ -112,11 +107,6 @@ public void Validate() throw new ConfigurationException($"Azure OpenAI: {nameof(this.Deployment)} (deployment name) is empty"); } - if (this.MaxTokenTotal < 1) - { - throw new ConfigurationException($"Azure OpenAI: {nameof(this.MaxTokenTotal)} cannot be less than 1"); - } - if (this.EmbeddingDimensions is < 1) { throw new ConfigurationException($"Azure OpenAI: {nameof(this.EmbeddingDimensions)} cannot be less than 1"); From 06dece383b71f8cd8f236fb9626992660fabff00 Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Fri, 17 May 2024 11:07:43 +0200 Subject: [PATCH 07/12] Add missing Experimental attribute --- .../OpenAIServiceCollectionExtensions.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs index 23c45884f023..2ed6e8956cf6 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs @@ -222,6 +222,7 @@ public static IServiceCollection AddAzureOpenAITextGeneration( /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . + [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextGeneration( this IKernelBuilder builder, AzureOpenAIConfig config, @@ -279,6 +280,7 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( /// Required configuration for Azure OpenAI. /// A local identifier for the given AI service. /// The same instance as . + [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextGeneration( this IServiceCollection services, AzureOpenAIConfig config, @@ -445,6 +447,7 @@ public static IServiceCollection AddOpenAITextGeneration( /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . + [Experimental("SKEXP0010")] public static IKernelBuilder AddOpenAITextGeneration( this IKernelBuilder builder, OpenAIConfig config, @@ -471,6 +474,7 @@ public static IKernelBuilder AddOpenAITextGeneration( /// Required configuration for OpenAI. /// A local identifier for the given AI service. /// The same instance as . + [Experimental("SKEXP0010")] public static IServiceCollection AddOpenAITextGeneration( this IServiceCollection services, OpenAIConfig config, @@ -2452,7 +2456,7 @@ public static IServiceCollection AddAzureOpenAITextToAudio( /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . - [Experimental("SKEXP0001")] + [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextToAudio( this IKernelBuilder builder, AzureOpenAIConfig config, @@ -2491,7 +2495,7 @@ public static IKernelBuilder AddAzureOpenAITextToAudio( /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . - [Experimental("SKEXP0001")] + [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextToAudio( this IServiceCollection services, AzureOpenAIConfig config, @@ -2594,7 +2598,7 @@ public static IServiceCollection AddOpenAITextToAudio( /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . - [Experimental("SKEXP0001")] + [Experimental("SKEXP0010")] public static IKernelBuilder AddOpenAITextToAudio( this IKernelBuilder builder, OpenAIConfig config, @@ -2621,7 +2625,7 @@ public static IKernelBuilder AddOpenAITextToAudio( /// Required configuration for OpenAI. /// A local identifier for the given AI service. /// The same instance as . - [Experimental("SKEXP0001")] + [Experimental("SKEXP0010")] public static IServiceCollection AddOpenAITextToAudio( this IServiceCollection services, OpenAIConfig config, @@ -2863,7 +2867,7 @@ public static IServiceCollection AddAzureOpenAIAudioToText( /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . - [Experimental("SKEXP0001")] + [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAIAudioToText( this IKernelBuilder builder, AzureOpenAIConfig config, @@ -2921,7 +2925,7 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( /// Required configuration for Azure OpenAI. /// A local identifier for the given AI service. /// The same instance as . - [Experimental("SKEXP0001")] + [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAIAudioToText( this IServiceCollection services, AzureOpenAIConfig config, @@ -3096,7 +3100,7 @@ public static IServiceCollection AddOpenAIAudioToText( /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . - [Experimental("SKEXP0001")] + [Experimental("SKEXP0010")] public static IKernelBuilder AddOpenAIAudioToText( this IKernelBuilder builder, OpenAIConfig config, @@ -3123,7 +3127,7 @@ public static IKernelBuilder AddOpenAIAudioToText( /// Required configuration for OpenAI. /// A local identifier for the given AI service. /// The same instance as . - [Experimental("SKEXP0001")] + [Experimental("SKEXP0010")] public static IServiceCollection AddOpenAIAudioToText( this IServiceCollection services, OpenAIConfig config, From 650071f98208c3be85bab6be0422dd5a23301ac4 Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Tue, 21 May 2024 08:30:43 +0200 Subject: [PATCH 08/12] Add ServiceId to AzureOpenAIConfig, adjust references --- .../Configuration/AzureOpenAIConfig.cs | 5 + .../OpenAIServiceCollectionExtensions.cs | 106 +++++++----------- 2 files changed, 44 insertions(+), 67 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs index 53aadfd8be12..f971d921cfef 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs @@ -58,6 +58,11 @@ public enum APITypes /// public int? EmbeddingDimensions { get; set; } + /// + /// A local identifier for the given AI service. + /// + public string ServiceId { get; set; } = string.Empty; + /// /// Set credentials manually from code. /// diff --git a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs index 2ed6e8956cf6..6db3fcbfe01c 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs @@ -219,14 +219,12 @@ public static IServiceCollection AddAzureOpenAITextGeneration( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextGeneration( this IKernelBuilder builder, AzureOpenAIConfig config, - string? serviceId = null, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -241,7 +239,7 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: new DefaultAzureCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -251,7 +249,7 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: config.GetTokenCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -261,7 +259,7 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -278,13 +276,11 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextGeneration( this IServiceCollection services, - AzureOpenAIConfig config, - string? serviceId = null) + AzureOpenAIConfig config) { Verify.NotNull(services); Verify.NotNull(config); @@ -298,7 +294,7 @@ public static IServiceCollection AddAzureOpenAITextGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: new DefaultAzureCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment); break; @@ -307,7 +303,7 @@ public static IServiceCollection AddAzureOpenAITextGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: config.GetTokenCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment); break; @@ -316,7 +312,7 @@ public static IServiceCollection AddAzureOpenAITextGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment); break; @@ -721,14 +717,12 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( this IKernelBuilder builder, AzureOpenAIConfig config, - string? serviceId = null, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -743,7 +737,7 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, credential: new DefaultAzureCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient, dimensions: config.EmbeddingDimensions); @@ -754,7 +748,7 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, credential: config.GetTokenCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient, dimensions: config.EmbeddingDimensions); @@ -765,7 +759,7 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient, dimensions: config.EmbeddingDimensions); @@ -783,13 +777,11 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( this IServiceCollection services, - AzureOpenAIConfig config, - string? serviceId = null) + AzureOpenAIConfig config) { Verify.NotNull(services); Verify.NotNull(config); @@ -803,7 +795,7 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, credential: new DefaultAzureCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, dimensions: config.EmbeddingDimensions); break; @@ -813,7 +805,7 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, credential: config.GetTokenCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, dimensions: config.EmbeddingDimensions); break; @@ -823,7 +815,7 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, dimensions: config.EmbeddingDimensions); break; @@ -1303,7 +1295,6 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( /// /// The instance. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . /// @@ -1313,7 +1304,6 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( public static IKernelBuilder AddAzureOpenAIChatCompletion( this IKernelBuilder builder, AzureOpenAIConfig config, - string? serviceId = null, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -1328,7 +1318,7 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: new DefaultAzureCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -1338,7 +1328,7 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: config.GetTokenCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -1348,7 +1338,7 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -1365,7 +1355,6 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( /// /// The instance. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The same instance as . /// /// More information: @@ -1373,8 +1362,7 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAIChatCompletion( this IServiceCollection services, - AzureOpenAIConfig config, - string? serviceId = null) + AzureOpenAIConfig config) { Verify.NotNull(services); Verify.NotNull(config); @@ -1388,7 +1376,7 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: new DefaultAzureCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment); break; @@ -1397,7 +1385,7 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: config.GetTokenCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment); break; @@ -1406,7 +1394,7 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment); break; @@ -1988,14 +1976,12 @@ public static IKernelBuilder AddAzureOpenAITextToImage( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// Azure OpenAI API version. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextToImage( this IServiceCollection services, AzureOpenAIConfig config, - string? serviceId = null, string? apiVersion = null) { Verify.NotNull(services); @@ -2011,7 +1997,7 @@ public static IServiceCollection AddAzureOpenAITextToImage( endpoint: config.Endpoint, credentials: new DefaultAzureCredential(), modelId: config.Deployment, - serviceId: serviceId, + serviceId: config.ServiceId, apiVersion: apiVersion); break; @@ -2021,7 +2007,7 @@ public static IServiceCollection AddAzureOpenAITextToImage( endpoint: config.Endpoint, credentials: config.GetTokenCredential(), modelId: config.Deployment, - serviceId: serviceId, + serviceId: config.ServiceId, apiVersion: apiVersion); break; @@ -2030,7 +2016,7 @@ public static IServiceCollection AddAzureOpenAITextToImage( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, apiVersion: apiVersion); break; @@ -2047,7 +2033,6 @@ public static IServiceCollection AddAzureOpenAITextToImage( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// Azure OpenAI API version. /// The HttpClient to use with this service. /// The same instance as . @@ -2055,7 +2040,6 @@ public static IServiceCollection AddAzureOpenAITextToImage( public static IKernelBuilder AddAzureOpenAITextToImage( this IKernelBuilder builder, AzureOpenAIConfig config, - string? serviceId = null, string? apiVersion = null, HttpClient? httpClient = null) { @@ -2072,7 +2056,7 @@ public static IKernelBuilder AddAzureOpenAITextToImage( endpoint: config.Endpoint, credentials: new DefaultAzureCredential(), modelId: config.Deployment, - serviceId: serviceId, + serviceId: config.ServiceId, apiVersion: apiVersion); break; @@ -2082,7 +2066,7 @@ public static IKernelBuilder AddAzureOpenAITextToImage( endpoint: config.Endpoint, credentials: config.GetTokenCredential(), modelId: config.Deployment, - serviceId: serviceId, + serviceId: config.ServiceId, apiVersion: apiVersion); break; @@ -2092,7 +2076,7 @@ public static IKernelBuilder AddAzureOpenAITextToImage( endpoint: config.Endpoint, apiKey: config.APIKey, modelId: config.Deployment, - serviceId: serviceId, + serviceId: config.ServiceId, apiVersion: apiVersion, httpClient: httpClient); break; @@ -2294,7 +2278,6 @@ public static IServiceCollection AddAzureOpenAIFiles( /// Required configuration for Azure OpenAI. /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. /// The API version to target. - /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] @@ -2303,7 +2286,6 @@ public static IKernelBuilder AddAzureOpenAIFiles( AzureOpenAIConfig config, string? orgId = null, string? version = null, - string? serviceId = null, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -2319,7 +2301,7 @@ public static IKernelBuilder AddAzureOpenAIFiles( apiKey: config.APIKey, orgId: orgId, version: version, - serviceId: serviceId, + serviceId: config.ServiceId, httpClient: httpClient); break; @@ -2337,15 +2319,13 @@ public static IKernelBuilder AddAzureOpenAIFiles( /// Required configuration for Azure OpenAI. /// OpenAI organization id. This is usually optional unless your account belongs to multiple organizations. /// The API version to target. - /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAIFiles( this IServiceCollection services, AzureOpenAIConfig config, string? orgId = null, - string? version = null, - string? serviceId = null) + string? version = null) { Verify.NotNull(services); Verify.NotNull(config); @@ -2360,7 +2340,7 @@ public static IServiceCollection AddAzureOpenAIFiles( apiKey: config.APIKey, orgId: orgId, version: version, - serviceId: serviceId); + serviceId: config.ServiceId); break; default: @@ -2453,14 +2433,12 @@ public static IServiceCollection AddAzureOpenAITextToAudio( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextToAudio( this IKernelBuilder builder, AzureOpenAIConfig config, - string? serviceId = null, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -2475,7 +2453,7 @@ public static IKernelBuilder AddAzureOpenAITextToAudio( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -2492,14 +2470,12 @@ public static IKernelBuilder AddAzureOpenAITextToAudio( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextToAudio( this IServiceCollection services, AzureOpenAIConfig config, - string? serviceId = null, HttpClient? httpClient = null) { Verify.NotNull(services); @@ -2514,7 +2490,7 @@ public static IServiceCollection AddAzureOpenAITextToAudio( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -2864,14 +2840,12 @@ public static IServiceCollection AddAzureOpenAIAudioToText( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAIAudioToText( this IKernelBuilder builder, AzureOpenAIConfig config, - string? serviceId = null, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -2886,7 +2860,7 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: new DefaultAzureCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -2896,7 +2870,7 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: config.GetTokenCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -2906,7 +2880,7 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment, httpClient: httpClient); break; @@ -2923,13 +2897,11 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// A local identifier for the given AI service. /// The same instance as . [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAIAudioToText( this IServiceCollection services, - AzureOpenAIConfig config, - string? serviceId = null) + AzureOpenAIConfig config) { Verify.NotNull(services); Verify.NotNull(config); @@ -2943,7 +2915,7 @@ public static IServiceCollection AddAzureOpenAIAudioToText( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: new DefaultAzureCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment); break; @@ -2952,7 +2924,7 @@ public static IServiceCollection AddAzureOpenAIAudioToText( deploymentName: config.Deployment, endpoint: config.Endpoint, credentials: config.GetTokenCredential(), - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment); break; @@ -2961,7 +2933,7 @@ public static IServiceCollection AddAzureOpenAIAudioToText( deploymentName: config.Deployment, endpoint: config.Endpoint, apiKey: config.APIKey, - serviceId: serviceId, + serviceId: config.ServiceId, modelId: config.Deployment); break; From 2b664476cb1ca7f9657db4fad1c013f4e5ad6c33 Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Tue, 21 May 2024 08:54:45 +0200 Subject: [PATCH 09/12] Add concrete AzureOpenAI config classes, adjust extension methods to use concrete config classes --- .../Configuration/AzureOpenAIApiKeyConfig.cs | 24 + .../Configuration/AzureOpenAIConfig.cs | 68 +- .../Configuration/AzureOpenAITokenConfig.cs | 26 + .../Configuration/OpenAIConfig.cs | 6 +- .../OpenAIServiceCollectionExtensions.cs | 845 +++++++++--------- 5 files changed, 467 insertions(+), 502 deletions(-) create mode 100644 dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIApiKeyConfig.cs create mode 100644 dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAITokenConfig.cs diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIApiKeyConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIApiKeyConfig.cs new file mode 100644 index 000000000000..1b80bec92d60 --- /dev/null +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIApiKeyConfig.cs @@ -0,0 +1,24 @@ +// Copyright (c) Microsoft. All rights reserved. + +namespace Microsoft.SemanticKernel; + +public class AzureOpenAIApiKeyConfig : AzureOpenAIConfig +{ + /// + /// API key required by the service. + /// + public string ApiKey { get; set; } = string.Empty; + + /// + /// Verify that the current state is valid. + /// + public override void Validate() + { + base.Validate(); + + if (string.IsNullOrWhiteSpace(this.ApiKey)) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.ApiKey)} is empty"); + } + } +} diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs index f971d921cfef..9bda86d2d499 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs @@ -1,41 +1,11 @@ // Copyright (c) Microsoft. All rights reserved. using System; -using Azure.Core; namespace Microsoft.SemanticKernel; -public class AzureOpenAIConfig +public abstract class AzureOpenAIConfig { - private TokenCredential? _tokenCredential; - - public enum AuthTypes - { - Unknown = -1, - AzureIdentity, - APIKey, - ManualTokenCredential, - } - - public enum APITypes - { - Unknown = -1, - TextCompletion, - ChatCompletion, - ImageGeneration, - EmbeddingGeneration, - } - - /// - /// OpenAI API type, e.g. text completion, chat completion, image generation, etc. - /// - public APITypes APIType { get; set; } = APITypes.ChatCompletion; - - /// - /// Azure authentication type. - /// - public AuthTypes Auth { get; set; } - /// /// Azure OpenAI endpoint URL. /// @@ -46,11 +16,6 @@ public enum APITypes /// public string Deployment { get; set; } = string.Empty; - /// - /// API key, required if Auth == APIKey. - /// - public string APIKey { get; set; } = string.Empty; - /// /// The number of dimensions output embeddings should have. /// Only supported in "text-embedding-3" and later models developed with @@ -63,40 +28,11 @@ public enum APITypes /// public string ServiceId { get; set; } = string.Empty; - /// - /// Set credentials manually from code. - /// - /// Token credentials - public void SetCredential(TokenCredential credential) - { - this.Auth = AuthTypes.ManualTokenCredential; - this._tokenCredential = credential; - } - - /// - /// Fetch the credentials passed manually from code. - /// - public TokenCredential GetTokenCredential() - { - return this._tokenCredential - ?? throw new ConfigurationException($"Azure OpenAI: {nameof(this._tokenCredential)} not defined"); - } - /// /// Verify that the current state is valid. /// - public void Validate() + public virtual void Validate() { - if (this.Auth == AuthTypes.Unknown) - { - throw new ConfigurationException($"Azure OpenAI: {nameof(this.Auth)} (authentication type) is not defined"); - } - - if (this.Auth == AuthTypes.APIKey && string.IsNullOrWhiteSpace(this.APIKey)) - { - throw new ConfigurationException($"Azure OpenAI: {nameof(this.APIKey)} is empty"); - } - if (string.IsNullOrWhiteSpace(this.Endpoint)) { throw new ConfigurationException($"Azure OpenAI: {nameof(this.Endpoint)} is empty"); diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAITokenConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAITokenConfig.cs new file mode 100644 index 000000000000..301aa3e5969c --- /dev/null +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAITokenConfig.cs @@ -0,0 +1,26 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Azure.Core; + +namespace Microsoft.SemanticKernel; + +public class AzureOpenAITokenConfig : AzureOpenAIConfig +{ + /// + /// Token credential required by the service. + /// + public TokenCredential? TokenCredential { get; set; } + + /// + /// Verify that the current state is valid. + /// + public override void Validate() + { + base.Validate(); + + if (this.TokenCredential is null) + { + throw new ConfigurationException($"Azure OpenAI: {nameof(this.TokenCredential)} is null"); + } + } +} diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/OpenAIConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/OpenAIConfig.cs index 27ba37359b48..7fde2188f9ba 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/OpenAIConfig.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/OpenAIConfig.cs @@ -43,7 +43,7 @@ public enum TextGenerationTypes /// /// OpenAI API key. /// - public string APIKey { get; set; } = string.Empty; + public string ApiKey { get; set; } = string.Empty; /// /// Optional OpenAI Organization ID. @@ -62,9 +62,9 @@ public enum TextGenerationTypes /// public void Validate() { - if (string.IsNullOrWhiteSpace(this.APIKey)) + if (string.IsNullOrWhiteSpace(this.ApiKey)) { - throw new ConfigurationException($"OpenAI: {nameof(this.APIKey)} is empty"); + throw new ConfigurationException($"OpenAI: {nameof(this.ApiKey)} is empty"); } if (this.EmbeddingDimensions is < 1) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs index 6db3fcbfe01c..6e722e994c41 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs @@ -6,7 +6,6 @@ using Azure; using Azure.AI.OpenAI; using Azure.Core; -using Azure.Identity; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.SemanticKernel.AudioToText; @@ -224,7 +223,7 @@ public static IServiceCollection AddAzureOpenAITextGeneration( [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextGeneration( this IKernelBuilder builder, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -232,41 +231,42 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - builder.AddAzureOpenAITextGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: new DefaultAzureCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - builder.AddAzureOpenAITextGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: config.GetTokenCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - builder.AddAzureOpenAITextGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + builder.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient); + + return builder; + } + + /// + /// Adds an Azure OpenAI text generation service with the specified configuration. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddAzureOpenAITextGeneration( + this IKernelBuilder builder, + AzureOpenAITokenConfig config, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + builder.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.TokenCredential!, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient); return builder; } @@ -280,45 +280,45 @@ public static IKernelBuilder AddAzureOpenAITextGeneration( [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextGeneration( this IServiceCollection services, - AzureOpenAIConfig config) + AzureOpenAIApiKeyConfig config) { Verify.NotNull(services); Verify.NotNull(config); config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - services.AddAzureOpenAITextGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: new DefaultAzureCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - services.AddAzureOpenAITextGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: config.GetTokenCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - services.AddAzureOpenAITextGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + services.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment); + + return services; + } + + /// + /// Adds an Azure OpenAI text generation service with the specified configuration. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAITextGeneration( + this IServiceCollection services, + AzureOpenAITokenConfig config) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + services.AddAzureOpenAITextGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.TokenCredential!, + serviceId: config.ServiceId, + modelId: config.Deployment); return services; } @@ -457,7 +457,7 @@ public static IKernelBuilder AddOpenAITextGeneration( return builder.AddOpenAITextGeneration( modelId: config.TextModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId, httpClient: httpClient); @@ -483,7 +483,7 @@ public static IServiceCollection AddOpenAITextGeneration( return services.AddOpenAITextGeneration( modelId: config.TextModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId); } @@ -722,7 +722,7 @@ public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( this IKernelBuilder builder, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -730,44 +730,44 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - builder.AddAzureOpenAITextEmbeddingGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credential: new DefaultAzureCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient, - dimensions: config.EmbeddingDimensions); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - builder.AddAzureOpenAITextEmbeddingGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credential: config.GetTokenCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient, - dimensions: config.EmbeddingDimensions); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - builder.AddAzureOpenAITextEmbeddingGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient, - dimensions: config.EmbeddingDimensions); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + builder.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient, + dimensions: config.EmbeddingDimensions); + + return builder; + } + + /// + /// Adds an Azure OpenAI text embeddings service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( + this IKernelBuilder builder, + AzureOpenAITokenConfig config, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + builder.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credential: config.TokenCredential!, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient, + dimensions: config.EmbeddingDimensions); return builder; } @@ -781,48 +781,47 @@ public static IKernelBuilder AddAzureOpenAITextEmbeddingGeneration( [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( this IServiceCollection services, - AzureOpenAIConfig config) + AzureOpenAIApiKeyConfig config) { Verify.NotNull(services); Verify.NotNull(config); config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - services.AddAzureOpenAITextEmbeddingGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credential: new DefaultAzureCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - dimensions: config.EmbeddingDimensions); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - services.AddAzureOpenAITextEmbeddingGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credential: config.GetTokenCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - dimensions: config.EmbeddingDimensions); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - services.AddAzureOpenAITextEmbeddingGeneration( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment, - dimensions: config.EmbeddingDimensions); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + services.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment, + dimensions: config.EmbeddingDimensions); + + return services; + } + + /// + /// Adds an Azure OpenAI text embeddings service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAITextEmbeddingGeneration( + this IServiceCollection services, + AzureOpenAITokenConfig config) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + services.AddAzureOpenAITextEmbeddingGeneration( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credential: config.TokenCredential!, + serviceId: config.ServiceId, + modelId: config.Deployment, + dimensions: config.EmbeddingDimensions); return services; } @@ -977,7 +976,7 @@ public static IKernelBuilder AddOpenAITextEmbeddingGeneration( return builder.AddOpenAITextEmbeddingGeneration( modelId: config.EmbeddingModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId, httpClient: httpClient, @@ -1004,7 +1003,7 @@ public static IServiceCollection AddOpenAITextEmbeddingGeneration( return services.AddOpenAITextEmbeddingGeneration( modelId: config.EmbeddingModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId, dimensions: config.EmbeddingDimensions); @@ -1303,7 +1302,7 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAIChatCompletion( this IKernelBuilder builder, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -1311,41 +1310,45 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - builder.AddAzureOpenAIChatCompletion( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: new DefaultAzureCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - builder.AddAzureOpenAIChatCompletion( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: config.GetTokenCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - builder.AddAzureOpenAIChatCompletion( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + builder.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient); + + return builder; + } + + /// + /// Adds the Azure OpenAI chat completion with data service to the list. + /// + /// The instance. + /// Required configuration for Azure OpenAI. + /// The HttpClient to use with this service. + /// The same instance as . + /// + /// More information: + /// + [Experimental("SKEXP0010")] + public static IKernelBuilder AddAzureOpenAIChatCompletion( + this IKernelBuilder builder, + AzureOpenAITokenConfig config, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + builder.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.TokenCredential!, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient); return builder; } @@ -1362,45 +1365,48 @@ public static IKernelBuilder AddAzureOpenAIChatCompletion( [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAIChatCompletion( this IServiceCollection services, - AzureOpenAIConfig config) + AzureOpenAIApiKeyConfig config) { Verify.NotNull(services); Verify.NotNull(config); config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - services.AddAzureOpenAIChatCompletion( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: new DefaultAzureCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - services.AddAzureOpenAIChatCompletion( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: config.GetTokenCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - services.AddAzureOpenAIChatCompletion( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + services.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment); + + return services; + } + + /// + /// Adds the Azure OpenAI chat completion with data service to the list. + /// + /// The instance. + /// Required configuration for Azure OpenAI. + /// The same instance as . + /// + /// More information: + /// + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAIChatCompletion( + this IServiceCollection services, + AzureOpenAITokenConfig config) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + services.AddAzureOpenAIChatCompletion( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.TokenCredential!, + serviceId: config.ServiceId, + modelId: config.Deployment); return services; } @@ -1619,7 +1625,7 @@ public static IServiceCollection AddOpenAIChatCompletion( return services.AddOpenAIChatCompletion( modelId: config.TextModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId); } @@ -1646,7 +1652,7 @@ public static IKernelBuilder AddOpenAIChatCompletion( return builder.AddOpenAIChatCompletion( modelId: config.TextModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId, httpClient: httpClient); @@ -1883,7 +1889,7 @@ public static IKernelBuilder AddOpenAITextToImage( config.Validate(); return builder.AddOpenAITextToImage( - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId, httpClient: httpClient); @@ -1908,7 +1914,7 @@ public static IServiceCollection AddOpenAITextToImage( config.Validate(); return services.AddOpenAITextToImage( - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId); } @@ -1981,7 +1987,7 @@ public static IKernelBuilder AddAzureOpenAITextToImage( [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextToImage( this IServiceCollection services, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, string? apiVersion = null) { Verify.NotNull(services); @@ -1989,41 +1995,42 @@ public static IServiceCollection AddAzureOpenAITextToImage( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - services.AddAzureOpenAITextToImage( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: new DefaultAzureCredential(), - modelId: config.Deployment, - serviceId: config.ServiceId, - apiVersion: apiVersion); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - services.AddAzureOpenAITextToImage( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: config.GetTokenCredential(), - modelId: config.Deployment, - serviceId: config.ServiceId, - apiVersion: apiVersion); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - services.AddAzureOpenAITextToImage( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment, - apiVersion: apiVersion); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + services.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment, + apiVersion: apiVersion); + + return services; + } + + /// + /// Add the OpenAI Dall-E text to image service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// Azure OpenAI API version. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAITextToImage( + this IServiceCollection services, + AzureOpenAITokenConfig config, + string? apiVersion = null) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + services.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.TokenCredential!, + modelId: config.Deployment, + serviceId: config.ServiceId, + apiVersion: apiVersion); return services; } @@ -2039,7 +2046,7 @@ public static IServiceCollection AddAzureOpenAITextToImage( [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextToImage( this IKernelBuilder builder, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, string? apiVersion = null, HttpClient? httpClient = null) { @@ -2048,42 +2055,45 @@ public static IKernelBuilder AddAzureOpenAITextToImage( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - builder.AddAzureOpenAITextToImage( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: new DefaultAzureCredential(), - modelId: config.Deployment, - serviceId: config.ServiceId, - apiVersion: apiVersion); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - builder.AddAzureOpenAITextToImage( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: config.GetTokenCredential(), - modelId: config.Deployment, - serviceId: config.ServiceId, - apiVersion: apiVersion); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - builder.AddAzureOpenAITextToImage( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - modelId: config.Deployment, - serviceId: config.ServiceId, - apiVersion: apiVersion, - httpClient: httpClient); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + builder.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + modelId: config.Deployment, + serviceId: config.ServiceId, + apiVersion: apiVersion, + httpClient: httpClient); + + return builder; + } + + /// + /// Add the OpenAI Dall-E text to image service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// Azure OpenAI API version. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddAzureOpenAITextToImage( + this IKernelBuilder builder, + AzureOpenAITokenConfig config, + string? apiVersion = null, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + builder.AddAzureOpenAITextToImage( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.TokenCredential!, + modelId: config.Deployment, + serviceId: config.ServiceId, + apiVersion: apiVersion); return builder; } @@ -2171,7 +2181,7 @@ public static IKernelBuilder AddOpenAIFiles( config.Validate(); return builder.AddOpenAIFiles( - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId, httpClient: httpClient); @@ -2196,7 +2206,7 @@ public static IServiceCollection AddOpenAIFiles( config.Validate(); return services.AddOpenAIFiles( - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId); } @@ -2283,7 +2293,7 @@ public static IServiceCollection AddAzureOpenAIFiles( [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAIFiles( this IKernelBuilder builder, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, string? orgId = null, string? version = null, HttpClient? httpClient = null) @@ -2293,21 +2303,13 @@ public static IKernelBuilder AddAzureOpenAIFiles( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.APIKey: - builder.AddAzureOpenAIFiles( - endpoint: config.Endpoint, - apiKey: config.APIKey, - orgId: orgId, - version: version, - serviceId: config.ServiceId, - httpClient: httpClient); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + builder.AddAzureOpenAIFiles( + endpoint: config.Endpoint, + apiKey: config.ApiKey, + orgId: orgId, + version: version, + serviceId: config.ServiceId, + httpClient: httpClient); return builder; } @@ -2323,7 +2325,7 @@ public static IKernelBuilder AddAzureOpenAIFiles( [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAIFiles( this IServiceCollection services, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, string? orgId = null, string? version = null) { @@ -2332,20 +2334,12 @@ public static IServiceCollection AddAzureOpenAIFiles( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.APIKey: - services.AddAzureOpenAIFiles( - endpoint: config.Endpoint, - apiKey: config.APIKey, - orgId: orgId, - version: version, - serviceId: config.ServiceId); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + services.AddAzureOpenAIFiles( + endpoint: config.Endpoint, + apiKey: config.ApiKey, + orgId: orgId, + version: version, + serviceId: config.ServiceId); return services; } @@ -2438,7 +2432,7 @@ public static IServiceCollection AddAzureOpenAITextToAudio( [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextToAudio( this IKernelBuilder builder, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -2446,21 +2440,13 @@ public static IKernelBuilder AddAzureOpenAITextToAudio( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.APIKey: - builder.AddAzureOpenAITextToAudio( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + builder.AddAzureOpenAITextToAudio( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient); return builder; } @@ -2475,7 +2461,7 @@ public static IKernelBuilder AddAzureOpenAITextToAudio( [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAITextToAudio( this IServiceCollection services, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, HttpClient? httpClient = null) { Verify.NotNull(services); @@ -2483,21 +2469,13 @@ public static IServiceCollection AddAzureOpenAITextToAudio( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.APIKey: - services.AddAzureOpenAITextToAudio( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + services.AddAzureOpenAITextToAudio( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient); return services; } @@ -2588,7 +2566,7 @@ public static IKernelBuilder AddOpenAITextToAudio( return builder.AddOpenAITextToAudio( modelId: config.TextModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId, httpClient: httpClient); @@ -2614,7 +2592,7 @@ public static IServiceCollection AddOpenAITextToAudio( return services.AddOpenAITextToAudio( modelId: config.TextModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId); } @@ -2845,7 +2823,7 @@ public static IServiceCollection AddAzureOpenAIAudioToText( [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAIAudioToText( this IKernelBuilder builder, - AzureOpenAIConfig config, + AzureOpenAIApiKeyConfig config, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -2853,41 +2831,42 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - builder.AddAzureOpenAIAudioToText( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: new DefaultAzureCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - builder.AddAzureOpenAIAudioToText( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: config.GetTokenCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - builder.AddAzureOpenAIAudioToText( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment, - httpClient: httpClient); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + builder.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient); + + return builder; + } + + /// + /// Adds the Azure OpenAI audio-to-text service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// The HttpClient to use with this service. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IKernelBuilder AddAzureOpenAIAudioToText( + this IKernelBuilder builder, + AzureOpenAITokenConfig config, + HttpClient? httpClient = null) + { + Verify.NotNull(builder); + Verify.NotNull(config); + + config.Validate(); + + builder.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.TokenCredential!, + serviceId: config.ServiceId, + modelId: config.Deployment, + httpClient: httpClient); return builder; } @@ -2901,45 +2880,45 @@ public static IKernelBuilder AddAzureOpenAIAudioToText( [Experimental("SKEXP0010")] public static IServiceCollection AddAzureOpenAIAudioToText( this IServiceCollection services, - AzureOpenAIConfig config) + AzureOpenAIApiKeyConfig config) { Verify.NotNull(services); Verify.NotNull(config); config.Validate(); - switch (config.Auth) - { - case AzureOpenAIConfig.AuthTypes.AzureIdentity: - services.AddAzureOpenAIAudioToText( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: new DefaultAzureCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment); - break; - - case AzureOpenAIConfig.AuthTypes.ManualTokenCredential: - services.AddAzureOpenAIAudioToText( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - credentials: config.GetTokenCredential(), - serviceId: config.ServiceId, - modelId: config.Deployment); - break; - - case AzureOpenAIConfig.AuthTypes.APIKey: - services.AddAzureOpenAIAudioToText( - deploymentName: config.Deployment, - endpoint: config.Endpoint, - apiKey: config.APIKey, - serviceId: config.ServiceId, - modelId: config.Deployment); - break; - - default: - throw new NotImplementedException($"Azure OpenAI auth type '{config.Auth}' not available"); - } + services.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + apiKey: config.ApiKey, + serviceId: config.ServiceId, + modelId: config.Deployment); + + return services; + } + + /// + /// Adds the Azure OpenAI audio-to-text service to the list. + /// + /// The instance to augment. + /// Required configuration for Azure OpenAI. + /// The same instance as . + [Experimental("SKEXP0010")] + public static IServiceCollection AddAzureOpenAIAudioToText( + this IServiceCollection services, + AzureOpenAITokenConfig config) + { + Verify.NotNull(services); + Verify.NotNull(config); + + config.Validate(); + + services.AddAzureOpenAIAudioToText( + deploymentName: config.Deployment, + endpoint: config.Endpoint, + credentials: config.TokenCredential!, + serviceId: config.ServiceId, + modelId: config.Deployment); return services; } @@ -3086,7 +3065,7 @@ public static IKernelBuilder AddOpenAIAudioToText( return builder.AddOpenAIAudioToText( modelId: config.TextModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId, httpClient: httpClient); @@ -3112,7 +3091,7 @@ public static IServiceCollection AddOpenAIAudioToText( return services.AddOpenAIAudioToText( modelId: config.TextModel, - apiKey: config.APIKey, + apiKey: config.ApiKey, orgId: config.OrgId, serviceId: serviceId); } From 54e07c21ffdd453b962a56ed2b53374aedb4dbcf Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Tue, 21 May 2024 09:21:20 +0200 Subject: [PATCH 10/12] Add ApiVersion to AzureOpenAIConfig, adjust extension methods --- .../Configuration/AzureOpenAIConfig.cs | 5 +++++ .../OpenAIServiceCollectionExtensions.cs | 12 +++--------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs index 9bda86d2d499..aafdb521353f 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIConfig.cs @@ -23,6 +23,11 @@ public abstract class AzureOpenAIConfig /// public int? EmbeddingDimensions { get; set; } + /// + /// Azure OpenAI API version. + /// + public string ApiVersion { get; set; } = string.Empty; + /// /// A local identifier for the given AI service. /// diff --git a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs index 6e722e994c41..3cda2f9ee0bb 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs @@ -2040,14 +2040,12 @@ public static IServiceCollection AddAzureOpenAITextToImage( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// Azure OpenAI API version. /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextToImage( this IKernelBuilder builder, AzureOpenAIApiKeyConfig config, - string? apiVersion = null, HttpClient? httpClient = null) { Verify.NotNull(builder); @@ -2061,7 +2059,7 @@ public static IKernelBuilder AddAzureOpenAITextToImage( apiKey: config.ApiKey, modelId: config.Deployment, serviceId: config.ServiceId, - apiVersion: apiVersion, + apiVersion: config.ApiVersion, httpClient: httpClient); return builder; @@ -2072,15 +2070,11 @@ public static IKernelBuilder AddAzureOpenAITextToImage( /// /// The instance to augment. /// Required configuration for Azure OpenAI. - /// Azure OpenAI API version. - /// The HttpClient to use with this service. /// The same instance as . [Experimental("SKEXP0010")] public static IKernelBuilder AddAzureOpenAITextToImage( this IKernelBuilder builder, - AzureOpenAITokenConfig config, - string? apiVersion = null, - HttpClient? httpClient = null) + AzureOpenAITokenConfig config) { Verify.NotNull(builder); Verify.NotNull(config); @@ -2093,7 +2087,7 @@ public static IKernelBuilder AddAzureOpenAITextToImage( credentials: config.TokenCredential!, modelId: config.Deployment, serviceId: config.ServiceId, - apiVersion: apiVersion); + apiVersion: config.ApiVersion); return builder; } From 265c7322d6e1c4f31d4251908d550713dabf93c9 Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Tue, 21 May 2024 09:22:29 +0200 Subject: [PATCH 11/12] Delete KernelConfig --- .../Configuration/KernelConfig.cs | 45 ------------------- 1 file changed, 45 deletions(-) delete mode 100644 dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs diff --git a/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs b/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs deleted file mode 100644 index c7cb874b586c..000000000000 --- a/dotnet/src/SemanticKernel.Core/Configuration/KernelConfig.cs +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using System.Collections.Generic; -using Microsoft.Extensions.Configuration; - -namespace Microsoft.SemanticKernel; - -public class KernelConfig -{ - public string TextGenerationType { get; set; } = string.Empty; - - public string EmbeddingGeneratorType { get; set; } = string.Empty; - - public string ChatCompletionType { get; set; } = string.Empty; - - public string TextToImageType { get; set; } = string.Empty; - - public string FilesType { get; set; } = string.Empty; - - public string TextToAudioType { get; set; } = string.Empty; - - public string AudioToTextType { get; set; } = string.Empty; - - /// - /// Dependencies settings, e.g. credentials, endpoints, etc. - /// - public Dictionary> Services { get; set; } = new(); - - /// - /// Fetch a service configuration from the "Services" node - /// - /// Configuration instance - /// Service name - /// Root node name of the Kernel Memory config - /// Type of configuration to retrieve - /// Instance of T configuration class - public T GetServiceConfig(IConfiguration cfg, string serviceName, string root = "Kernel") - { - return cfg - .GetSection(root) - .GetSection("Services") - .GetSection(serviceName) - .Get() ?? throw new ConfigurationException($"The {serviceName} configuration is NULL"); - } -} From 0911792dddc9af599936cba1580f7745fb10cc3c Mon Sep 17 00:00:00 2001 From: Artur Kordowski <9746197+akordowski@users.noreply.github.com> Date: Mon, 27 May 2024 08:13:49 +0200 Subject: [PATCH 12/12] Apply suggestions from code review Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com> --- .../Connectors.OpenAI/Configuration/AzureOpenAIApiKeyConfig.cs | 2 +- .../src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj | 1 - dotnet/src/SemanticKernel.Core/SemanticKernel.Core.csproj | 2 -- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIApiKeyConfig.cs b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIApiKeyConfig.cs index 1b80bec92d60..c9f969568967 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIApiKeyConfig.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/Configuration/AzureOpenAIApiKeyConfig.cs @@ -5,7 +5,7 @@ namespace Microsoft.SemanticKernel; public class AzureOpenAIApiKeyConfig : AzureOpenAIConfig { /// - /// API key required by the service. + /// API key required by the service. /// public string ApiKey { get; set; } = string.Empty; diff --git a/dotnet/src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj b/dotnet/src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj index 63f00af1599f..f873d8d9cd29 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj +++ b/dotnet/src/Connectors/Connectors.OpenAI/Connectors.OpenAI.csproj @@ -30,6 +30,5 @@ - diff --git a/dotnet/src/SemanticKernel.Core/SemanticKernel.Core.csproj b/dotnet/src/SemanticKernel.Core/SemanticKernel.Core.csproj index b8ba0247e9e0..7eeee98743d5 100644 --- a/dotnet/src/SemanticKernel.Core/SemanticKernel.Core.csproj +++ b/dotnet/src/SemanticKernel.Core/SemanticKernel.Core.csproj @@ -29,8 +29,6 @@ - -