-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
79 lines (63 loc) · 3.81 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// See https://aka.ms/new-console-template for more information
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using semantic_kernel_poc;
internal static class Program
{
internal static async Task Main(string[] args)
{
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
// Actual code to execute is found in Worker class
builder.Services.AddHostedService<Worker>();
// Get configuration
builder.Services.AddOptions<AzureOpenAI>()
.Bind(builder.Configuration.GetSection(nameof(AzureOpenAI)))
.ValidateDataAnnotations()
.ValidateOnStart();
// Chat completion service that kernels will use
builder.Services.AddSingleton<IChatCompletionService>(sp =>
{
AzureOpenAI options = sp.GetRequiredService<IOptions<AzureOpenAI>>().Value;
// A custom HttpClient can be provided to this constructor
return new AzureOpenAIChatCompletionService(options.ChatDeploymentName, options.Endpoint, options.ApiKey);
/* Alternatively, you can use plain, non-Azure OpenAI after loading OpenAIOptions instead
of AzureOpenAI options with builder.Services.AddOptions:
OpenAI options = sp.GetRequiredService<IOptions<OpenAIOptions>>().Value;
return new OpenAIChatCompletionService(options.ChatModelId, options.ApiKey);*/
});
// Add plugins that can be used by kernels
// The plugins are added as singletons so that they can be used by multiple kernels
builder.Services.AddSingleton<MyTimePlugin>();
builder.Services.AddSingleton<MyAlarmPlugin>();
builder.Services.AddKeyedSingleton<MyLightPlugin>("OfficeLight");
builder.Services.AddKeyedSingleton<MyLightPlugin>("PorchLight", (sp, key) =>
{
return new MyLightPlugin(turnedOn: true);
});
/* To add an OpenAI or OpenAPI plugin, you need to be using Microsoft.SemanticKernel.Plugins.OpenApi.
Then create a temporary kernel, use it to load the plugin and add it as keyed singleton.
Kernel kernel = new();
KernelPlugin openAIPlugin = await kernel.ImportPluginFromOpenAIAsync("<plugin name>", new Uri("<OpenAI-plugin>"));
builder.Services.AddKeyedSingleton<KernelPlugin>("MyImportedOpenAIPlugin", openAIPlugin);
KernelPlugin openApiPlugin = await kernel.ImportPluginFromOpenApiAsync("<plugin name>", new Uri("<OpenAPI-plugin>"));
builder.Services.AddKeyedSingleton<KernelPlugin>("MyImportedOpenApiPlugin", openApiPlugin);*/
// Add a home automation kernel to the dependency injection container
builder.Services.AddKeyedTransient<Kernel>("HomeAutomationKernel", (sp, key) =>
{
// Create a collection of plugins that the kernel will use
KernelPluginCollection pluginCollection = [];
pluginCollection.AddFromObject(sp.GetRequiredService<MyTimePlugin>());
pluginCollection.AddFromObject(sp.GetRequiredService<MyAlarmPlugin>());
pluginCollection.AddFromObject(sp.GetRequiredKeyedService<MyLightPlugin>("OfficeLight"), "OfficeLight");
pluginCollection.AddFromObject(sp.GetRequiredKeyedService<MyLightPlugin>("PorchLight"), "PorchLight");
// When created by the dependency injection container, Semantic Kernel logging is included by default
return new Kernel(sp, pluginCollection);
});
using IHost host = builder.Build();
await host.RunAsync();
}
}