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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

tanaka-takayoshi
Copy link

Fixed the secret section name.

Motivation and Context

Fixing #10049

Description

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

Contribution Checklist

@tanaka-takayoshi tanaka-takayoshi requested a review from a team as a code owner January 3, 2025 01:24
@markwallace-microsoft markwallace-microsoft added the .NET Issue or Pull requests regarding .NET code label Jan 3, 2025
@github-actions github-actions bot changed the title Update Program.cs .Net: Update Program.cs Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the SectionName property/field was missing in the AzureOpenAIOptions and OpenAIOptions classes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the field in line with another demo project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tanaka-takayoshi Consider changing the Program.cs to something like this.

Adding the namespaces and UserSecrets support and making it AzureOpenAI friendly right out of the bat.

Namespaces

// For Azure OpenAI configuration
#pragma warning disable IDE0005 // Using directive is unnecessary.
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using Microsoft.SemanticKernel.Connectors.OpenAI;

Main (Loading both OpenAI and AzureOpenAI (commented) options)

 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(AzureOpenAIOptions.SectionName))
                 .ValidateDataAnnotations()
                 .ValidateOnStart();
 */

 // Chat completion service that kernels will use
 builder.Services.AddSingleton<IChatCompletionService>(sp =>
 {
     OpenAIOptions openAIOptions = sp.GetRequiredService<IOptions<OpenAIOptions>>().Value;

     // A custom HttpClient can be provided to this constructor
     return new OpenAIChatCompletionService(openAIOptions.ChatModelId, openAIOptions.ApiKey);

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

     AzureOpenAIOptions azureOpenAIOptions = sp.GetRequiredService<IOptions<AzureOpenAIOptions>>().Value;
     return new AzureOpenAIChatCompletionService(azureOpenAIOptions.ChatDeploymentName, azureOpenAIOptions.Endpoint, azureOpenAIOptions.ApiKey);

     */
 });

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget also to change the dependency in the HomeAutomation.csproj from OpenAI to AzureOpenAI csproj.

- <ProjectReference Include="..\..\..\src\Connectors\Connectors.OpenAI\Connectors.OpenAI.csproj" />
+ <ProjectReference Include="..\..\..\src\Connectors\Connectors.AzureOpenAI\Connectors.AzureOpenAI.csproj" />

Fixed the secret section name.
@RogerBarreto RogerBarreto changed the title .Net: Update Program.cs .Net: HomeAutomation AzureOpenAI Configuration Fix Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.NET Issue or Pull requests regarding .NET code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants