From 164de98c8f00075a83d077c461c4926d3a837b46 Mon Sep 17 00:00:00 2001 From: markwallace-microsoft <127216156+markwallace-microsoft@users.noreply.github.com> Date: Fri, 19 Apr 2024 08:44:46 +0100 Subject: [PATCH] Update sample to use enum[] --- .../Getting_Started/Step2_Add_Plugins.cs | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/dotnet/samples/KernelSyntaxExamples/Getting_Started/Step2_Add_Plugins.cs b/dotnet/samples/KernelSyntaxExamples/Getting_Started/Step2_Add_Plugins.cs index 9dba309a19d9..5ff7e4d0aa47 100644 --- a/dotnet/samples/KernelSyntaxExamples/Getting_Started/Step2_Add_Plugins.cs +++ b/dotnet/samples/KernelSyntaxExamples/Getting_Started/Step2_Add_Plugins.cs @@ -2,9 +2,11 @@ using System; using System.ComponentModel; +using System.Linq; using System.Text.Json.Serialization; using System.Threading.Tasks; using Examples; +using Microsoft.OpenApi.Extensions; using Microsoft.SemanticKernel; using Microsoft.SemanticKernel.Connectors.OpenAI; using Xunit; @@ -45,6 +47,7 @@ public async Task RunAsync() // Example 4. Invoke the kernel with a prompt and allow the AI to automatically invoke functions that use enumerations WriteLine(await kernel.InvokePromptAsync("Create a handy lime colored widget for me.", new(settings))); WriteLine(await kernel.InvokePromptAsync("Create a beautiful scarlet colored widget for me.", new(settings))); + WriteLine(await kernel.InvokePromptAsync("Create an attractive maroon and navy colored widget for me.", new(settings))); } /// @@ -58,23 +61,27 @@ public class TimeInformation } /// - /// A plugin that returns the current time. + /// A plugin that creates widgets. /// public class WidgetFactory { [KernelFunction] - [Description("Creates a new widget of the specified type and color")] - public WidgetDetails CreateWidget([Description("The type of widget to be created")] WidgetType widgetType, [Description("The color of the widget to be created")] WidgetColor widgetColor) + [Description("Creates a new widget of the specified type and colors")] + public WidgetDetails CreateWidget([Description("The type of widget to be created")] WidgetType widgetType, [Description("The colors of the widget to be created")] WidgetColor[] widgetColors) { + var colors = string.Join('-', widgetColors.Select(c => c.GetDisplayName()).ToArray()); return new() { - SerialNumber = $"{widgetType}-{widgetColor}-{Guid.NewGuid()}", + SerialNumber = $"{widgetType}-{colors}-{Guid.NewGuid()}", Type = widgetType, - Color = widgetColor + Colors = widgetColors }; } } + /// + /// A is required to correctly convert enum values. + /// [JsonConverter(typeof(JsonStringEnumConverter))] public enum WidgetType { @@ -85,16 +92,19 @@ public enum WidgetType Decorative } + /// + /// A is required to correctly convert enum values. + /// [JsonConverter(typeof(JsonStringEnumConverter))] public enum WidgetColor { - [Description("Use when creating a red widget.")] + [Description("Use when creating a red item.")] Red, - [Description("Use when creating a green widget.")] + [Description("Use when creating a green item.")] Green, - [Description("Use when creating a blue widget.")] + [Description("Use when creating a blue item.")] Blue } @@ -102,6 +112,6 @@ public class WidgetDetails { public string SerialNumber { get; init; } public WidgetType Type { get; init; } - public WidgetColor Color { get; init; } + public WidgetColor[] Colors { get; init; } } }