diff --git a/dotnet/samples/KernelSyntaxExamples/Example05_InlineFunctionDefinition.cs b/dotnet/samples/KernelSyntaxExamples/Example05_InlineFunctionDefinition.cs index e48b3249e114..08c36c1f31eb 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example05_InlineFunctionDefinition.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example05_InlineFunctionDefinition.cs @@ -27,7 +27,7 @@ public static async Task RunAsync() * function inline if you like. */ - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: openAIModelId, apiKey: openAIApiKey) diff --git a/dotnet/samples/KernelSyntaxExamples/Example06_TemplateLanguage.cs b/dotnet/samples/KernelSyntaxExamples/Example06_TemplateLanguage.cs index 515766f98834..499c4cb145c2 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example06_TemplateLanguage.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example06_TemplateLanguage.cs @@ -26,7 +26,7 @@ public static async Task RunAsync() return; } - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: openAIModelId, apiKey: openAIApiKey) diff --git a/dotnet/samples/KernelSyntaxExamples/Example07_BingAndGooglePlugins.cs b/dotnet/samples/KernelSyntaxExamples/Example07_BingAndGooglePlugins.cs index f9259b0cf450..74814827c15d 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example07_BingAndGooglePlugins.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example07_BingAndGooglePlugins.cs @@ -28,7 +28,7 @@ public static async Task RunAsync() return; } - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: openAIModelId, apiKey: openAIApiKey) diff --git a/dotnet/samples/KernelSyntaxExamples/Example08_RetryHandler.cs b/dotnet/samples/KernelSyntaxExamples/Example08_RetryHandler.cs index 1c3ff52672c6..5f3389b18783 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example08_RetryHandler.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example08_RetryHandler.cs @@ -19,7 +19,7 @@ public static class Example08_RetryHandler public static async Task RunAsync() { // Create a Kernel with the HttpClient - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddLogging(c => c.AddConsole().SetMinimumLevel(LogLevel.Information)); builder.Services.ConfigureHttpClientDefaults(c => { diff --git a/dotnet/samples/KernelSyntaxExamples/Example09_FunctionTypes.cs b/dotnet/samples/KernelSyntaxExamples/Example09_FunctionTypes.cs index d7f3d0a3f569..f38992e80987 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example09_FunctionTypes.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example09_FunctionTypes.cs @@ -15,7 +15,7 @@ public static async Task RunAsync() { Console.WriteLine("======== Method Function types ========"); - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey) .Build(); diff --git a/dotnet/samples/KernelSyntaxExamples/Example10_DescribeAllPluginsAndFunctions.cs b/dotnet/samples/KernelSyntaxExamples/Example10_DescribeAllPluginsAndFunctions.cs index e63a06d82795..519959a8aaa3 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example10_DescribeAllPluginsAndFunctions.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example10_DescribeAllPluginsAndFunctions.cs @@ -21,7 +21,7 @@ public static Task RunAsync() { Console.WriteLine("======== Describe all plugins and functions ========"); - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: TestConfiguration.OpenAI.ChatModelId, apiKey: TestConfiguration.OpenAI.ApiKey) diff --git a/dotnet/samples/KernelSyntaxExamples/Example13_ConversationSummaryPlugin.cs b/dotnet/samples/KernelSyntaxExamples/Example13_ConversationSummaryPlugin.cs index 8206548ea76a..591115289268 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example13_ConversationSummaryPlugin.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example13_ConversationSummaryPlugin.cs @@ -168,7 +168,7 @@ private static async Task GetConversationTopicsAsync() private static Kernel InitializeKernel() { - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( TestConfiguration.AzureOpenAI.ChatDeploymentName, TestConfiguration.AzureOpenAI.ChatModelId, diff --git a/dotnet/samples/KernelSyntaxExamples/Example15_TextMemoryPlugin.cs b/dotnet/samples/KernelSyntaxExamples/Example15_TextMemoryPlugin.cs index 2c33070153e3..48a121f9e710 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example15_TextMemoryPlugin.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example15_TextMemoryPlugin.cs @@ -150,7 +150,7 @@ private static IMemoryStore CreateSampleKustoMemoryStore() private static async Task RunWithStoreAsync(IMemoryStore memoryStore, CancellationToken cancellationToken) { - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey) .AddOpenAITextEmbeddingGeneration(TestConfiguration.OpenAI.EmbeddingModelId, TestConfiguration.OpenAI.ApiKey) .Build(); diff --git a/dotnet/samples/KernelSyntaxExamples/Example16_CustomLLM.cs b/dotnet/samples/KernelSyntaxExamples/Example16_CustomLLM.cs index 49ef09a685d0..cc99ea628d30 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example16_CustomLLM.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example16_CustomLLM.cs @@ -51,7 +51,7 @@ private static async Task CustomTextGenerationWithSKFunctionAsync() { Console.WriteLine("======== Custom LLM - Text Completion - SKFunction ========"); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddSingleton(ConsoleLogger.LoggerFactory); // Add your text generation service as a singleton instance builder.Services.AddKeyedSingleton("myService1", new MyTextGenerationService()); diff --git a/dotnet/samples/KernelSyntaxExamples/Example18_DallE.cs b/dotnet/samples/KernelSyntaxExamples/Example18_DallE.cs index 5f0fef22a644..8692290dc735 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example18_DallE.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example18_DallE.cs @@ -22,7 +22,7 @@ private static async Task OpenAIDallEAsync() { Console.WriteLine("======== OpenAI Dall-E 2 Text To Image ========"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() // Add your text to image service .AddOpenAITextToImage(TestConfiguration.OpenAI.ApiKey) // Add your chat completion service @@ -90,7 +90,7 @@ public static async Task AzureOpenAIDallEAsync() { Console.WriteLine("========Azure OpenAI Dall-E 2 Text To Image ========"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() // Add your text to image service .AddAzureOpenAITextToImage(TestConfiguration.AzureOpenAI.Endpoint, TestConfiguration.AzureOpenAI.ImageModelId, TestConfiguration.AzureOpenAI.ApiKey) // Add your chat completion service diff --git a/dotnet/samples/KernelSyntaxExamples/Example20_HuggingFace.cs b/dotnet/samples/KernelSyntaxExamples/Example20_HuggingFace.cs index a5fd31b6b030..b6d9b73bb86e 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example20_HuggingFace.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example20_HuggingFace.cs @@ -23,7 +23,7 @@ private static async Task RunInferenceApiExampleAsync() { Console.WriteLine("\n======== HuggingFace Inference API example ========\n"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddHuggingFaceTextGeneration( model: TestConfiguration.HuggingFace.ModelId, apiKey: TestConfiguration.HuggingFace.ApiKey) @@ -57,7 +57,7 @@ private static async Task RunLlamaExampleAsync() // HuggingFace local HTTP server endpoint const string Endpoint = "http://localhost:5000/completions"; - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddHuggingFaceTextGeneration( model: Model, endpoint: Endpoint, diff --git a/dotnet/samples/KernelSyntaxExamples/Example26_AADAuth.cs b/dotnet/samples/KernelSyntaxExamples/Example26_AADAuth.cs index 106bf8d98c9a..80020891a77d 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example26_AADAuth.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example26_AADAuth.cs @@ -40,7 +40,7 @@ public static async Task RunAsync() ExcludeAzurePowerShellCredential = true }; - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() // Add Azure OpenAI chat completion service using DefaultAzureCredential AAD auth .AddAzureOpenAIChatCompletion( TestConfiguration.AzureOpenAI.ChatDeploymentName, diff --git a/dotnet/samples/KernelSyntaxExamples/Example27_PromptFunctionsUsingChatGPT.cs b/dotnet/samples/KernelSyntaxExamples/Example27_PromptFunctionsUsingChatGPT.cs index d5342c5d46d2..b237ef95ad23 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example27_PromptFunctionsUsingChatGPT.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example27_PromptFunctionsUsingChatGPT.cs @@ -14,7 +14,7 @@ public static async Task RunAsync() { Console.WriteLine("======== Using Chat GPT model for text generation ========"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion(TestConfiguration.AzureOpenAI.ChatDeploymentName, TestConfiguration.AzureOpenAI.ChatModelId, TestConfiguration.AzureOpenAI.Endpoint, TestConfiguration.AzureOpenAI.ApiKey) .Build(); diff --git a/dotnet/samples/KernelSyntaxExamples/Example30_ChatWithPrompts.cs b/dotnet/samples/KernelSyntaxExamples/Example30_ChatWithPrompts.cs index 79aa810df929..d5eab99da63d 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example30_ChatWithPrompts.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example30_ChatWithPrompts.cs @@ -50,7 +50,7 @@ public static async Task RunAsync() var selectedText = EmbeddedResource.Read("30-user-context.txt"); var userPromptTemplate = EmbeddedResource.Read("30-user-prompt.txt"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey, serviceId: "chat") .Build(); diff --git a/dotnet/samples/KernelSyntaxExamples/Example41_HttpClientUsage.cs b/dotnet/samples/KernelSyntaxExamples/Example41_HttpClientUsage.cs index 80d10f821295..9cbdad84bf2e 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example41_HttpClientUsage.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example41_HttpClientUsage.cs @@ -31,7 +31,7 @@ public static Task RunAsync() /// private static void UseDefaultHttpClient() { - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: TestConfiguration.OpenAI.ChatModelId, apiKey: TestConfiguration.OpenAI.ApiKey) // If you need to use the default HttpClient from the SK SDK, simply omit the argument for the httpMessageInvoker parameter. @@ -46,7 +46,7 @@ private static void UseCustomHttpClient() using var httpClient = new HttpClient(); // If you need to use a custom HttpClient, simply pass it as an argument for the httpClient parameter. - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: TestConfiguration.OpenAI.ModelId, apiKey: TestConfiguration.OpenAI.ApiKey, @@ -67,7 +67,7 @@ private static void UseBasicRegistrationWithHttpClientFactory() { var factory = sp.GetRequiredService(); - return new KernelBuilder() + return Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: TestConfiguration.OpenAI.ChatModelId, apiKey: TestConfiguration.OpenAI.ApiKey, @@ -96,7 +96,7 @@ private static void UseNamedRegistrationWitHttpClientFactory() { var factory = sp.GetRequiredService(); - return new KernelBuilder() + return Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: TestConfiguration.OpenAI.ChatModelId, apiKey: TestConfiguration.OpenAI.ApiKey, diff --git a/dotnet/samples/KernelSyntaxExamples/Example42_KernelBuilder.cs b/dotnet/samples/KernelSyntaxExamples/Example42_KernelBuilder.cs index 23bc99d7903d..626669508980 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example42_KernelBuilder.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example42_KernelBuilder.cs @@ -2,7 +2,7 @@ // ========================================================================================================== // The easier way to instantiate the Semantic Kernel is to use KernelBuilder. -// You can access the builder using new KernelBuilder(). +// You can access the builder using Kernel.CreateBuilder(). #pragma warning disable CA1852 @@ -26,26 +26,26 @@ public static Task RunAsync() // KernelBuilder provides a simple way to configure a Kernel. This constructs a kernel // with logging and an Azure OpenAI chat completion service configured. - Kernel kernel1 = new KernelBuilder() + Kernel kernel1 = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion(azureOpenAIChatDeploymentName, azureOpenAIChatModelId, azureOpenAIEndpoint, azureOpenAIKey) .Build(); // For greater flexibility and to incorporate arbitrary services, KernelBuilder.Services // provides direct access to an underlying IServiceCollection. - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddLogging(c => c.AddConsole().SetMinimumLevel(LogLevel.Information)) .AddHttpClient() .AddAzureOpenAIChatCompletion(azureOpenAIChatDeploymentName, azureOpenAIChatModelId, azureOpenAIEndpoint, azureOpenAIKey); Kernel kernel2 = builder.Build(); // Plugins may also be configured via the corresponding Plugins property. - builder = new KernelBuilder(); + builder = Kernel.CreateBuilder(); builder.Plugins.AddFromType(); Kernel kernel3 = builder.Build(); // Every call to KernelBuilder.Build creates a new Kernel instance, with a new service provider // and a new plugin collection. - builder = new KernelBuilder(); + builder = Kernel.CreateBuilder(); Debug.Assert(!ReferenceEquals(builder.Build(), builder.Build())); // KernelBuilder provides a convenient API for creating Kernel instances. However, it is just a diff --git a/dotnet/samples/KernelSyntaxExamples/Example43_GetModelResult.cs b/dotnet/samples/KernelSyntaxExamples/Example43_GetModelResult.cs index 192ce8160bd1..5147f244c2e4 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example43_GetModelResult.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example43_GetModelResult.cs @@ -17,7 +17,7 @@ public static async Task RunAsync() { Console.WriteLine("======== Inline Function Definition + Result ========"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: TestConfiguration.OpenAI.ChatModelId, apiKey: TestConfiguration.OpenAI.ApiKey) @@ -47,7 +47,7 @@ public static async Task RunAsync() Console.WriteLine(); // Getting the error details - kernel = new KernelBuilder() + kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, "Invalid Key") .Build(); var errorFunction = kernel.CreateFunctionFromPrompt(FunctionDefinition); diff --git a/dotnet/samples/KernelSyntaxExamples/Example48_GroundednessChecks.cs b/dotnet/samples/KernelSyntaxExamples/Example48_GroundednessChecks.cs index 7ae40fe2eda7..9eb265d13577 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example48_GroundednessChecks.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example48_GroundednessChecks.cs @@ -59,7 +59,7 @@ public static async Task RunAsync() public static async Task GroundednessCheckingAsync() { Console.WriteLine("\n======== Groundedness Checks ========"); - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( TestConfiguration.AzureOpenAI.ChatDeploymentName, TestConfiguration.AzureOpenAI.ChatModelId, @@ -126,7 +126,7 @@ which are not grounded in the original. Console.WriteLine("\n======== Planning - Groundedness Checks ========"); - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( TestConfiguration.AzureOpenAI.ChatDeploymentName, TestConfiguration.AzureOpenAI.ChatModelId, diff --git a/dotnet/samples/KernelSyntaxExamples/Example52_ApimAuth.cs b/dotnet/samples/KernelSyntaxExamples/Example52_ApimAuth.cs index 32d08d82650f..31c80aadc476 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example52_ApimAuth.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example52_ApimAuth.cs @@ -48,7 +48,7 @@ public static async Task RunAsync() }; var openAIClient = new OpenAIClient(apimUri, new BearerTokenCredential(accessToken), clientOptions); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddLogging(c => c.SetMinimumLevel(LogLevel.Warning).AddConsole()); builder.AddAzureOpenAIChatCompletion(TestConfiguration.AzureOpenAI.ChatDeploymentName, TestConfiguration.AzureOpenAI.ChatModelId, openAIClient); Kernel kernel = builder.Build(); diff --git a/dotnet/samples/KernelSyntaxExamples/Example54_AzureChatCompletionWithData.cs b/dotnet/samples/KernelSyntaxExamples/Example54_AzureChatCompletionWithData.cs index 305803232912..30478af29e81 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example54_AzureChatCompletionWithData.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example54_AzureChatCompletionWithData.cs @@ -82,7 +82,7 @@ private static async Task ExampleWithKernelAsync() var completionWithDataConfig = GetCompletionWithDataConfig(); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion(config: completionWithDataConfig) .Build(); diff --git a/dotnet/samples/KernelSyntaxExamples/Example56_TemplateMethodFunctionsWithMultipleArguments.cs b/dotnet/samples/KernelSyntaxExamples/Example56_TemplateMethodFunctionsWithMultipleArguments.cs index 79ff2c2397ef..8d913992900b 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example56_TemplateMethodFunctionsWithMultipleArguments.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example56_TemplateMethodFunctionsWithMultipleArguments.cs @@ -31,7 +31,7 @@ public static async Task RunAsync() return; } - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddLogging(c => c.AddConsole()); builder.AddAzureOpenAIChatCompletion( deploymentName: deploymentName, diff --git a/dotnet/samples/KernelSyntaxExamples/Example57_KernelHooks.cs b/dotnet/samples/KernelSyntaxExamples/Example57_KernelHooks.cs index 560815bd4417..0c3c733f3086 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example57_KernelHooks.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example57_KernelHooks.cs @@ -42,7 +42,7 @@ private static async Task GetUsageAsync() { Console.WriteLine("\n======== Get Usage Data ========\n"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: s_openAIModelId!, apiKey: s_openAIApiKey!) @@ -87,7 +87,7 @@ private static async Task GetRenderedPromptAsync() { Console.WriteLine("\n======== Get Rendered Prompt ========\n"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: s_openAIModelId!, apiKey: s_openAIApiKey!) @@ -126,7 +126,7 @@ private static async Task ChangingResultAsync() { Console.WriteLine("\n======== Changing/Filtering Function Result ========\n"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: s_openAIModelId!, apiKey: s_openAIApiKey!) @@ -160,7 +160,7 @@ private static async Task BeforeInvokeCancellationAsync() { Console.WriteLine("\n======== Cancelling Pipeline Execution - Invoking event ========\n"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: s_openAIModelId!, apiKey: s_openAIApiKey!) @@ -195,7 +195,7 @@ private static async Task AfterInvokeCancellationAsync() { Console.WriteLine("\n======== Cancelling Pipeline Execution - Invoked event ========\n"); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: s_openAIModelId!, apiKey: s_openAIApiKey!) diff --git a/dotnet/samples/KernelSyntaxExamples/Example58_ConfigureExecutionSettings.cs b/dotnet/samples/KernelSyntaxExamples/Example58_ConfigureExecutionSettings.cs index 2905fd9cddb3..5bcf2f802dbf 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example58_ConfigureExecutionSettings.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example58_ConfigureExecutionSettings.cs @@ -28,7 +28,7 @@ public static async Task RunAsync() return; } - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( deploymentName: chatDeploymentName, modelId: chatModelId, diff --git a/dotnet/samples/KernelSyntaxExamples/Example59_OpenAIFunctionCalling.cs b/dotnet/samples/KernelSyntaxExamples/Example59_OpenAIFunctionCalling.cs index 1b279e4b4701..32d6266638b6 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example59_OpenAIFunctionCalling.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example59_OpenAIFunctionCalling.cs @@ -24,7 +24,7 @@ public static class Example59_OpenAIFunctionCalling public static async Task RunAsync() { // Create kernel with chat completions service and plugins - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Plugins.AddFromType(); builder.Plugins.AddFromType(); builder.AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey); diff --git a/dotnet/samples/KernelSyntaxExamples/Example61_MultipleLLMs.cs b/dotnet/samples/KernelSyntaxExamples/Example61_MultipleLLMs.cs index 07e5dec83c87..70ac07222866 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example61_MultipleLLMs.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example61_MultipleLLMs.cs @@ -35,7 +35,7 @@ public static async Task RunAsync() return; } - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( deploymentName: azureDeploymentName, endpoint: azureEndpoint, diff --git a/dotnet/samples/KernelSyntaxExamples/Example62_CustomAIServiceSelector.cs b/dotnet/samples/KernelSyntaxExamples/Example62_CustomAIServiceSelector.cs index 30b15d562a6d..775ab14c51e4 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example62_CustomAIServiceSelector.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example62_CustomAIServiceSelector.cs @@ -39,7 +39,7 @@ public static async Task RunAsync() } // Build a kernel with multiple chat completion services - var builder = new KernelBuilder() + var builder = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( deploymentName: azureDeploymentName, endpoint: azureEndpoint, diff --git a/dotnet/samples/KernelSyntaxExamples/Example63_ChatCompletionPrompts.cs b/dotnet/samples/KernelSyntaxExamples/Example63_ChatCompletionPrompts.cs index 4663a1255f1d..13117521c548 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example63_ChatCompletionPrompts.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example63_ChatCompletionPrompts.cs @@ -18,7 +18,7 @@ public static async Task RunAsync() Respond with JSON. "; - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion( modelId: TestConfiguration.OpenAI.ChatModelId, apiKey: TestConfiguration.OpenAI.ApiKey) diff --git a/dotnet/samples/KernelSyntaxExamples/Example63_FlowOrchestrator.cs b/dotnet/samples/KernelSyntaxExamples/Example63_FlowOrchestrator.cs index 93bdf1c9628a..8b9543c8efb5 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example63_FlowOrchestrator.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example63_FlowOrchestrator.cs @@ -202,7 +202,7 @@ private static FlowOrchestratorConfig GetOrchestratorConfig() private static KernelBuilder GetKernelBuilder(ILoggerFactory loggerFactory) { - var builder = new KernelBuilder(); + var builder = Kernel.CreateBuilder(); return builder .WithAzureOpenAIChatCompletion( diff --git a/dotnet/samples/KernelSyntaxExamples/Example64_MultiplePromptTemplates.cs b/dotnet/samples/KernelSyntaxExamples/Example64_MultiplePromptTemplates.cs index c2a2beffa6de..9bb11fe454d7 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example64_MultiplePromptTemplates.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example64_MultiplePromptTemplates.cs @@ -29,7 +29,7 @@ public static async Task RunAsync() return; } - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( deploymentName: chatDeploymentName, modelId: chatModelId, diff --git a/dotnet/samples/KernelSyntaxExamples/Example65_HandlebarsPlanner.cs b/dotnet/samples/KernelSyntaxExamples/Example65_HandlebarsPlanner.cs index 0084e04def5e..fa13a14e9753 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example65_HandlebarsPlanner.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example65_HandlebarsPlanner.cs @@ -55,7 +55,7 @@ private static async Task RunSampleAsync(string goal, bool shouldPrintPrompt = f return; } - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( deploymentName: chatDeploymentName, modelId: chatModelId, diff --git a/dotnet/samples/KernelSyntaxExamples/Example66_FunctionCallingStepwisePlanner.cs b/dotnet/samples/KernelSyntaxExamples/Example66_FunctionCallingStepwisePlanner.cs index 0af398ca7282..ba16b06e838a 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example66_FunctionCallingStepwisePlanner.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example66_FunctionCallingStepwisePlanner.cs @@ -44,7 +44,7 @@ public static async Task RunAsync() /// A kernel instance private static Kernel InitializeKernel() { - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( TestConfiguration.AzureOpenAI.ChatDeploymentName, TestConfiguration.AzureOpenAI.ChatModelId, diff --git a/dotnet/samples/KernelSyntaxExamples/Example72_KernelStreaming.cs b/dotnet/samples/KernelSyntaxExamples/Example72_KernelStreaming.cs index 8a90377f5c00..c0ec80fd6178 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example72_KernelStreaming.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example72_KernelStreaming.cs @@ -30,7 +30,7 @@ public static async Task RunAsync() return; } - var kernel = new KernelBuilder() + var kernel = Kernel.CreateBuilder() .AddAzureOpenAIChatCompletion( deploymentName: chatDeploymentName, modelId: chatModelId, diff --git a/dotnet/samples/KernelSyntaxExamples/Example74_Pipelining.cs b/dotnet/samples/KernelSyntaxExamples/Example74_Pipelining.cs index de640b9d28a9..75afaf88e4d3 100644 --- a/dotnet/samples/KernelSyntaxExamples/Example74_Pipelining.cs +++ b/dotnet/samples/KernelSyntaxExamples/Example74_Pipelining.cs @@ -32,7 +32,7 @@ public static async Task RunAsync() }); KernelFunction pipeline = KernelFunctionCombinators.Pipe(new[] { parseInt32, multiplyByN, truncate, humanize }, "pipeline"); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.AddOpenAIChatCompletion( TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey); diff --git a/dotnet/samples/TelemetryExample/Program.cs b/dotnet/samples/TelemetryExample/Program.cs index d2c2b647481a..d9929d3bf949 100644 --- a/dotnet/samples/TelemetryExample/Program.cs +++ b/dotnet/samples/TelemetryExample/Program.cs @@ -93,7 +93,7 @@ private static Kernel GetKernel(ILoggerFactory loggerFactory) var bingConnector = new BingConnector(Env.Var("Bing__ApiKey")); var webSearchEnginePlugin = new WebSearchEnginePlugin(bingConnector); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddSingleton(loggerFactory); builder.AddAzureOpenAIChatCompletion( diff --git a/dotnet/src/Connectors/Connectors.HuggingFace/HuggingFaceKernelBuilderExtensions.cs b/dotnet/src/Connectors/Connectors.HuggingFace/HuggingFaceKernelBuilderExtensions.cs index 873dedab551a..cc6fccec7706 100644 --- a/dotnet/src/Connectors/Connectors.HuggingFace/HuggingFaceKernelBuilderExtensions.cs +++ b/dotnet/src/Connectors/Connectors.HuggingFace/HuggingFaceKernelBuilderExtensions.cs @@ -9,14 +9,14 @@ namespace Microsoft.SemanticKernel; /// -/// Provides extension methods for the class to configure Hugging Face connectors. +/// Provides extension methods for the class to configure Hugging Face connectors. /// public static class HuggingFaceKernelBuilderExtensions { /// /// Adds an Hugging Face text generation service with the specified configuration. /// - /// The instance to augment. + /// The instance to augment. /// The name of the Hugging Face model. /// The API key required for accessing the Hugging Face service. /// The endpoint URL for the text generation service. @@ -66,7 +66,7 @@ public static IServiceCollection AddHuggingFaceTextGeneration( /// /// Adds an Hugging Face text embedding generation service with the specified configuration. /// - /// The instance to augment. + /// The instance to augment. /// The name of the Hugging Face model. /// The endpoint for the text embedding generation service. /// 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 704541b4e135..cb0836c6bb5a 100644 --- a/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs +++ b/dotnet/src/Connectors/Connectors.OpenAI/OpenAIServiceCollectionExtensions.cs @@ -893,7 +893,7 @@ public static IServiceCollection AddAzureOpenAIChatCompletion( /// /// Adds the Azure OpenAI chat completion with data service to the list. /// - /// The instance. + /// The instance. /// Required configuration for Azure OpenAI chat completion with data. /// A local identifier for the given AI service. /// The same instance as . diff --git a/dotnet/src/Connectors/Connectors.UnitTests/OpenAI/AIServicesOpenAIExtensionsTests.cs b/dotnet/src/Connectors/Connectors.UnitTests/OpenAI/AIServicesOpenAIExtensionsTests.cs index 78e7c3457be3..a265b1327e9c 100644 --- a/dotnet/src/Connectors/Connectors.UnitTests/OpenAI/AIServicesOpenAIExtensionsTests.cs +++ b/dotnet/src/Connectors/Connectors.UnitTests/OpenAI/AIServicesOpenAIExtensionsTests.cs @@ -17,7 +17,7 @@ public class AIServicesOpenAIExtensionsTests [Fact] public void ItSucceedsWhenAddingDifferentServiceTypeWithSameId() { - Kernel targetKernel = new KernelBuilder() + Kernel targetKernel = Kernel.CreateBuilder() .AddAzureOpenAITextGeneration("depl", "model", "https://url", "key", "azure") .AddAzureOpenAITextEmbeddingGeneration("depl2", "model2", "https://url", "key", "azure") .Build(); @@ -29,7 +29,7 @@ public void ItSucceedsWhenAddingDifferentServiceTypeWithSameId() [Fact] public void ItTellsIfAServiceIsAvailable() { - Kernel targetKernel = new KernelBuilder() + Kernel targetKernel = Kernel.CreateBuilder() .AddAzureOpenAITextGeneration("depl", "model", "https://url", "key", serviceId: "azure") .AddOpenAITextGeneration("model", "apikey", serviceId: "oai") .AddAzureOpenAITextEmbeddingGeneration("depl2", "model2", "https://url2", "key", serviceId: "azure") @@ -48,7 +48,7 @@ public void ItCanOverwriteServices() { // Arrange // Act - Assert no exception occurs - var builder = new KernelBuilder(); + var builder = Kernel.CreateBuilder(); builder.Services.AddAzureOpenAITextGeneration("dep", "model", "https://localhost", "key", serviceId: "one"); builder.Services.AddAzureOpenAITextGeneration("dep", "model", "https://localhost", "key", serviceId: "one"); diff --git a/dotnet/src/Experimental/Assistants/Internal/Assistant.cs b/dotnet/src/Experimental/Assistants/Internal/Assistant.cs index 66d7f9ef9744..a37e4a2f1ae3 100644 --- a/dotnet/src/Experimental/Assistants/Internal/Assistant.cs +++ b/dotnet/src/Experimental/Assistants/Internal/Assistant.cs @@ -81,7 +81,7 @@ internal Assistant( this._model = model; this._restContext = restContext; - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.AddOpenAIChatCompletion(this._model.Model, this._restContext.ApiKey); this.Kernel = builder.Build(); diff --git a/dotnet/src/Experimental/Orchestration.Flow.IntegrationTests/FlowOrchestratorTests.cs b/dotnet/src/Experimental/Orchestration.Flow.IntegrationTests/FlowOrchestratorTests.cs index 308844be2a54..7427d4eddc61 100644 --- a/dotnet/src/Experimental/Orchestration.Flow.IntegrationTests/FlowOrchestratorTests.cs +++ b/dotnet/src/Experimental/Orchestration.Flow.IntegrationTests/FlowOrchestratorTests.cs @@ -42,7 +42,7 @@ public FlowOrchestratorTests(ITestOutputHelper output) public async Task CanExecuteFlowAsync() { // Arrange - KernelBuilder builder = this.InitializeKernelBuilder(); + IKernelBuilder builder = this.InitializeKernelBuilder(); var bingConnector = new BingConnector(this._bingApiKey); var webSearchEnginePlugin = new WebSearchEnginePlugin(bingConnector); var sessionId = Guid.NewGuid().ToString(); @@ -103,7 +103,7 @@ private KernelBuilder InitializeKernelBuilder() AzureOpenAIConfiguration? azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get(); Assert.NotNull(azureOpenAIConfiguration); - return new KernelBuilder() + return Kernel.CreateBuilder() .WithAzureOpenAIChatCompletion( deploymentName: azureOpenAIConfiguration.ChatDeploymentName!, endpoint: azureOpenAIConfiguration.Endpoint, diff --git a/dotnet/src/Experimental/Orchestration.Flow/Execution/FlowExecutor.cs b/dotnet/src/Experimental/Orchestration.Flow/Execution/FlowExecutor.cs index baf815649ad4..290e0a224d2d 100644 --- a/dotnet/src/Experimental/Orchestration.Flow/Execution/FlowExecutor.cs +++ b/dotnet/src/Experimental/Orchestration.Flow/Execution/FlowExecutor.cs @@ -29,7 +29,7 @@ internal class FlowExecutor : IFlowExecutor /// /// The kernel builder /// - private readonly KernelBuilder _kernelBuilder; + private readonly IKernelBuilder _kernelBuilder; /// /// The logger @@ -94,7 +94,7 @@ internal class FlowExecutor : IFlowExecutor /// private readonly KernelFunction _checkStartStepFunction; - internal FlowExecutor(KernelBuilder kernelBuilder, IFlowStatusProvider statusProvider, Dictionary globalPluginCollection, FlowOrchestratorConfig? config = null) + internal FlowExecutor(IKernelBuilder kernelBuilder, IFlowStatusProvider statusProvider, Dictionary globalPluginCollection, FlowOrchestratorConfig? config = null) { this._kernelBuilder = kernelBuilder; this._systemKernel = kernelBuilder.Build(); diff --git a/dotnet/src/Experimental/Orchestration.Flow/FlowOrchestrator.cs b/dotnet/src/Experimental/Orchestration.Flow/FlowOrchestrator.cs index 77e50e28aec8..1fe0b2a1d39c 100644 --- a/dotnet/src/Experimental/Orchestration.Flow/FlowOrchestrator.cs +++ b/dotnet/src/Experimental/Orchestration.Flow/FlowOrchestrator.cs @@ -14,7 +14,7 @@ namespace Microsoft.SemanticKernel.Experimental.Orchestration; /// public class FlowOrchestrator { - private readonly KernelBuilder _kernelBuilder; + private readonly IKernelBuilder _kernelBuilder; private readonly IFlowStatusProvider _flowStatusProvider; diff --git a/dotnet/src/IntegrationTests/Connectors/OpenAI/OpenAICompletionTests.cs b/dotnet/src/IntegrationTests/Connectors/OpenAI/OpenAICompletionTests.cs index fe8697c7ef95..2a17ae3048ed 100644 --- a/dotnet/src/IntegrationTests/Connectors/OpenAI/OpenAICompletionTests.cs +++ b/dotnet/src/IntegrationTests/Connectors/OpenAI/OpenAICompletionTests.cs @@ -24,7 +24,7 @@ namespace SemanticKernel.IntegrationTests.Connectors.OpenAI; public sealed class OpenAICompletionTests : IDisposable { - private readonly KernelBuilder _kernelBuilder; + private readonly IKernelBuilder _kernelBuilder; private readonly IConfigurationRoot _configuration; public OpenAICompletionTests(ITestOutputHelper output) @@ -41,7 +41,7 @@ public OpenAICompletionTests(ITestOutputHelper output) .AddUserSecrets() .Build(); - this._kernelBuilder = new KernelBuilder(); + this._kernelBuilder = Kernel.CreateBuilder(); } [Theory(Skip = "OpenAI will often throttle requests. This test is for manual verification.")] @@ -75,7 +75,7 @@ public async Task OpenAIChatAsTextTestAsync(string prompt, string expectedAnswer { // Arrange this._kernelBuilder.Services.AddSingleton(this._logger); - KernelBuilder builder = this._kernelBuilder; + IKernelBuilder builder = this._kernelBuilder; this.ConfigureChatOpenAI(builder); @@ -95,7 +95,7 @@ public async Task CanUseOpenAiChatForTextGenerationAsync() { // Note: we use OpenAI Chat Completion and GPT 3.5 Turbo this._kernelBuilder.Services.AddSingleton(this._logger); - KernelBuilder builder = this._kernelBuilder; + IKernelBuilder builder = this._kernelBuilder; this.ConfigureChatOpenAI(builder); Kernel target = builder.Build(); @@ -212,7 +212,7 @@ public async Task OpenAIHttpRetryPolicyTestAsync(string prompt, string expectedO public async Task AzureOpenAIHttpRetryPolicyTestAsync(string prompt, string expectedOutput) { this._kernelBuilder.Services.AddSingleton(this._testOutputHelper); - KernelBuilder builder = this._kernelBuilder; + IKernelBuilder builder = this._kernelBuilder; var azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get(); Assert.NotNull(azureOpenAIConfiguration); @@ -483,7 +483,7 @@ private void Dispose(bool disposing) } } - private void ConfigureChatOpenAI(KernelBuilder kernelBuilder) + private void ConfigureChatOpenAI(IKernelBuilder kernelBuilder) { var openAIConfiguration = this._configuration.GetSection("OpenAI").Get(); @@ -498,7 +498,7 @@ private void ConfigureChatOpenAI(KernelBuilder kernelBuilder) serviceId: openAIConfiguration.ServiceId); } - private void ConfigureAzureOpenAI(KernelBuilder kernelBuilder) + private void ConfigureAzureOpenAI(IKernelBuilder kernelBuilder) { var azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get(); @@ -515,7 +515,7 @@ private void ConfigureAzureOpenAI(KernelBuilder kernelBuilder) apiKey: azureOpenAIConfiguration.ApiKey, serviceId: azureOpenAIConfiguration.ServiceId); } - private void ConfigureInvalidAzureOpenAI(KernelBuilder kernelBuilder) + private void ConfigureInvalidAzureOpenAI(IKernelBuilder kernelBuilder) { var azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get(); @@ -531,7 +531,7 @@ private void ConfigureInvalidAzureOpenAI(KernelBuilder kernelBuilder) serviceId: $"invalid-{azureOpenAIConfiguration.ServiceId}"); } - private void ConfigureAzureOpenAIChatAsText(KernelBuilder kernelBuilder) + private void ConfigureAzureOpenAIChatAsText(IKernelBuilder kernelBuilder) { var azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get(); diff --git a/dotnet/src/IntegrationTests/Extensions/KernelFunctionExtensionsTests.cs b/dotnet/src/IntegrationTests/Extensions/KernelFunctionExtensionsTests.cs index b04dc38f23f8..0957fd2cb4f3 100644 --- a/dotnet/src/IntegrationTests/Extensions/KernelFunctionExtensionsTests.cs +++ b/dotnet/src/IntegrationTests/Extensions/KernelFunctionExtensionsTests.cs @@ -25,7 +25,7 @@ public KernelFunctionExtensionsTests(ITestOutputHelper output) [Fact] public async Task ItSupportsFunctionCallsAsync() { - var builder = new KernelBuilder(); + var builder = Kernel.CreateBuilder(); builder.Services.AddSingleton(this._logger); builder.Services.AddSingleton(new RedirectTextGenerationService()); builder.Plugins.AddFromType(); @@ -43,7 +43,7 @@ public async Task ItSupportsFunctionCallsAsync() [Fact] public async Task ItSupportsFunctionCallsWithInputAsync() { - var builder = new KernelBuilder(); + var builder = Kernel.CreateBuilder(); builder.Services.AddSingleton(this._logger); builder.Services.AddSingleton(new RedirectTextGenerationService()); builder.Plugins.AddFromType(); diff --git a/dotnet/src/IntegrationTests/Planners/Handlebars/HandlebarsPlannerTests.cs b/dotnet/src/IntegrationTests/Planners/Handlebars/HandlebarsPlannerTests.cs index 257079194372..1d46d95998b9 100644 --- a/dotnet/src/IntegrationTests/Planners/Handlebars/HandlebarsPlannerTests.cs +++ b/dotnet/src/IntegrationTests/Planners/Handlebars/HandlebarsPlannerTests.cs @@ -76,7 +76,7 @@ private Kernel InitializeKernel(bool useEmbeddings = false, bool useChatModel = AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get(); Assert.NotNull(azureOpenAIEmbeddingsConfiguration); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); if (useChatModel) { diff --git a/dotnet/src/IntegrationTests/Planners/PlanTests.cs b/dotnet/src/IntegrationTests/Planners/PlanTests.cs index 23a909fcd235..df329d068085 100644 --- a/dotnet/src/IntegrationTests/Planners/PlanTests.cs +++ b/dotnet/src/IntegrationTests/Planners/PlanTests.cs @@ -562,7 +562,7 @@ private Kernel InitializeKernel(bool useEmbeddings = false, bool useChatModel = AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get(); Assert.NotNull(azureOpenAIEmbeddingsConfiguration); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); if (useChatModel) { diff --git a/dotnet/src/IntegrationTests/Planners/SequentialPlanner/SequentialPlanParserTests.cs b/dotnet/src/IntegrationTests/Planners/SequentialPlanner/SequentialPlanParserTests.cs index 791de0f9f750..da15ecf935ee 100644 --- a/dotnet/src/IntegrationTests/Planners/SequentialPlanner/SequentialPlanParserTests.cs +++ b/dotnet/src/IntegrationTests/Planners/SequentialPlanner/SequentialPlanParserTests.cs @@ -30,7 +30,7 @@ public void CanCallToPlanFromXml() AzureOpenAIConfiguration? azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get(); Assert.NotNull(azureOpenAIConfiguration); - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .WithAzureOpenAITextGeneration( deploymentName: azureOpenAIConfiguration.DeploymentName, endpoint: azureOpenAIConfiguration.Endpoint, diff --git a/dotnet/src/IntegrationTests/Planners/SequentialPlanner/SequentialPlannerTests.cs b/dotnet/src/IntegrationTests/Planners/SequentialPlanner/SequentialPlannerTests.cs index b102aff9e960..7eef3864cf8c 100644 --- a/dotnet/src/IntegrationTests/Planners/SequentialPlanner/SequentialPlannerTests.cs +++ b/dotnet/src/IntegrationTests/Planners/SequentialPlanner/SequentialPlannerTests.cs @@ -114,7 +114,7 @@ private Kernel InitializeKernel(bool useEmbeddings = false, bool useChatModel = AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get(); Assert.NotNull(azureOpenAIEmbeddingsConfiguration); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); if (useChatModel) { diff --git a/dotnet/src/IntegrationTests/Planners/StepwisePlanner/FunctionCallingStepwisePlannerTests.cs b/dotnet/src/IntegrationTests/Planners/StepwisePlanner/FunctionCallingStepwisePlannerTests.cs index 520cbd76f323..f4e743fdb989 100644 --- a/dotnet/src/IntegrationTests/Planners/StepwisePlanner/FunctionCallingStepwisePlannerTests.cs +++ b/dotnet/src/IntegrationTests/Planners/StepwisePlanner/FunctionCallingStepwisePlannerTests.cs @@ -71,7 +71,7 @@ private Kernel InitializeKernel(bool useEmbeddings = false) AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get(); Assert.NotNull(azureOpenAIEmbeddingsConfiguration); - var builder = new KernelBuilder() + var builder = Kernel.CreateBuilder() .WithAzureOpenAIChatCompletion( deploymentName: azureOpenAIConfiguration.ChatDeploymentName!, endpoint: azureOpenAIConfiguration.Endpoint, diff --git a/dotnet/src/IntegrationTests/Planners/StepwisePlanner/StepwisePlannerTests.cs b/dotnet/src/IntegrationTests/Planners/StepwisePlanner/StepwisePlannerTests.cs index ec4d6f33172e..62556eb21670 100644 --- a/dotnet/src/IntegrationTests/Planners/StepwisePlanner/StepwisePlannerTests.cs +++ b/dotnet/src/IntegrationTests/Planners/StepwisePlanner/StepwisePlannerTests.cs @@ -143,7 +143,7 @@ private Kernel InitializeKernel(bool useEmbeddings = false, bool useChatModel = AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get(); Assert.NotNull(azureOpenAIEmbeddingsConfiguration); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); if (useChatModel) { diff --git a/dotnet/src/IntegrationTests/PromptTests.cs b/dotnet/src/IntegrationTests/PromptTests.cs index a806683a5b06..2dcff295ae8f 100644 --- a/dotnet/src/IntegrationTests/PromptTests.cs +++ b/dotnet/src/IntegrationTests/PromptTests.cs @@ -32,7 +32,7 @@ public PromptTests(ITestOutputHelper output) .AddUserSecrets() .Build(); - this._kernelBuilder = new KernelBuilder(); + this._kernelBuilder = Kernel.CreateBuilder(); this._kernelBuilder.Services.AddSingleton(this._logger); } @@ -64,7 +64,7 @@ public async Task GenerateStoryTestAsync(string resourceName, bool isHandlebars) #region private methods - private readonly KernelBuilder _kernelBuilder; + private readonly IKernelBuilder _kernelBuilder; private readonly IConfigurationRoot _configuration; private readonly XunitLogger _logger; private readonly RedirectOutput _testOutputHelper; @@ -89,7 +89,7 @@ private void Dispose(bool disposing) } } - private void ConfigureAzureOpenAI(KernelBuilder kernelBuilder) + private void ConfigureAzureOpenAI(IKernelBuilder kernelBuilder) { var azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get(); diff --git a/dotnet/src/SemanticKernel.Abstractions/IKernelBuilder.cs b/dotnet/src/SemanticKernel.Abstractions/IKernelBuilder.cs index 6a356916b2d7..4e9fffd1aab6 100644 --- a/dotnet/src/SemanticKernel.Abstractions/IKernelBuilder.cs +++ b/dotnet/src/SemanticKernel.Abstractions/IKernelBuilder.cs @@ -12,14 +12,6 @@ public interface IKernelBuilder /// Gets a builder for adding collections as singletons to . IKernelBuilderPlugins Plugins { get; } - - /// Constructs a new instance of using all of the settings configured on the builder. - /// The new instance. - /// - /// Every call to produces a new instance. The resulting - /// instances will not share the same plugins collection or services provider (unless there are no services). - /// - Kernel Build(); } /// Provides a builder for adding plugins as singletons to a service collection. diff --git a/dotnet/src/SemanticKernel.Abstractions/Kernel.cs b/dotnet/src/SemanticKernel.Abstractions/Kernel.cs index 818d4be2bc83..7ffba9e27dab 100644 --- a/dotnet/src/SemanticKernel.Abstractions/Kernel.cs +++ b/dotnet/src/SemanticKernel.Abstractions/Kernel.cs @@ -70,6 +70,10 @@ public Kernel( } } + /// Creates a builder for constructing instances. + /// A new instance. + public static IKernelBuilder CreateBuilder() => new KernelBuilder(); + /// /// Clone the object to create a new instance that may be mutated without affecting the current instance. /// diff --git a/dotnet/src/SemanticKernel.Abstractions/KernelBuilder.cs b/dotnet/src/SemanticKernel.Abstractions/KernelBuilder.cs new file mode 100644 index 000000000000..a792944bab82 --- /dev/null +++ b/dotnet/src/SemanticKernel.Abstractions/KernelBuilder.cs @@ -0,0 +1,39 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.Extensions.DependencyInjection; + +namespace Microsoft.SemanticKernel; + +/// Provides a builder for constructing instances of . +internal sealed class KernelBuilder : IKernelBuilder, IKernelBuilderPlugins +{ + /// The collection of services to be available through the . + private IServiceCollection? _services; + + /// Initializes a new instance of the . + public KernelBuilder() + { + this.AllowBuild = true; + } + + /// Initializes a new instance of the . + /// + /// The to wrap and use for building the . + /// + public KernelBuilder(IServiceCollection services) + { + Verify.NotNull(services); + + this._services = services; + } + + /// Whether to allow a call to Build. + /// As a minor aid to help avoid misuse, we try to prevent Build from being called on instances returned from AddKernel. + internal bool AllowBuild { get; } + + /// Gets the collection of services to be built into the . + public IServiceCollection Services => this._services ??= new ServiceCollection(); + + /// Gets a builder for plugins to be built as services into the . + public IKernelBuilderPlugins Plugins => this; +} diff --git a/dotnet/src/SemanticKernel.Abstractions/Services/KernelServiceCollectionExtensions.cs b/dotnet/src/SemanticKernel.Abstractions/Services/KernelServiceCollectionExtensions.cs new file mode 100644 index 000000000000..79a6f6116e9a --- /dev/null +++ b/dotnet/src/SemanticKernel.Abstractions/Services/KernelServiceCollectionExtensions.cs @@ -0,0 +1,35 @@ +// Copyright (c) Microsoft. All rights reserved. + +using Microsoft.SemanticKernel; + +namespace Microsoft.Extensions.DependencyInjection; + +/// Extension methods for interacting with . +public static class KernelServiceCollectionExtensions +{ + /// Adds a and services to the services collection. + /// The service collection. + /// + /// A that can be used to add additional services to the same . + /// + /// + /// Both services are registered as transient, as both objects are mutable. + /// + public static IKernelBuilder AddKernel(this IServiceCollection services) + { + Verify.NotNull(services); + + // Register a KernelPluginCollection to be populated with any IKernelPlugins that have been + // directly registered in DI. It's transient because the Kernel will store the collection + // directly, and we don't want two Kernel instances to hold on to the same mutable collection. + services.AddTransient(); + + // Register the Kernel as transient. It's mutable and expected to be mutated by consumers, + // such as via adding event handlers, adding plugins, storing state in its Data collection, etc. + services.AddTransient(); + + // Create and return a builder that can be used for adding services and plugins + // to the IServiceCollection. + return new KernelBuilder(services); + } +} diff --git a/dotnet/src/SemanticKernel.Core/KernelBuilder.cs b/dotnet/src/SemanticKernel.Core/KernelBuilder.cs deleted file mode 100644 index 4d6002d2b834..000000000000 --- a/dotnet/src/SemanticKernel.Core/KernelBuilder.cs +++ /dev/null @@ -1,100 +0,0 @@ -// Copyright (c) Microsoft. All rights reserved. - -using System; -using System.Collections.Generic; -using Microsoft.Extensions.DependencyInjection; - -namespace Microsoft.SemanticKernel; - -/// Provides a builder for constructing instances of . -public sealed class KernelBuilder : IKernelBuilder -{ - /// Whether to allow . - /// - /// When the is directly created by the user, this is true. - /// When it's created by over an existing - /// , we currently don't want to allow - /// to call BuildServiceProvider, as that can lead to confusion around how other services - /// registered in that service collection behave. - /// - private readonly bool _supportsBuild = true; - /// The collection of services to be available through the . - private IServiceCollection? _services; - /// A facade on top of for adding plugins to the services collection. - private KernelBuilderPlugins? _plugins; - - /// Initializes a new instance of the . - public KernelBuilder() { } - - /// Initializes a new instance of the . - /// - /// The to wrap and use for building the . - /// - /// - /// As the service collection is externally provided, is disabled - /// to avoid unexpected confusion with multiple builds of the same service collection. - /// - internal KernelBuilder(IServiceCollection services) - { - Verify.NotNull(services); - - this._services = services; - this._supportsBuild = false; - } - - /// Gets the collection of services to be built into the . - public IServiceCollection Services => this._services ??= new ServiceCollection(); - - /// Gets a builder for plugins to be built as services into the . - public IKernelBuilderPlugins Plugins => this._plugins ??= new(this.Services); - - /// Constructs a new instance of using all of the settings configured on the builder. - /// The new instance. - /// - /// Every call to produces a new instance. The resulting - /// instances will not share the same plugins collection or services provider (unless there are no services). - /// - public Kernel Build() - { - if (!this._supportsBuild) - { - throw new InvalidOperationException("Build is not supported on this instance. Use BuildServiceProvider on the original IServiceCollection."); - } - - IServiceProvider serviceProvider = EmptyServiceProvider.Instance; - if (this._services is { Count: > 0 } services) - { - // This is a workaround for Microsoft.Extensions.DependencyInjection's GetKeyedServices not currently supporting - // enumerating all services for a given type regardless of key. - // https://github.com/dotnet/runtime/issues/91466 - // We need this support to, for example, allow IServiceSelector to pick from multiple named instances of an AI - // service based on their characteristics. Until that is addressed, we work around it by injecting as a service all - // of the keys used for a given type, such that Kernel can then query for this dictionary and enumerate it. This means - // that such functionality will work when KernelBuilder is used to build the kernel but not when the IServiceProvider - // is created via other means, such as if Kernel is directly created by DI. However, it allows us to create the APIs - // the way we want them for the longer term and then subsequently fix the implementation when M.E.DI is fixed. - Dictionary> typeToKeyMappings = new(); - foreach (ServiceDescriptor serviceDescriptor in services) - { - if (!typeToKeyMappings.TryGetValue(serviceDescriptor.ServiceType, out HashSet? keys)) - { - typeToKeyMappings[serviceDescriptor.ServiceType] = keys = new(); - } - - keys.Add(serviceDescriptor.ServiceKey); - } - services.AddKeyedSingleton(Kernel.KernelServiceTypeToKeyMappings, typeToKeyMappings); - - serviceProvider = services.BuildServiceProvider(); - } - - return new Kernel(serviceProvider); - } - - private sealed class KernelBuilderPlugins : IKernelBuilderPlugins - { - public KernelBuilderPlugins(IServiceCollection services) => this.Services = services; - - public IServiceCollection Services { get; } - } -} diff --git a/dotnet/src/SemanticKernel.Core/KernelExtensions.cs b/dotnet/src/SemanticKernel.Core/KernelExtensions.cs index 972bbd3e0155..8d507227e12e 100644 --- a/dotnet/src/SemanticKernel.Core/KernelExtensions.cs +++ b/dotnet/src/SemanticKernel.Core/KernelExtensions.cs @@ -575,34 +575,52 @@ public static IAsyncEnumerable InvokePromptStreamingAsync( } #endregion - #region AddKernel for IServiceCollection - /// Adds a and services to the services collection. - /// The service collection. - /// - /// A that can be used to add additional services to the same . - /// + #region Build for IKernelBuilder + /// Constructs a new instance of using all of the settings configured on the builder. + /// The new instance. /// - /// Both services are registered as transient, as both objects are mutable. - /// The builder returned from this method may be used to add additional plugins and services, - /// but it may not be used for : doing so would build the - /// entire service provider from . + /// Every call to produces a new instance. The resulting + /// instances will not share the same plugins collection or services provider (unless there are no services). /// - public static IKernelBuilder AddKernel(this IServiceCollection services) + public static Kernel Build(this IKernelBuilder builder) { - Verify.NotNull(services); + Verify.NotNull(builder); - // Register a KernelPluginCollection to be populated with any IKernelPlugins that have been - // directly registered in DI. It's transient because the Kernel will store the collection - // directly, and we don't want two Kernel instances to hold on to the same mutable collection. - services.AddTransient(); + if (builder is KernelBuilder kb && !kb.AllowBuild) + { + throw new InvalidOperationException( + "Build is not permitted on instances returned from AddKernel. " + + "Resolve the Kernel from the service provider."); + } + + IServiceProvider serviceProvider = EmptyServiceProvider.Instance; + if (builder.Services is { Count: > 0 } services) + { + // This is a workaround for Microsoft.Extensions.DependencyInjection's GetKeyedServices not currently supporting + // enumerating all services for a given type regardless of key. + // https://github.com/dotnet/runtime/issues/91466 + // We need this support to, for example, allow IServiceSelector to pick from multiple named instances of an AI + // service based on their characteristics. Until that is addressed, we work around it by injecting as a service all + // of the keys used for a given type, such that Kernel can then query for this dictionary and enumerate it. This means + // that such functionality will work when KernelBuilder is used to build the kernel but not when the IServiceProvider + // is created via other means, such as if Kernel is directly created by DI. However, it allows us to create the APIs + // the way we want them for the longer term and then subsequently fix the implementation when M.E.DI is fixed. + Dictionary> typeToKeyMappings = new(); + foreach (ServiceDescriptor serviceDescriptor in services) + { + if (!typeToKeyMappings.TryGetValue(serviceDescriptor.ServiceType, out HashSet? keys)) + { + typeToKeyMappings[serviceDescriptor.ServiceType] = keys = new(); + } + + keys.Add(serviceDescriptor.ServiceKey); + } + services.AddKeyedSingleton(Kernel.KernelServiceTypeToKeyMappings, typeToKeyMappings); - // Register the Kernel as transient. It's mutable and expected to be mutated by consumers, - // such as via adding event handlers, adding plugins, storing state in its Data collection, etc. - services.AddTransient(); + serviceProvider = services.BuildServiceProvider(); + } - // Create and return a builder that can be used for adding services and plugins - // to the IServiceCollection. - return new KernelBuilder(services); + return new Kernel(serviceProvider); } #endregion } diff --git a/dotnet/src/SemanticKernel.UnitTests/Functions/KernelBuilderTests.cs b/dotnet/src/SemanticKernel.UnitTests/Functions/KernelBuilderTests.cs index 2be81334fc89..f17ccd29f5d8 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Functions/KernelBuilderTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Functions/KernelBuilderTests.cs @@ -18,14 +18,14 @@ public class KernelBuilderTests [Fact] public void ItCreatesNewKernelsOnEachBuild() { - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); Assert.NotSame(builder.Build(), builder.Build()); } [Fact] public void ItHasIdempotentServicesAndPlugins() { - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); Assert.NotNull(builder.Services); Assert.NotNull(builder.Plugins); @@ -44,14 +44,14 @@ public void ItHasIdempotentServicesAndPlugins() [Fact] public void ItDefaultsDataToAnEmptyDictionary() { - Kernel kernel = new KernelBuilder().Build(); + Kernel kernel = Kernel.CreateBuilder().Build(); Assert.Empty(kernel.Data); } [Fact] public void ItDefaultsServiceSelectorToSingleton() { - Kernel kernel = new KernelBuilder().Build(); + Kernel kernel = Kernel.CreateBuilder().Build(); Assert.Null(kernel.Services.GetService()); Assert.NotNull(kernel.ServiceSelector); Assert.Same(kernel.ServiceSelector, kernel.ServiceSelector); @@ -65,7 +65,7 @@ public void ItDefaultsServiceSelectorToSingleton() NopServiceSelector selector = new(); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddSingleton(selector); kernel = builder.Build(); Assert.Same(selector, kernel.Services.GetService()); @@ -88,7 +88,7 @@ public void ItPropagatesPluginsToBuiltKernel() KernelPlugin plugin1 = KernelPluginFactory.CreateFromFunctions("plugin1"); KernelPlugin plugin2 = KernelPluginFactory.CreateFromFunctions("plugin2"); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Plugins.Add(plugin1); builder.Plugins.Add(plugin2); Kernel kernel = builder.Build(); @@ -100,14 +100,14 @@ public void ItPropagatesPluginsToBuiltKernel() [Fact] public void ItSuppliesServicesCollectionToPluginsBuilder() { - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); Assert.Same(builder.Services, builder.Plugins.Services); } [Fact] public void ItBuildsServicesIntoKernel() { - var builder = new KernelBuilder() + var builder = Kernel.CreateBuilder() .AddOpenAIChatCompletion(modelId: "abcd", apiKey: "efg", serviceId: "openai") .AddAzureOpenAITextGeneration(deploymentName: "hijk", modelId: "qrs", endpoint: "https://lmnop", apiKey: "tuv", serviceId: "azureopenai"); @@ -129,7 +129,7 @@ public void ItBuildsServicesIntoKernel() [Fact] public void ItSupportsMultipleEqualNamedServices() { - Kernel kernel = new KernelBuilder() + Kernel kernel = Kernel.CreateBuilder() .AddOpenAIChatCompletion(modelId: "abcd", apiKey: "efg", serviceId: "openai") .AddOpenAIChatCompletion(modelId: "abcd", apiKey: "efg", serviceId: "openai") .AddOpenAIChatCompletion(modelId: "abcd", apiKey: "efg", serviceId: "openai") @@ -197,10 +197,10 @@ public void ItFindsAllPluginsToPopulatePluginsCollection() KernelPlugin plugin2 = KernelPluginFactory.CreateFromFunctions("plugin2"); KernelPlugin plugin3 = KernelPluginFactory.CreateFromFunctions("plugin3"); - KernelBuilder builder = new(); - builder.Services.AddSingleton(plugin1); - builder.Services.AddSingleton(plugin2); - builder.Services.AddSingleton(plugin3); + IKernelBuilder builder = Kernel.CreateBuilder(); + builder.Services.AddSingleton(plugin1); + builder.Services.AddSingleton(plugin2); + builder.Services.AddSingleton(plugin3); Kernel kernel = builder.Build(); Assert.Equal(3, kernel.Plugins.Count); @@ -213,7 +213,7 @@ public void ItFindsPluginCollectionToUse() KernelPlugin plugin2 = KernelPluginFactory.CreateFromFunctions("plugin2"); KernelPlugin plugin3 = KernelPluginFactory.CreateFromFunctions("plugin3"); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddTransient(_ => new(new[] { plugin1, plugin2, plugin3 })); Kernel kernel1 = builder.Build(); @@ -224,4 +224,22 @@ public void ItFindsPluginCollectionToUse() Assert.NotSame(kernel1.Plugins, kernel2.Plugins); } + + [Fact] + public void ItAddsTheRightTypesInAddKernel() + { + IServiceCollection sc = new ServiceCollection(); + + IKernelBuilder builder = sc.AddKernel(); + Assert.NotNull(builder); + Assert.Throws(() => builder.Build()); + + builder.Services.AddSingleton>(new Dictionary()); + + IServiceProvider provider = sc.BuildServiceProvider(); + + Assert.NotNull(provider.GetService>()); + Assert.NotNull(provider.GetService()); + Assert.NotNull(provider.GetService()); + } } diff --git a/dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromPromptTests.cs b/dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromPromptTests.cs index 2fc28b1e2457..ee1587700bd7 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromPromptTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Functions/KernelFunctionFromPromptTests.cs @@ -24,7 +24,7 @@ public void ItProvidesAccessToFunctionsViaFunctionCollection() { // Arrange var factory = new Mock>(); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddSingleton(factory.Object); Kernel kernel = builder.Build(); @@ -46,7 +46,7 @@ public async Task ItUsesChatSystemPromptWhenProvidedAsync(string providedSystemC mockTextGeneration.Setup(c => c.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new[] { fakeTextContent }); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("x", mockTextGeneration.Object); Kernel kernel = builder.Build(); @@ -77,7 +77,7 @@ public async Task ItUsesServiceIdWhenProvidedAsync() mockTextGeneration1.Setup(c => c.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new[] { fakeTextContent }); mockTextGeneration2.Setup(c => c.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new[] { fakeTextContent }); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", mockTextGeneration1.Object); builder.Services.AddKeyedSingleton("service2", mockTextGeneration2.Object); Kernel kernel = builder.Build(); @@ -102,7 +102,7 @@ public async Task ItFailsIfInvalidServiceIdIsProvidedAsync() var mockTextGeneration1 = new Mock(); var mockTextGeneration2 = new Mock(); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", mockTextGeneration1.Object); builder.Services.AddKeyedSingleton("service2", mockTextGeneration2.Object); Kernel kernel = builder.Build(); @@ -123,7 +123,7 @@ public async Task ItFailsIfInvalidServiceIdIsProvidedAsync() public async Task ItParsesStandardizedPromptWhenServiceIsChatCompletionAsync() { var fakeService = new FakeChatAsTextService(); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddTransient((sp) => fakeService); Kernel kernel = builder.Build(); @@ -145,7 +145,7 @@ public async Task ItParsesStandardizedPromptWhenServiceIsChatCompletionAsync() public async Task ItParsesStandardizedPromptWhenServiceIsStreamingChatCompletionAsync() { var fakeService = new FakeChatAsTextService(); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddTransient((sp) => fakeService); Kernel kernel = builder.Build(); @@ -172,7 +172,7 @@ public async Task ItNotParsesStandardizedPromptWhenServiceIsOnlyTextCompletionAs var mockResult = mockService.Setup(s => s.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(new List() { new("something") }); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddTransient((sp) => mockService.Object); Kernel kernel = builder.Build(); @@ -200,7 +200,7 @@ public async Task ItNotParsesStandardizedPromptWhenStreamingWhenServiceIsOnlyTex var mockResult = mockService.Setup(s => s.GetStreamingTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(this.ToAsyncEnumerable(new List() { new("something") })); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddTransient((sp) => mockService.Object); Kernel kernel = builder.Build(); diff --git a/dotnet/src/SemanticKernel.UnitTests/Functions/MultipleModelTests.cs b/dotnet/src/SemanticKernel.UnitTests/Functions/MultipleModelTests.cs index 25b8eec8c8f8..28073e99ae4a 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Functions/MultipleModelTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Functions/MultipleModelTests.cs @@ -23,7 +23,7 @@ public async Task ItUsesServiceIdWhenProvidedAsync() mockTextGeneration1.Setup(c => c.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new[] { fakeTextContent }); mockTextGeneration2.Setup(c => c.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new[] { fakeTextContent }); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", mockTextGeneration1.Object); builder.Services.AddKeyedSingleton("service2", mockTextGeneration2.Object); Kernel kernel = builder.Build(); @@ -48,7 +48,7 @@ public async Task ItFailsIfInvalidServiceIdIsProvidedAsync() var mockTextGeneration1 = new Mock(); var mockTextGeneration2 = new Mock(); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", mockTextGeneration1.Object); builder.Services.AddKeyedSingleton("service2", mockTextGeneration2.Object); Kernel kernel = builder.Build(); @@ -80,7 +80,7 @@ public async Task ItUsesServiceIdByOrderAsync(string[] serviceIds, int[] callCou mockTextGeneration2.Setup(c => c.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new[] { fakeTextContent }); mockTextGeneration3.Setup(c => c.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new[] { fakeTextContent }); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", mockTextGeneration1.Object); builder.Services.AddKeyedSingleton("service2", mockTextGeneration2.Object); builder.Services.AddKeyedSingleton("service3", mockTextGeneration3.Object); @@ -116,7 +116,7 @@ public async Task ItUsesServiceIdWithJsonPromptTemplateConfigAsync() mockTextGeneration2.Setup(c => c.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new[] { fakeTextContent }); mockTextGeneration3.Setup(c => c.GetTextContentsAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(new[] { fakeTextContent }); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", mockTextGeneration1.Object); builder.Services.AddKeyedSingleton("service2", mockTextGeneration2.Object); builder.Services.AddKeyedSingleton("service3", mockTextGeneration3.Object); diff --git a/dotnet/src/SemanticKernel.UnitTests/Functions/OrderedAIServiceConfigurationProviderTests.cs b/dotnet/src/SemanticKernel.UnitTests/Functions/OrderedAIServiceConfigurationProviderTests.cs index 127975575d4d..73e852882d72 100644 --- a/dotnet/src/SemanticKernel.UnitTests/Functions/OrderedAIServiceConfigurationProviderTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/Functions/OrderedAIServiceConfigurationProviderTests.cs @@ -31,7 +31,7 @@ public void ItThrowsAnSKExceptionForNoServices() public void ItGetsAIServiceConfigurationForSingleAIService() { // Arrange - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", new AIService()); Kernel kernel = builder.Build(); @@ -50,7 +50,7 @@ public void ItGetsAIServiceConfigurationForSingleAIService() public void ItGetsAIServiceConfigurationForSingleTextGeneration() { // Arrange - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", new TextGenerationService("model_id_1")); Kernel kernel = builder.Build(); @@ -69,7 +69,7 @@ public void ItGetsAIServiceConfigurationForSingleTextGeneration() public void ItGetsAIServiceConfigurationForTextGenerationByServiceId() { // Arrange - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", new TextGenerationService("model_id_1")); builder.Services.AddKeyedSingleton("service2", new TextGenerationService("model_id_2")); Kernel kernel = builder.Build(); @@ -90,7 +90,7 @@ public void ItGetsAIServiceConfigurationForTextGenerationByServiceId() public void ItThrowsAnSKExceptionForNotFoundService() { // Arrange - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", new TextGenerationService("model_id_1")); builder.Services.AddKeyedSingleton("service2", new TextGenerationService("model_id_2")); Kernel kernel = builder.Build(); @@ -108,7 +108,7 @@ public void ItThrowsAnSKExceptionForNotFoundService() public void ItUsesDefaultServiceForNoExecutionSettings() { // Arrange - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", new TextGenerationService("model_id_1")); builder.Services.AddKeyedSingleton("service2", new TextGenerationService("model_id_2")); Kernel kernel = builder.Build(); @@ -127,7 +127,7 @@ public void ItUsesDefaultServiceForNoExecutionSettings() public void ItUsesDefaultServiceAndSettingsForDefaultExecutionSettings() { // Arrange - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", new TextGenerationService("model_id_1")); builder.Services.AddKeyedSingleton("service2", new TextGenerationService("model_id_2")); Kernel kernel = builder.Build(); @@ -148,7 +148,7 @@ public void ItUsesDefaultServiceAndSettingsForDefaultExecutionSettings() public void ItUsesDefaultServiceAndSettingsEmptyServiceId() { // Arrange - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", new TextGenerationService("model_id_1")); builder.Services.AddKeyedSingleton("service2", new TextGenerationService("model_id_2")); Kernel kernel = builder.Build(); @@ -174,7 +174,7 @@ public void ItUsesDefaultServiceAndSettingsEmptyServiceId() public void ItGetsAIServiceConfigurationByOrder(string[] serviceIds, string expectedServiceId) { // Arrange - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton("service1", new TextGenerationService("model_id_1")); builder.Services.AddKeyedSingleton("service2", new TextGenerationService("model_id_2")); builder.Services.AddKeyedSingleton("service3", new TextGenerationService("model_id_3")); @@ -203,7 +203,7 @@ public void ItGetsAIServiceConfigurationByOrder(string[] serviceIds, string expe public void ItGetsAIServiceConfigurationForTextGenerationByModelId() { // Arrange - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddKeyedSingleton(null, new TextGenerationService("model1")); builder.Services.AddKeyedSingleton(null, new TextGenerationService("model2")); Kernel kernel = builder.Build(); diff --git a/dotnet/src/SemanticKernel.UnitTests/KernelTests.cs b/dotnet/src/SemanticKernel.UnitTests/KernelTests.cs index b14bf0b55ecb..7c00e124b090 100644 --- a/dotnet/src/SemanticKernel.UnitTests/KernelTests.cs +++ b/dotnet/src/SemanticKernel.UnitTests/KernelTests.cs @@ -320,7 +320,7 @@ public async Task InvokeAsyncHandlesPostInvocationWithServicesAsync() { // Arrange var (mockTextResult, mockTextCompletion) = this.SetupMocks(); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddSingleton(mockTextCompletion.Object); Kernel kernel = builder.Build(); @@ -617,7 +617,7 @@ public async Task InvokeStreamingAsyncCallsConnectorStreamingApiAsync() var mockTextCompletion = this.SetupStreamingMocks( new StreamingTextContent("chunk1"), new StreamingTextContent("chunk2")); - KernelBuilder builder = new(); + IKernelBuilder builder = Kernel.CreateBuilder(); builder.Services.AddSingleton(mockTextCompletion.Object); Kernel kernel = builder.Build(); var prompt = "Write a simple phrase about UnitTests {{$input}}";