Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: HomeAutomation AzureOpenAI Configuration Fix #10054

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Connectors\Connectors.AzureOpenAI\Connectors.AzureOpenAI.csproj" />
<ProjectReference Include="..\..\..\src\Connectors\Connectors.OpenAI\Connectors.OpenAI.csproj" />
<ProjectReference Include="..\..\..\src\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace HomeAutomation.Options;
/// </summary>
public sealed class AzureOpenAIOptions
{
public const string SectionName = "AzureOpenAI";

[Required]
public string ChatDeploymentName { get; set; } = string.Empty;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace HomeAutomation.Options;
/// </summary>
public sealed class OpenAIOptions
{
public const string SectionName = "OpenAI";

[Required]
public string ChatModelId { get; set; } = string.Empty;

Expand Down
26 changes: 20 additions & 6 deletions dotnet/samples/Demos/HomeAutomation/Program.cs
RogerBarreto marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,30 +31,40 @@ internal static class Program
internal static async Task Main(string[] args)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Configuration.AddUserSecrets<Worker>();

// Actual code to execute is found in Worker class
builder.Services.AddHostedService<Worker>();

// Get configuration
builder.Services.AddOptions<OpenAIOptions>()
.Bind(builder.Configuration.GetSection(OpenAIOptions.SectionName))
.ValidateDataAnnotations()
.ValidateOnStart();

/* Alternatively, you can use plain, Azure OpenAI after loading AzureOpenAIOptions instead of OpenAI

builder.Services.AddOptions<AzureOpenAIOptions>()
.Bind(builder.Configuration.GetSection(nameof(AzureOpenAIOptions)))
.Bind(builder.Configuration.GetSection(AzureOpenAIOptions.SectionName))
.ValidateDataAnnotations()
.ValidateOnStart();
*/

// Chat completion service that kernels will use
builder.Services.AddSingleton<IChatCompletionService>(sp =>
{
OpenAIOptions options = sp.GetRequiredService<IOptions<OpenAIOptions>>().Value;
OpenAIOptions openAIOptions = sp.GetRequiredService<IOptions<OpenAIOptions>>().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<IOptions<AzureOpenAIOptions>>().Value;
return new AzureOpenAIChatCompletionService(azureOpenAIOptions.ChatDeploymentName, azureOpenAIOptions.Endpoint, azureOpenAIOptions.ApiKey);

AzureOpenAIOptions options = sp.GetRequiredService<IOptions<AzureOpenAI>>().Value;

return new AzureOpenAIChatCompletionService(options.ChatDeploymentName, options.Endpoint, options.ApiKey); */
*/
});

// Add plugins that can be used by kernels
Expand Down
Loading