-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Kernel_Injecting.cs
53 lines (41 loc) · 2.25 KB
/
Kernel_Injecting.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
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
namespace DependencyInjection;
// The following examples show how to use SK SDK in applications using DI/IoC containers.
public class Kernel_Injecting(ITestOutputHelper output) : BaseTest(output)
{
[Fact]
public async Task RunAsync()
{
ServiceCollection collection = new();
collection.AddLogging(c => c.AddConsole().SetMinimumLevel(LogLevel.Information));
collection.AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey);
collection.AddSingleton<Kernel>();
// Registering class that uses Kernel to execute a plugin
collection.AddTransient<KernelClient>();
// Create a service provider for resolving registered services
await using ServiceProvider serviceProvider = collection.BuildServiceProvider();
//If an application follows DI guidelines, the following line is unnecessary because DI will inject an instance of the KernelClient class to a class that references it.
//DI container guidelines - https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#recommendations
KernelClient kernelClient = serviceProvider.GetRequiredService<KernelClient>();
//Execute the function
await kernelClient.SummarizeAsync("What's the tallest building in South America?");
}
/// <summary>
/// Class that uses/references Kernel.
/// </summary>
private sealed class KernelClient(Kernel kernel, ILoggerFactory loggerFactory)
{
private readonly Kernel _kernel = kernel;
private readonly ILogger _logger = loggerFactory.CreateLogger(nameof(KernelClient));
public async Task SummarizeAsync(string ask)
{
string folder = RepoFiles.SamplePluginsPath();
var summarizePlugin = this._kernel.ImportPluginFromPromptDirectory(Path.Combine(folder, "SummarizePlugin"));
var result = await this._kernel.InvokeAsync(summarizePlugin["Summarize"], new() { ["input"] = ask });
this._logger.LogWarning("Result - {0}", result.GetValue<string>());
}
}
}