Files
semantic-kernel/dotnet/samples/KernelSyntaxExamples/Example43_GetModelResult.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

42 lines
1.2 KiB
C#

// Copyright (c) Microsoft. All rights reserved.
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using RepoUtils;
using Xunit;
using Xunit.Abstractions;
namespace Examples;
public class Example43_GetModelResult : BaseTest
{
[Fact]
public async Task GetTokenUsageMetadataAsync()
{
WriteLine("======== Inline Function Definition + Invocation ========");
// Create kernel
Kernel kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey)
.Build();
// Create function
const string FunctionDefinition = "Hi, give me 5 book suggestions about: {{$input}}";
KernelFunction myFunction = kernel.CreateFunctionFromPrompt(FunctionDefinition);
// Invoke function through kernel
FunctionResult result = await kernel.InvokeAsync(myFunction, new() { ["input"] = "travel" });
// Display results
WriteLine(result.GetValue<string>());
WriteLine(result.Metadata?["Usage"]?.AsJson());
WriteLine();
}
public Example43_GetModelResult(ITestOutputHelper output) : base(output)
{
}
}