Skip to content
Merged
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
8 changes: 4 additions & 4 deletions dotnet/samples/GettingStarted/Step1_Create_Kernel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
namespace GettingStarted;

/// <summary>
/// This example shows how to create and use a <see cref="Kernel"/>.
/// This example shows how to create and use a <see cref="Kernel"/> with ChatClient.
/// </summary>
public sealed class Step1_Create_Kernel(ITestOutputHelper output) : BaseTest(output)
{
/// <summary>
/// Show how to create a <see cref="Kernel"/> and use it to execute prompts.
/// Show how to create a <see cref="Kernel"/> using ChatClient and use it to execute prompts.
/// </summary>
[Fact]
public async Task CreateKernel()
{
// Create a kernel with OpenAI chat completion
// Create a kernel with OpenAI chat completion using ChatClient
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey)
.Build();
Expand Down
22 changes: 13 additions & 9 deletions dotnet/samples/GettingStarted/Step2_Add_Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,40 @@
namespace GettingStarted;

/// <summary>
/// This example shows how to load a <see cref="KernelPlugin"/> instances.
/// This example shows how to load a <see cref="KernelPlugin"/> instances with ChatClient.
/// </summary>
public sealed class Step2_Add_Plugins(ITestOutputHelper output) : BaseTest(output)
{
/// <summary>
/// Shows different ways to load a <see cref="KernelPlugin"/> instances.
/// Shows different ways to load a <see cref="KernelPlugin"/> instances with ChatClient.
/// </summary>
[Fact]
public async Task AddPlugins()
{
// Create a kernel with OpenAI chat completion
// Create a kernel with ChatClient and plugins
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
kernelBuilder.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
kernelBuilder.Plugins.AddFromType<TimeInformation>();
kernelBuilder.Plugins.AddFromType<WidgetFactory>();
Kernel kernel = kernelBuilder.Build();

// Example 1. Invoke the kernel with a prompt that asks the AI for information it cannot provide and may hallucinate
Console.WriteLine("Example 1: Asking the AI for information it cannot provide:");
Console.WriteLine(await kernel.InvokePromptAsync("How many days until Christmas?"));

// Example 2. Invoke the kernel with a templated prompt that invokes a plugin and display the result
// Example 2. Use kernel for templated prompts that invoke plugins directly
Console.WriteLine("\nExample 2: Using templated prompts that invoke plugins directly:");
Console.WriteLine(await kernel.InvokePromptAsync("The current time is {{TimeInformation.GetCurrentUtcTime}}. How many days until Christmas?"));

// Example 3. Invoke the kernel with a prompt and allow the AI to automatically invoke functions
// Example 3. Use kernel with function calling for automatic plugin invocation
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
Console.WriteLine("\nExample 3: Using function calling for automatic plugin invocation:");
Console.WriteLine(await kernel.InvokePromptAsync("How many days until Christmas? Explain your thinking.", new(settings)));

// Example 4. Invoke the kernel with a prompt and allow the AI to automatically invoke functions that use enumerations
// Example 4. Use kernel with function calling for complex scenarios with enumerations
Console.WriteLine("\nExample 4: Using function calling for complex scenarios with enumerations:");
Console.WriteLine(await kernel.InvokePromptAsync("Create a handy lime colored widget for me.", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("Create a beautiful scarlet colored widget for me.", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("Create an attractive maroon and navy colored widget for me.", new(settings)));
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 @@ -19,7 +19,7 @@ public async Task CreatePromptFromYaml()
{
// Create a kernel with OpenAI chat completion
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey)
.Build();
Expand Down
8 changes: 6 additions & 2 deletions dotnet/samples/GettingStarted/Step4_Dependency_Injection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public async Task GetKernelUsingDependencyInjection()
[Fact]
public async Task PluginUsingDependencyInjection()
{
// 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.
// If an application follows DI guidelines, the following line is unnecessary because DI will inject an instance of the Kernel 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>();
Expand All @@ -57,8 +57,12 @@ private ServiceProvider BuildServiceProvider()
collection.AddSingleton<ILoggerFactory>(new XunitLogger(this.Output));
collection.AddSingleton<IUserService>(new FakeUserService());

// Add ChatClient using OpenAI
collection.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);

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

Expand Down
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 @@ -14,7 +14,7 @@ public async Task InvokeChatPrompt()
{
// Create a kernel with OpenAI chat completion
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey)
.Build();
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 @@ -15,7 +15,7 @@ public async Task AddPromptFilter()
{
// Create a kernel with OpenAI chat completion
var builder = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);

Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/GettingStarted/Step7_Observability.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task ObservabilityWithFilters()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
kernelBuilder.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);

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 @@ -17,7 +17,7 @@ public sealed class Step8_Pipelining(ITestOutputHelper output) : BaseTest(output
public async Task CreateFunctionPipeline()
{
IKernelBuilder builder = Kernel.CreateBuilder();
builder.AddOpenAIChatCompletion(
builder.AddOpenAIChatClient(
TestConfiguration.OpenAI.ChatModelId,
TestConfiguration.OpenAI.ApiKey);
builder.Services.AddLogging(c => c.AddConsole().SetMinimumLevel(LogLevel.Trace));
Expand Down
12 changes: 8 additions & 4 deletions dotnet/samples/GettingStarted/Step9_OpenAPI_Plugins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task AddOpenAPIPlugins()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
kernelBuilder.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
Kernel kernel = kernelBuilder.Build();
Expand All @@ -33,12 +33,12 @@ public async Task AddOpenAPIPlugins()
}

/// <summary>
/// Shows how to transform an Open API <see cref="KernelPlugin"/> instance to support dependency injection.
/// Shows how to transform an Open API <see cref="KernelPlugin"/> instance to support dependency injection with ChatClient.
/// </summary>
[Fact]
public async Task TransformOpenAPIPlugins()
{
// Create a kernel with OpenAI chat completion
// Create a kernel with ChatClient and dependency injection
var serviceProvider = BuildServiceProvider();
var kernel = serviceProvider.GetRequiredService<Kernel>();

Expand All @@ -61,8 +61,12 @@ private ServiceProvider BuildServiceProvider()
var collection = new ServiceCollection();
collection.AddSingleton<IMechanicService>(new FakeMechanicService());

// Add ChatClient using OpenAI
collection.AddOpenAIChatClient(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);

var kernelBuilder = collection.AddKernel();
kernelBuilder.Services.AddOpenAIChatCompletion(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey);

return collection.BuildServiceProvider();
}
Expand Down
Loading