OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; FunctionResult result = await kernel.InvokePromptAsync("What is the current weather?", new(settings)); Console.Write(result);
The current weather is as follows:
- Temperature: 35°C
- Humidity: 20%
- Dew Point: 10°C
- Wind Speed: 15 km/h
///<summary> /// A plugin that provides the current weather data and specifies the return type schema in the function <see cref="DescriptionAttribute"/>. ///</summary> privatesealedclassWeatherPlugin2 { [KernelFunction] [Description("""Returns current weather: {"type":"object","properties":{"Data1":{"description":"Temperature (°C)","type":"number"},"Data2":{"description":"Humidity(%)","type":"number"}, "Data3":{"description":"Dew point (°C)","type":"number"},"Data4":{"description":"Wind speed (km/h)","type":"number"}}}""")] public WeatherData GetWeatherData() { returnnew WeatherData() { Data1 = 35.0, // Temperature in degrees Celsius Data2 = 20.0, // Humidity in percentage Data3 = 10.0, // Dew point in degrees Celsius Data4 = 15.0// Wind speed in kilometers per hour }; } publicsealedclassWeatherData { publicdouble Data1 { get; set; } publicdouble Data2 { get; set; } publicdouble Data3 { get; set; } publicdouble Data4 { get; set; } } }
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; FunctionResult result = await kernel.InvokePromptAsync("What is the current weather?", new(settings)); Console.WriteLine(result);
The current weather is as follows:
- Temperature: 35°C
- Humidity: 20%
- Dew point: 10°C
- Wind speed: 15 km/h
///<summary> /// A plugin that provides the current weather data and provides descriptions for the return type properties. ///</summary> privatesealedclassWeatherPlugin3 { [KernelFunction] public WeatherData GetWeatherData() { returnnew WeatherData() { Data1 = 35.0, // Temperature in degrees Celsius Data2 = 20.0, // Humidity in percentage Data3 = 10.0, // Dew point in degrees Celsius Data4 = 15.0// Wind speed in kilometers per hour }; }
///<summary> /// A auto function invocation filter that replaces the original function's result with a new result that includes both the original result and its schema. ///</summary> privatesealedclassAddReturnTypeSchemaFilter : IAutoFunctionInvocationFilter { publicasync Task OnAutoFunctionInvocationAsync(AutoFunctionInvocationContext context, Func<AutoFunctionInvocationContext, Task> next) { // Invoke the function await next(context); // Crete the result with the schema FunctionResultWithSchema resultWithSchema = new() { Value = context.Result.GetValue<object>(), // Get the original result Schema = context.Function.Metadata.ReturnParameter?.Schema // Get the function return type schema }; // Return the result with the schema instead of the original one context.Result = new FunctionResult(context.Result, resultWithSchema); } privatesealedclassFunctionResultWithSchema { publicobject? Value { get; set; } public KernelJsonSchema? Schema { get; set; } } }
1 2 3 4 5 6 7 8
kernel.AutoFunctionInvocationFilters.Add(new AddReturnTypeSchemaFilter()); // Import the plugin that provides descriptions for the return type properties. // This additional information is used when extracting the schema from the return type. kernel.Plugins.Clear(); kernel.ImportPluginFromType<WeatherPlugin3>(); OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() }; FunctionResult result = await kernel.InvokePromptAsync("What is the current weather?", new(settings)); Console.WriteLine(result);
The current weather is as follows:
- Temperature: 35°C
- Humidity: 20%
- Dew Point: 10°C
- Wind Speed: 15 km/h