Skip to content

.Net: New Feature: Define Additional Metadata of native plugin function via data annotations #11182

@RamType0

Description

@RamType0

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

Labels

.NETIssue or Pull requests regarding .NET code

Projects

Status

Sprint: Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions