//创建kernel后。。。 var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
预定义插件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
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", _ => "31 and snowing", }, "GetWeatherForCity", "Gets the current weather for the specified city and specified date time."), ]);
1. Auto
1.1 自动调用函数
1 2 3 4
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();
The likely color of the sky in Boston today is gray, due to the rain.
如果使用Yaml模板,可以如下配置:
1 2 3 4 5 6 7 8 9 10 11
string promptTemplateConfig = """ template_format: semantic-kernel template: What is the likely color of the sky in Boston today? execution_settings: default: function_choice_behavior: type: auto """;
ChatHistory chatHistory = []; chatHistory.AddUserMessage("What is the likely color of the sky in Boston today?"); while (true) { // Start or continue chat based on the chat history ChatMessageContent result = await chatCompletionService.GetChatMessageContentAsync(chatHistory, settings, kernel); if (result.Content isnotnull) { Console.Write(result.Content); // Expected output: "The color of the sky in Boston is likely to be gray due to the rainy weather." chatHistory.Add(result); } // Get function calls from the chat message content and quit the chat loop if no function calls are found. IEnumerable<FunctionCallContent> functionCalls = FunctionCallContent.GetFunctionCalls(result); if (!functionCalls.Any()) { break; } // Preserving the original chat message content with function calls in the chat history. chatHistory.Add(result); // Iterating over the requested function calls and invoking them sequentially. // The code can easily be modified to invoke functions in concurrently if needed. foreach (FunctionCallContent functionCall in functionCalls) { try { // Invoking the function FunctionResultContent resultContent = await functionCall.InvokeAsync(kernel); // Adding the function result to the chat history chatHistory.Add(resultContent.ToChatMessage()); } catch (Exception ex) { // Adding function exception to the chat history. chatHistory.Add(new FunctionResultContent(functionCall, ex).ToChatMessage()); // or //chatHistory.Add(new FunctionResultContent(functionCall, "Error details that the AI model can reason about.").ToChatMessage()); } } } chatHistory.Display();
The sky in Boston today is likely to be gray due to the rainy weather.
Console.WriteLine(await kernel.InvokePromptAsync("Given that it is now the 9th of September 2024, 11:29 AM, what is the likely color of the sky in Boston?", new(settings)));
The sky in Boston is likely gray due to the rainy weather.
如果使用Yaml模板,可以如下配置:
1 2 3 4 5 6 7 8 9 10 11 12 13
string promptTemplateConfig = """ template_format: semantic-kernel template: Given that it is now the 9th of September 2024, 11:29 AM, what is the likely color of the sky in Boston? execution_settings: default: function_choice_behavior: type: auto functions: - HelperFunctions.GetWeatherForCity """;
To determine the color of the sky in Boston for today, you would need to follow these steps:
1. **HelperFunctions-GetCurrentDateTimeInUtc**: Call this function to get the current date and time in UTC. This will help you determine the current time to fetch the weather information accurately.
2. **HelperFunctions-GetWeatherForCity**: After obtaining the current UTC date and time, use this function with "Boston" as the city name and the current UTC date and time as parameters to get the weather information for Boston. The weather information will typically include aspects like cloud cover, which can help deduce the color of the sky.
By using both these functions together, you can get the necessary weather information to infer the sky's color.
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto(functions:[]) }; var response =await kernel.InvokePromptAsync("What is the likely color of the sky in Boston today?", new(settings));
response.Display();
如果在Yaml中配置,等同于:
1 2 3 4 5 6 7 8 9 10 11 12
string promptTemplateConfig = """ template_format: semantic-kernel template: WWhat is the likely color of the sky in Boston? execution_settings: default: function_choice_behavior: type: auto functions: [] """;