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

.Net: Fix for Issue 6690 .Net: New Feature: Add helper method that allows a plugin to be imported from a type and have the metadata specified externally

Open emtabiraobarias opened this issue 1 year ago • 2 comments

Motivation and Context

  1. Why is this change required? [x] Function/parameter descriptions need to be hard coded for kernel functions. It is better to load these from a yamml configuration file. Kernel function descriptions will be used more often with the usage of agents and planners, and it will be helpful to have a feature where it can be modified through a yaml configuration, instead of hard coding.
  2. What problem does it solve? [x] Some reasons you would want to do this: (a) It's not possible to modify the existing code to add the KernelFunction attribute; (b) You want to keep the function metadata separate from the function implementation.
  3. What scenario does it contribute to? [x] Creation of kernel function from an external metadata specified through yaml config
  4. If it fixes an open issue, please link to the issue here. [x] Issue #6690 https://github.com/microsoft/semantic-kernel/issues/6690

Description

Guided by the concept implemantation in https://github.com/microsoft/semantic-kernel/pull/6650/files, the overall approach to addressing this issue is to shadow the implementation of KernelFunctionYaml.ToPromptTemplateConfig() function. The added test cases show how the new helper function must be used. This involved the following changes made:

  1. changes made to existing files: [x] dotnet/src/Functions/Functions.Yaml/KernelFunctionYaml.cs added the following function: |__ FromMethodYaml() |__ ToMethodTemplateConfig() [x] dotnet/src/Functions/Functions.UnitTests/Yaml/Functions/KernelFunctionYamlTests.cs added the following test cases: |__ ItShouldCreateFunctionFromMethodYaml() |__ ItShouldImportPluginFromMethodYamlConfigAsync()
  2. new files added: [x] dotnet/src/Functions/Functions.Yaml/FunctionSettingsNodeDeserializer.cs to convert the yaml string into config variables [x] dotnet/src/Functions/Functions.Yaml/FunctionYamlKernelExtension.cs to as class for extensions methods to define functions using method YAML format.
  3. new files added under a new directory dotnet/src/SemanticKernel.Abstractions/Functions/FunctionTemplate/: [x] dotnet/src/SemanticKernel.Abstractions/Functions/FunctionTemplate/FunctionSettings.cs which is used for deserialisation of yaml function configuration. [x] dotnet/src/SemanticKernel.Abstractions/Functions/FunctionTemplate/FunctionTemplateConfig.cs as the template configuration class for kernel functions.

Contribution Checklist

  • [x] The code builds clean without any errors or warnings |__ Errors/Warnings were the same prior to making the changes, and all of them are not related to the changes I made
  • [x] The PR follows the SK Contribution Guidelines and the pre-submission formatting script raises no violations
  • [x] All unit tests pass, and I have added new tests where possible
  • [x] I didn't break anyone :smile: |__ There are no existing functions modified. My approach has instead created new functions and new classes to implement the code path for this new feature. |__ The classes I have reused from PromptTemplate definitions are the InputVariable and OutputVariable classes.

emtabiraobarias avatar Jun 23 '24 08:06 emtabiraobarias

@microsoft-github-policy-service agree

emtabiraobarias avatar Jun 23 '24 08:06 emtabiraobarias

Hi @markwallace-microsoft I have committed some updates to address the branch code coverage issue that was blocking the workflows. I have also added handling of exception cases. Please review and kindly let me know if more updates are required.

emtabiraobarias avatar Jun 30 '24 22:06 emtabiraobarias

Hi @markwallace-microsoft please review and kindly let me know if you require more updates.

emtabiraobarias avatar Jul 09 '24 01:07 emtabiraobarias

Apologies for the delay @emtabiraobarias, will get to this one tomorrow

markwallace-microsoft avatar Jul 23 '24 18:07 markwallace-microsoft

Hi @emtabiraobarias many thanks for creating this PR and for your interest in the Semantic Kernel.

We have had a lot of requests recently from developers who need to more control over the Kernel Function metadata, for all function types i.e. native, semantic, OpenAPI and GRPC. We are still gathering requirements before deciding on the final design for this feature. We also have some new functionality in development which also needs to be considered.

For now we are recommending developers transform the KernelFunction into a new KernelPlugin instance with the function definitions you need. Transforming could involve changing function and parameters descriptions or even removing certain parameters as shown in this sample: https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/Concepts/Plugins/TransformPlugin.cs. This can be done without any changes to the Semantic Kernel.

We won't be including your changes because we need to consider additional use cases and finalise the requirements for this feature.

I would recommend you take a look at this project SemanticPluginForge which adds functionality to dynamically alter the metadata for Semantic Kernel plugins. This could be a good place for your changes.

Thanks again!

markwallace-microsoft avatar Jul 24 '24 08:07 markwallace-microsoft