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: Improve the DI sample and function naming #9794

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStarted/Step1_Create_Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class Step1_Create_Kernel(ITestOutputHelper output) : BaseTest(out
/// Show how to create a <see cref="Kernel"/> and use it to execute prompts.
/// </summary>
[Fact]
public async Task RunAsync()
public async Task CreateKernelAsync()
{
// Create a kernel with OpenAI chat completion
Kernel kernel = Kernel.CreateBuilder()
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStarted/Step2_Add_Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public sealed class Step2_Add_Plugins(ITestOutputHelper output) : BaseTest(outpu
/// Shows different ways to load a <see cref="KernelPlugin"/> instances.
/// </summary>
[Fact]
public async Task RunAsync()
public async Task AddPluginsAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStarted/Step3_Yaml_Prompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public sealed class Step3_Yaml_Prompt(ITestOutputHelper output) : BaseTest(outpu
/// Show how to create a prompt <see cref="KernelFunction"/> from a YAML resource.
/// </summary>
[Fact]
public async Task RunAsync()
public async Task CreatPromptFromYamlAsync()
{
// Create a kernel with OpenAI chat completion
Kernel kernel = Kernel.CreateBuilder()
Expand Down
54 changes: 53 additions & 1 deletion dotnet/samples/GettingStarted/Step4_Dependency_Injection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/// Show how to create a <see cref="Kernel"/> that participates in Dependency Injection.
/// </summary>
[Fact]
public async Task RunAsync()
public async Task GetKernelUsingDependencyInjectionAsync()
{
// 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
Expand All @@ -32,17 +32,35 @@
}
}

/// <summary>
/// Show how to use a plugin that participates in Dependency Injection.
/// </summary>
[Fact]
public async Task PluginUsingDependencyInjectionAsync()
{
// 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
var serviceProvider = BuildServiceProvider();
var kernel = serviceProvider.GetRequiredService<Kernel>();

// Invoke the prompt which relies on invoking a plugin that depends on a service made avaiable using Dependency Injection.

Check warning on line 46 in dotnet/samples/GettingStarted/Step4_Dependency_Injection.cs

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"avaiable" should be "available".
PromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
Console.WriteLine(await kernel.InvokePromptAsync("Greet the current user by name.", new(settings)));
}

/// <summary>
/// Build a ServiceProvider that can be used to resolve services.
/// </summary>
private ServiceProvider BuildServiceProvider()
{
var collection = new ServiceCollection();
collection.AddSingleton<ILoggerFactory>(new XunitLogger(this.Output));
collection.AddSingleton<IUserService>(new FakeUserService());

var kernelBuilder = collection.AddKernel();
kernelBuilder.Services.AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey);
kernelBuilder.Plugins.AddFromType<TimeInformation>();
kernelBuilder.Plugins.AddFromType<UserInformation>();

return collection.BuildServiceProvider();
}
Expand All @@ -63,4 +81,38 @@
return utcNow;
}
}

/// <summary>
/// A plugin that returns the current time.
/// </summary>
public class UserInformation(IUserService userService)
{

Check failure on line 90 in dotnet/samples/GettingStarted/Step4_Dependency_Injection.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-and-test (8.0, ubuntu-latest, Release, true, integration)

Remove unnecessary blank line (https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1036)

Check failure on line 90 in dotnet/samples/GettingStarted/Step4_Dependency_Injection.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-and-test (8.0, ubuntu-latest, Release, true, integration)

Remove unnecessary blank line (https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1036)

Check failure on line 90 in dotnet/samples/GettingStarted/Step4_Dependency_Injection.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-and-test (8.0, windows-latest, Debug)

Remove unnecessary blank line (https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1036)

Check failure on line 90 in dotnet/samples/GettingStarted/Step4_Dependency_Injection.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-and-test (8.0, windows-latest, Debug)

Remove unnecessary blank line (https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1036)

Check failure on line 90 in dotnet/samples/GettingStarted/Step4_Dependency_Injection.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-and-test (8.0, windows-latest, Release)

Remove unnecessary blank line (https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1036)

Check failure on line 90 in dotnet/samples/GettingStarted/Step4_Dependency_Injection.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-and-test (8.0, windows-latest, Release)

Remove unnecessary blank line (https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1036)
[KernelFunction]
[Description("Retrieves the current users name.")]
public string GetUsername()
{
return userService.GetCurrentUsername();
}
}

/// <summary>
/// Interface for a service to get the current user id.
/// </summary>
public interface IUserService
{
/// <summary>
/// Return the user id for the current user.
/// </summary>
string GetCurrentUsername();
}

/// <summary>
/// Fake implementation of <see cref="IUserService"/>
/// </summary>
public class FakeUserService : IUserService
{
/// <inheritdoc/>
public string GetCurrentUsername() => "Bob";
}
}
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStarted/Step5_Chat_Prompt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed class Step5_Chat_Prompt(ITestOutputHelper output) : BaseTest(outpu
/// Show how to construct a chat prompt and invoke it.
/// </summary>
[Fact]
public async Task RunAsync()
public async Task InvokeChatPromptAsync()
{
// Create a kernel with OpenAI chat completion
Kernel kernel = Kernel.CreateBuilder()
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStarted/Step6_Responsible_AI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class Step6_Responsible_AI(ITestOutputHelper output) : BaseTest(ou
/// Show how to use prompt filters to ensure that prompts are rendered in a responsible manner.
/// </summary>
[Fact]
public async Task RunAsync()
public async Task AddPromptFilterAsync()
{
// Create a kernel with OpenAI chat completion
var builder = Kernel.CreateBuilder()
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStarted/Step8_Pipelining.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public sealed class Step8_Pipelining(ITestOutputHelper output) : BaseTest(output
/// them in a sequence, passing the output from one as input to the next.
/// </summary>
[Fact]
public async Task RunAsync()
public async Task CreateFunctionPipelineAsync()
{
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion(
Expand Down
Loading