diff --git a/dotnet/samples/Demos/HomeAutomation/HomeAutomation.csproj b/dotnet/samples/Demos/HomeAutomation/HomeAutomation.csproj index bf825d89e087..39b79bd8b820 100644 --- a/dotnet/samples/Demos/HomeAutomation/HomeAutomation.csproj +++ b/dotnet/samples/Demos/HomeAutomation/HomeAutomation.csproj @@ -15,6 +15,7 @@ + diff --git a/dotnet/samples/Demos/HomeAutomation/Program.cs b/dotnet/samples/Demos/HomeAutomation/Program.cs index 3c4ec900731e..3b8d1f009c2f 100644 --- a/dotnet/samples/Demos/HomeAutomation/Program.cs +++ b/dotnet/samples/Demos/HomeAutomation/Program.cs @@ -13,11 +13,15 @@ Example that demonstrates how to use Semantic Kernel in conjunction with depende using HomeAutomation.Options; using HomeAutomation.Plugins; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.ChatCompletion; +// For Azure OpenAI configuration +#pragma warning disable IDE0005 // Using directive is unnecessary. +using Microsoft.SemanticKernel.Connectors.AzureOpenAI; using Microsoft.SemanticKernel.Connectors.OpenAI; namespace HomeAutomation; @@ -27,30 +31,40 @@ internal static class Program internal static async Task Main(string[] args) { HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); + builder.Configuration.AddUserSecrets(); // Actual code to execute is found in Worker class builder.Services.AddHostedService(); // Get configuration + builder.Services.AddOptions() + .Bind(builder.Configuration.GetSection(OpenAIOptions.SectionName)) + .ValidateDataAnnotations() + .ValidateOnStart(); + + /* Alternatively, you can use plain, Azure OpenAI after loading AzureOpenAIOptions instead of OpenAI + builder.Services.AddOptions() .Bind(builder.Configuration.GetSection(AzureOpenAIOptions.SectionName)) .ValidateDataAnnotations() .ValidateOnStart(); + */ // Chat completion service that kernels will use builder.Services.AddSingleton(sp => { - OpenAIOptions options = sp.GetRequiredService>().Value; + OpenAIOptions openAIOptions = sp.GetRequiredService>().Value; // A custom HttpClient can be provided to this constructor - return new OpenAIChatCompletionService(options.ChatModelId, options.ApiKey); + return new OpenAIChatCompletionService(openAIOptions.ChatModelId, openAIOptions.ApiKey); /* Alternatively, you can use plain, Azure OpenAI after loading AzureOpenAIOptions instead of OpenAI options with builder.Services.AddOptions: + + AzureOpenAIOptions azureOpenAIOptions = sp.GetRequiredService>().Value; + return new AzureOpenAIChatCompletionService(azureOpenAIOptions.ChatDeploymentName, azureOpenAIOptions.Endpoint, azureOpenAIOptions.ApiKey); - AzureOpenAIOptions options = sp.GetRequiredService>().Value; - - return new AzureOpenAIChatCompletionService(options.ChatDeploymentName, options.Endpoint, options.ApiKey); */ + */ }); // Add plugins that can be used by kernels