Skip to content

Commit

Permalink
.Net: Update sample for the accompanying Blog Post (#9663)
Browse files Browse the repository at this point in the history
### Motivation and Context

Closes #6605

### Description

Some minor enhancements to the sample to match content in the Blog Post

### 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 Nov 12, 2024
1 parent dcf682e commit d2b5dc1
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions dotnet/samples/Concepts/Plugins/TransformPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,12 @@ public async Task MissingRequiredInformationAsync()

// Invoke the kernel with a prompt and allow the AI to automatically invoke functions
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
Console.WriteLine(await kernel.InvokePromptAsync("What is my favourite color?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What color should I paint the fence?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("I am going diving what animals would I like to see?", new(settings)));

// Example response
// I'd be happy to help with that! Could you please provide your email address so I can look up your favorite color?
// Example responses
// If you would like a suggestion based on your preferences, I can find out your favorite color if you provide your email address.
// To help you with that, I would need to know your favorite type of aquatic animals.If you provide your email, I can check your preferences, if available, for your favorite type of fish or other marine creatures.
}

/// <summary>
Expand All @@ -108,9 +110,9 @@ public async Task CreatePluginWithAlteredParametersAsync()
// Create a new Plugin which hides parameters that require PII
var plugin = KernelPluginFactory.CreateFromType<UserFavorites>();
var transformedPlugin = CreatePluginWithParameters(
plugin,
(KernelParameterMetadata parameter) => parameter.Name != "email",
(KernelArguments arguments) => arguments.Add("email", "bob@contoso.com"));
plugin,
(KernelParameterMetadata parameter) => parameter.Name != "email",
(KernelFunctionMetadata function, KernelArguments arguments) => arguments.Add("email", "bob@contoso.com"));

// Create a kernel with OpenAI chat completion
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
Expand All @@ -122,10 +124,13 @@ public async Task CreatePluginWithAlteredParametersAsync()

// Invoke the kernel with a prompt and allow the AI to automatically invoke functions
OpenAIPromptExecutionSettings settings = new() { FunctionChoiceBehavior = FunctionChoiceBehavior.Auto() };
Console.WriteLine(await kernel.InvokePromptAsync("What is my favourite color?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What is my favourite cold-blooded animal?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What is my favourite marine animal?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What is my favourite creepy crawly?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What color should my new car be?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What color should I paint the fence?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What is my favorite cold-blooded animal?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What is my favorite marine animal?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What is my favorite creepy crawly?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("What is my favorite four legged friend?", new(settings)));
Console.WriteLine(await kernel.InvokePromptAsync("I am going diving what animals would I like to see?", new(settings)));

// Example response
// Your favorite color is Green. 🌿
Expand All @@ -136,7 +141,7 @@ public async Task CreatePluginWithAlteredParametersAsync()

public delegate bool IncludeKernelParameter(KernelParameterMetadata parameter);

public delegate void UpdateKernelArguments(KernelArguments arguments);
public delegate void UpdateKernelArguments(KernelFunctionMetadata function, KernelArguments arguments);

/// <summary>
/// Create a <see cref="KernelPlugin"/> instance from the provided instance where each function only includes
Expand Down Expand Up @@ -164,7 +169,7 @@ private static KernelFunction CreateFunctionWithParameters(KernelFunction functi
{
var method = (Kernel kernel, KernelFunction currentFunction, KernelArguments arguments, CancellationToken cancellationToken) =>
{
updateKernelArguments(arguments);
updateKernelArguments(currentFunction.Metadata, arguments);
return function.InvokeAsync(kernel, arguments, cancellationToken);
};

Expand Down

0 comments on commit d2b5dc1

Please sign in to comment.