上面已经提及Native Plugin = Native Function + Native Function + ... ,因此编写Native Plugin的关键就是Native Function 的编写,在SK 中Native Function 使用[KernelFunction] 特性并配合[Description]特性标识即可,SK会根据标记的特性自动生成此对象的信息并且传递给 AI 代理,并将 AI 代理生成的参数列表映射到正确的对象。以下就是一个计算插件用于加减乘除运送。
using System.ComponentModel; using Microsoft.SemanticKernel; publicclassMathPlugin { [KernelFunction, Description("Take the square root of a number")] publicstaticdoubleSqrt( [Description("The number to take a square root of")] double number1 ) { Console.WriteLine("Taking the square root of {0}", number1); return Math.Sqrt(number1); } [KernelFunction, Description("Add two numbers")] publicstaticdoubleAdd( [Description("The first number to add")] double number1, [Description("The second number to add")] double number2 ) { Console.WriteLine("Adding {0} to {1}", number1, number2); return number1 + number2; } [KernelFunction, Description("Subtract two numbers")] publicstaticdoubleSubtract( [Description("The first number to subtract from")] double number1, [Description("The second number to subtract away")] double number2 ) { Console.WriteLine("Subtracting {0} from {1}", number2, number1); return number1 - number2; } [KernelFunction, Description("Multiply two numbers. When increasing by a percentage, don't forget to add 1 to the percentage.")] publicstaticdoubleMultiply( [Description("The first number to multiply")] double number1, [Description("The second number to multiply")] double number2 ) { Console.WriteLine("Multiplying {0} by {1}", number1, number2); return number1 * number2; } [KernelFunction, Description("Divide two numbers")] publicstaticdoubleDivide( [Description("The first number to divide from")] double number1, [Description("The second number to divide by")] double number2 ) { Console.WriteLine("Dividing {0} by {1}", number1, number2); return number1 / number2; } }
// 注册插件 kernel.Plugins.Clear(); kernel.Plugins.AddFromType<MathPlugin>(); kernel.Plugins.AddFromFunctions("time_plugin", [ KernelFunctionFactory.CreateFromMethod( method: () => DateTime.Now, functionName: "get_time", description: "Get the current time" ) ]);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
using Microsoft.SemanticKernel.Connectors.OpenAI;
var request = await PolyglotKernel.GetInputAsync("请输入你的请求:"); // Enable auto function calling OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions }; // Get the response from the AI var result = await chatCompletionService.GetChatMessageContentAsync( prompt: request, executionSettings: openAIPromptExecutionSettings, kernel: kernel); result.Display();
var request = await PolyglotKernel.GetInputAsync("请输入你的请求:"); // Enable auto function calling OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new() { ToolCallBehavior = ToolCallBehavior.EnableKernelFunctions };
var chatHistory = new ChatHistory(); chatHistory.AddMessage(AuthorRole.User, request);
// Get the response from the AI var result = await chatCompletionService.GetChatMessageContentAsync( chatHistory: chatHistory, executionSettings: openAIPromptExecutionSettings, kernel: kernel);
// 返回函数调用请求描述 result.Display();
接下来可以使用以下方式进行手动调用:
1 2 3 4 5 6 7 8 9
// 获取LLMs返回的函数调用 var functionCalls = FunctionCallContent.GetFunctionCalls(result);