-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Closed
Feature
Copy link
Labels
.NETIssue or Pull requests regarding .NET codeIssue or Pull requests regarding .NET code
Description
name: Feature request
about: Suggest an idea for this project
Background and motivation
The most common, simple way to add native plugin is to use IKernelBuilderPlugins.AddFromType<T>
.
But today, we could not add additional metadata to kernel functions via this API.
Proposed API
[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public class AdditionalMetadataAttribute : Attribute
{
public AdditionalMetadataAttribute(string name, object? value)
{
Name = name;
Value = value;
}
public string Name { get; }
public object? Value { get; }
}
How to use
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class RequirePermissionAttribute : AdditionalMetadataAttribute
{
public const string MetadataName = "RequirePermission";
public RequirePermissionAttribute(bool value): base(MetadataName, value) { }
public RequirePermissionAttribute() : this(true) { }
}
FunctionCallContentBuilder functionCallContentBuilder = new();
foreach (var streamingChatMessageContent in streamingChatMessageContents)
{
functionCallContentBuilder.Append(streamingChatMessageContent);
}
var functionCallContents = functionCallContentBuilder.Build();
foreach (var functionCallContent in functionCallContents)
{
var function = Kernel.Plugins.GetFunction(functionCallContent.PluginName, functionCallContent.FunctionName);
if (function.Metadata.AdditionalProperties.TryGetValue(RequirePermissionAttribute.MetadataName, out var value) && value is true)
{
if (Kernel.Data.TryGetValue(functionCallContent.Id ?? throw new UnreachableException(), out var permitted))
{
FunctionResultContent functionResultContent;
if (permitted is true)
{
functionResultContent = await functionCallContent.InvokeAsync(Kernel, cancellationToken);
}
else
{
functionResultContent = new(functionCallContent, "User has denied permission to execute this function.");
}
ChatHistory.Add(functionResultContent.ToChatMessage());
}
else
{
// Ask user for permission
}
}
}
Metadata
Metadata
Assignees
Labels
.NETIssue or Pull requests regarding .NET codeIssue or Pull requests regarding .NET code
Type
Projects
Status
Sprint: Done