//准备工作 var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
kernel.Plugins.Clear(); kernel.ImportPluginFromFunctions("HelperFunctions", [ kernel.CreateFunctionFromMethod(() => DateTime.UtcNow.ToString("R"), "GetCurrentDateTimeInUtc", "Retrieves the current date time in UTC."), kernel.CreateFunctionFromMethod((string cityName, string currentDateTimeInUtc) => cityName switch { "Boston" => "61 and rainy", "London" => "55 and cloudy", "Miami" => "80 and sunny", "Paris" => "60 and rainy", "Tokyo" => "50 and sunny", "Sydney" => "75 and sunny", "Tel Aviv" => "80 and sunny", "Beijing" => throw new Exception("Weather data not available for Beijing."), _ => "31 and snowing", }, "GetWeatherForCity", "Gets the current weather for the specified city and specified date time."), ]);
kernel.FunctionInvocationFilters.Clear(); kernel.FunctionInvocationFilters.Add(new ExceptionHandleFilter()); OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; var response = await kernel.InvokePromptAsync("What is the likely color of the sky in Beijing today?", new(settings));
response.Display();
提示:Weather data not available for Beijing.
审计过滤器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
private sealed classFunctionCallsAuditFilter : IAutoFunctionInvocationFilter { public async Task OnAutoFunctionInvocationAsync(AutoFunctionInvocationContext context, Func<AutoFunctionInvocationContext, Task> next) { context.Display(); var chatHistory = context.ChatHistory; var functionCalls = FunctionCallContent.GetFunctionCalls(chatHistory.Last()).ToArray(); if (functionCalls is { Length: > 0 }) { foreach (var functionCall in functionCalls) { Console.WriteLine($"Request #{context.RequestSequenceIndex}. Function call: {functionCall.PluginName}.{functionCall.FunctionName}."); } } awaitnext(context); } }
1 2 3 4 5 6
kernel.AutoFunctionInvocationFilters.Clear(); kernel.AutoFunctionInvocationFilters.Add(new FunctionCallsAuditFilter()); OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; var response =await kernel.InvokePromptAsync("What is the likely color of the sky in Boston today?", new(settings));
response.Display();
Request #0. Function call: HelperFunctions.GetCurrentDateTimeInUtc.
Request #1. Function call: HelperFunctions.GetWeatherForCity.
上下文增强过滤器
在用户请求中,添加额外上下文信息
1 2 3 4 5 6 7 8 9 10 11 12 13
kernel.Plugins.Clear(); kernel.ImportPluginFromFunctions("TodoPlugin", [ kernel.CreateFunctionFromMethod((Kernel kernel) =>{ kernel.Data.Display(); var userId = kernel.Data["CurrentUserId"]; if (userId is"shengjie") { return new List<string> { "Buy groceries", "Walk the dog", "Write a blog post" }; } return []; }, "GetTodoList", " Gets the todo list."), ]);
using PolyglotKernel= Microsoft.DotNet.Interactive.Kernel;// 引入交互式的内核命名空间,以便用户输入
public classApprovalFilter() : IFunctionInvocationFilter { public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next) { if (context.Function.PluginName == "DynamicsPlugin" && context.Function.Name == "create_order") { Console.WriteLine("System > The agent wants to create an approval, do you want to proceed? (Y/N)");
var shouldProceed = await PolyglotKernel.GetInputAsync("Y/N");
if (shouldProceed != "Y") { context.Result = new FunctionResult(context.Result, "The order creation was not approved by the user"); return; } }
awaitnext(context); } }
1 2 3 4 5 6 7 8 9 10 11
kernel.Plugins.Clear(); kernel.ImportPluginFromFunctions("DynamicsPlugin", [ kernel.CreateFunctionFromMethod( (string userName, string skuId, int qty) => { var orderId = Guid.NewGuid().ToString(); Console.WriteLine("System > Creating order..."); Console.WriteLine($"System > Order created! The order ID is {orderId}"); return $"Order {orderId} created!"; }, "create_order", "Create an order for a given SKU and quantity") ]);;
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; var response = await kernel.InvokePromptAsync("Please place an order for Jery. She would like to purchase one iPhone16.", new(settings));