You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
3.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

namespace SemanticKernelStudy.Test;
/// <summary>
///
/// </summary>
public class TryTest
{
//推荐Xunit.DependencyInjection.Logging 库,与 Microsoft.Extensions.Logging 集成
private readonly ILogger<TryTest> _logger;
public TryTest(ILogger<TryTest> logger)
{
_logger = logger;
}
[Fact]
public void Try_SK_Test()
{
KernelFunction f1 = KernelFunctionFactory.CreateFromPrompt("河南省会在哪里?");
KernelFunction f2 = KernelFunctionFactory.CreateFromMethod(() => "河南省会在郑州");
KernelFunction f3 = KernelFunctionFactory.CreateFromPrompt(string.Empty);
}
[Fact]
public async Task Try_SK_Filter_Test()
{
var builder = Kernel.CreateBuilder();
// This filter overrides result with "Result from filter" value.
builder.Services.AddSingleton<IFunctionInvocationFilter, FunctionFilterExample>();
var kernel = builder.Build();
var function = KernelFunctionFactory.CreateFromMethod(() => "Result from method");
var result = await kernel.InvokeAsync(function);
Console.WriteLine(result);
Console.WriteLine($"Metadata: {string.Join(",", result.Metadata!.Select(kv => $"{kv.Key}: {kv.Value}"))}");
}
[Fact]
public async Task Try_SK_Filter2_Test()
{
var builder = Kernel.CreateBuilder();
// This filter overrides result with "Result from filter" value.
builder.Services.AddSingleton<IFunctionInvocationFilter, FunctionFilterExample>();
var kernel = builder.Build();
var function = KernelFunctionFactory.CreateFromMethod(() => "Result from method");
var result = await kernel.InvokeAsync(function);
Console.WriteLine(result);
Console.WriteLine($"Metadata: {string.Join(",", result.Metadata!.Select(kv => $"{kv.Key}: {kv.Value}"))}");
}
[Fact]
public void Try_Logger_Test()
{
var msg = "使用 Xunit.DependencyInjection 实现的 {abc} 输出测试内容";
_logger.LogInformation(msg);
Assert.True(true, msg);
}
private sealed class FunctionFilterExample : IFunctionInvocationFilter
{
public async Task OnFunctionInvocationAsync(Microsoft.SemanticKernel.FunctionInvocationContext context, Func<Microsoft.SemanticKernel.FunctionInvocationContext, Task> next)
{
// Example: override kernel arguments
context.Arguments["input"] = "new input";
// This call is required to proceed with next filters in pipeline and actual function.
// Without this call next filters and function won't be invoked.
await next(context);
// Example: get function result value
var value = context.Result!.GetValue<object>();
// Example: get token usage from metadata
var usage = context.Result.Metadata?["Usage"];
// Example: override function result value and metadata
Dictionary<string, object?> metadata = context.Result.Metadata is not null ? new(context.Result.Metadata) : [];
metadata["metadata_key"] = "metadata_value";
context.Result = new FunctionResult(context.Result, "Result from filter")
{
Metadata = metadata
};
}
}
}