semantic-kernel icon indicating copy to clipboard operation
semantic-kernel copied to clipboard

.Net: Pass Metadata from FunctionInvocationFilter

Open cdeisler opened this issue 1 year ago • 2 comments

Is there a trick to getting additional data back from an FunctionInvocationFilter in order to add additional info to the OpenAI response?

https://github.com/microsoft/semantic-kernel/blob/c669f74099629db40c281397886ae5d81856e9e4/dotnet/samples/Concepts/Filtering/FunctionInvocationFiltering.cs#L58

In AdaptiveCardFilter : IAutoFunctionInvocationFilter

  Dictionary<string, object?> metadata = context.Result.Metadata is not null ? new(context.Result.Metadata) : [];
  metadata["AdaptiveCard"] = adaptiveCardJson;

  context.Result = new FunctionResult(context.Result)
  {
      Metadata = metadata
  };

In SemanticKernelService.cs:

public async Task<string> GenerateReplyAsync(string input, string author...)
{

    // Get the chat function from the ChatPlugin
    var chatFunction = _kernel.Plugins.GetFunction(nameof(ChatPlugin), "Chat");

    var executionSettings = new AzureOpenAIPromptExecutionSettings
    {
        Temperature = 0.1, 
        MaxTokens = ...,
        Seed = ...,
        FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
    };

    // Invoke the chat function
    var result = await _kernel.InvokeAsync(chatFunction, new KernelArguments
    {
        ["input"] = input,
        ["author"] = author,
        ["executionSettings"] = executionSettings,
        ["kernel"] = _kernel
    });

    ******* result.Metadata = null *******

    return result.GetValue<string>() ?? "No response generated.";
}

Image

cdeisler avatar Mar 25 '25 20:03 cdeisler

This issue is stale because it has been open for 90 days with no activity.

github-actions[bot] avatar Jun 26 '25 02:06 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Jun 20 '26 02:06 github-actions[bot]