Skip to content

Commit

Permalink
.Net: Extend plugins sample to demonstrate the use of enums (#5850)
Browse files Browse the repository at this point in the history
### Motivation and Context

<!-- Thank you for your contribution to the semantic-kernel repo!
Please help reviewers and future users, providing the following
information:
  1. Why is this change required?
  2. What problem does it solve?
  3. What scenario does it contribute to?
  4. If it fixes an open issue, please link to the issue here.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
markwallace-microsoft authored Apr 12, 2024
1 parent 2e3d8cf commit 1626f7a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dotnet/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Azure.AI.OpenAI" Version="1.0.0-beta.15" />
<PackageVersion Include="Azure.Identity" Version="1.10.4" />
<PackageVersion Include="Azure.Identity" Version="1.11.0" />
<PackageVersion Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.2.0" />
<PackageVersion Include="Azure.Search.Documents" Version="11.5.1" />
<PackageVersion Include="Handlebars.Net.Helpers" Version="2.4.1.5" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.ComponentModel;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Examples;
using Microsoft.SemanticKernel;
Expand All @@ -28,6 +29,7 @@ public async Task RunAsync()
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey);
kernelBuilder.Plugins.AddFromType<TimeInformation>();
kernelBuilder.Plugins.AddFromType<WidgetFactory>();
Kernel kernel = kernelBuilder.Build();

// Example 1. Invoke the kernel with a prompt that asks the AI for information it cannot provide and may hallucinate
Expand All @@ -39,6 +41,10 @@ public async Task RunAsync()
// Example 3. Invoke the kernel with a prompt and allow the AI to automatically invoke functions
OpenAIPromptExecutionSettings settings = new() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };
WriteLine(await kernel.InvokePromptAsync("How many days until Christmas? Explain your thinking.", new(settings)));

// 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)));
}

/// <summary>
Expand All @@ -51,6 +57,54 @@ public class TimeInformation
public string GetCurrentUtcTime() => DateTime.UtcNow.ToString("R");
}

/// <summary>
/// A plugin that returns the current time.
/// </summary>
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)
{
return new()
{
SerialNumber = $"{widgetType}-{widgetColor}-{Guid.NewGuid()}",
Type = widgetType,
Color = widgetColor
};
}
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum WidgetType
{
[Description("A widget that is useful.")]
Useful,

[Description("A widget that is decorative.")]
Decorative
}

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum WidgetColor
{
[Description("Use when creating a red widget.")]
Red,

[Description("Use when creating a green widget.")]
Green,

[Description("Use when creating a blue widget.")]
Blue
}

public class WidgetDetails
{
public string SerialNumber { get; init; }
public WidgetType Type { get; init; }
public WidgetColor Color { get; init; }
}

public Step2_Add_Plugins(ITestOutputHelper output) : base(output)
{
}
Expand Down

0 comments on commit 1626f7a

Please sign in to comment.