Skip to content

Commit

Permalink
Add Example code for AzureOpenAi as comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
tanaka-takayoshi committed Jan 4, 2025
1 parent 76e2ccb commit 970a189
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 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
24 changes: 19 additions & 5 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(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 970a189

Please sign in to comment.