mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
56 lines
2.1 KiB
C#
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)
|
|
{
|
|
}
|
|
}
|