Files
semantic-kernel/dotnet/samples/KernelSyntaxExamples/Getting_Started/Step2_Add_Plugins.cs
T
Roger Barreto b99b780eca .Net Kernel Examples as tests (#4526)
### Motivation and Context

Convert Kernel Examples as Integration Tests.
2024-01-16 19:16:24 +00:00

56 lines
2.1 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Examples;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Xunit;
using Xunit.Abstractions;
namespace GettingStarted;
// This example shows how to load a <see cref="KernelPlugin"/> instances.
public class Step2_Add_Plugins : BaseTest
{
/// <summary>
/// Shows different ways to load a <see cref="KernelPlugin"/> instances.
/// </summary>
[Fact]
public async Task RunAsync()
{
// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
kernelBuilder.Plugins.AddFromType<TimeInformation>();
Kernel kernel = kernelBuilder.Build();
// Example 1. Invoke the kernel with a prompt that asks the AI for inromation it cannot provide and may hallucinate
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
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
OpenAIPromptExecutionSettings settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
WriteLine(await kernel.InvokePromptAsync("How many days until Christmas? Explain your thinking.", new(settings)));
}
/// <summary>
/// A plugin that returns the current time.
/// </summary>
public class TimeInformation
{
[KernelFunction]
[Description("Retrieves the current time in UTC.")]
public string GetCurrentUtcTime() => DateTime.UtcNow.ToString("R");
}
public Step2_Add_Plugins(ITestOutputHelper output) : base(output)
{
}
}