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