Skip to content

Commit

Permalink
.Net: HomeAutomation AzureOpenAI Configuration Fix (#10054)
Browse files Browse the repository at this point in the history
Fixed the secret section name.

### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

Fixing #10049

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

Fix the secret section name to `AzureOpenAI`, described in README.

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [X] The code builds clean without any errors or warnings
- [X] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [NA] All unit tests pass, and I have added new tests where possible
- [X] I didn't break anyone 😄

---------

Co-authored-by: Roger Barreto <[email protected]>
  • Loading branch information
tanaka-takayoshi and RogerBarreto authored Jan 6, 2025
1 parent 81a230f commit 5b97ad1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
1 change: 1 addition & 0 deletions dotnet/samples/Demos/HomeAutomation/HomeAutomation.csproj
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
2 changes: 2 additions & 0 deletions dotnet/samples/Demos/HomeAutomation/Options/OpenAIOptions.cs
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
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

0 comments on commit 5b97ad1

Please sign in to comment.