Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Update sample for the accompanying Blog Post #9663

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading