SemanticPluginForge adds functionality to dynamically alter the metadata for SemanticKernel plugins. This library introduces the IPluginMetadataProvider interface, allowing for real-time updates to plugin metadata, including descriptions, return value descriptions, and parameter descriptions, without the need for redeployment.
-
Dynamic Metadata Updates:
- Make real-time updates to plugin metadata, enhancing flexibility and reducing downtime.
- Implement changes without redeployment, ensuring a seamless update process.
-
Extensible Architecture:
- Implement new metadata providers, such as a database-backed provider, to enable metadata changes without requiring a service restart.
- Support various use cases and future expansions.
-
Dynamic Tuning:
- Fine-tune plugin descriptions and parameters based on evolving requirements or user feedback.
- Quickly respond to changes in business logic or user expectations without interrupting service availability.
-
Custom Metadata Providers:
- Develop custom providers that fetch metadata from different sources, such as databases, remote services, or configuration management systems.
- Achieve higher levels of customization and control over plugin behavior.
To add SemanticPluginForge to your project, use the following command:
dotnet add package SemanticPluginForge.CoreImplement the IPluginMetadataProvider interface. The following code is a sample provider that overrides the description of a function from the TimePlugin.
public class CustomTimeYearMetadataProvider : IPluginMetadataProvider
{
public PluginMetadata? GetPluginMetadata(KernelPlugin plugin) => null;
public FunctionMetadata GetFunctionMetadata(KernelPlugin plugin, KernelFunctionMetadata metadata) =>
plugin.Name == "TimePlugin" && metadata.Name == "Year"
? new FunctionMetadata(metadata.Name) { Description = "Get the current year in 4-digit number format." }
: null;
}Register the custom metadata provider with the service collection.
services.AddSingleton<IPluginMetadataProvider, CustomTimeYearMetadataProvider>();Use the extension methods from this library to add plugins with patched metadata.
var kernelBuilder = services.AddKernel();
kernelBuilder.Plugins.AddFromTypeWithMetadata<TimePlugin>();The library now supports suppressing functions and parameters in plugin metadata. This feature allows you to hide specific functions or parameters from the plugin's consumers while maintaining functionality. For parameters, if a default value is provided, it will be used even when the parameter is suppressed.
To suppress a function, set the Suppress property to true in the FunctionMetadata.
public class CustomMetadataProvider : IPluginMetadataProvider
{
public PluginMetadata? GetPluginMetadata(KernelPlugin plugin) => null;
public FunctionMetadata GetFunctionMetadata(KernelPlugin plugin, KernelFunctionMetadata metadata) =>
plugin.Name == "SamplePlugin" && metadata.Name == "HiddenFunction"
? new FunctionMetadata(metadata.Name) { Suppress = true }
: null;
}To suppress a parameter, set the Suppress property to true in the ParameterMetadata. If a default value is provided, it will be used.
public class CustomMetadataProvider : IPluginMetadataProvider
{
public PluginMetadata? GetPluginMetadata(KernelPlugin plugin) => null;
public FunctionMetadata GetFunctionMetadata(KernelPlugin plugin, KernelFunctionMetadata metadata) =>
plugin.Name == "SamplePlugin" && metadata.Name == "FunctionWithHiddenParameter"
? new FunctionMetadata(metadata.Name)
{
Parameters = new List<ParameterMetadata>
{
new ParameterMetadata("hiddenParam") { Suppress = true, DefaultValue = "default" }
}
}
: null;
}When a parameter is set to Suppress, it must either be optional or have a default value provided. This default value can be specified in the underlying implementation or through the metadata provider.
- Default Value Precedence: If a default value is provided through the metadata provider, it takes precedence over the default value specified in the original plugin implementation.
This ensures that suppressed parameters are handled gracefully without causing runtime errors.
The library allows you to use any CLR type or object as a plugin without requiring KernelFunction attribute. This enables you to create plugins from existing objects or types, making it easier to integrate with existing codebases.
When registering CLR types or objects as plugins, the library follows these rules:
-
Open Generic Methods: Open generic methods are filtered out and not exposed as plugin functions.
-
TAP Method Normalization: Methods following the Task-based Asynchronous Pattern (TAP) are normalized by:
- Removing any
CancellationTokenparameter - Removing the
Asyncsuffix from method names - Preferred selection order is: method with
CancellationTokenparameter, then method withAsyncsuffix
- Removing any
-
Handling Overloaded Methods: For methods with the same name but different parameters, you can use
FunctionMetadata.OverrideFunctionNamein your metadata provider to give each overload a unique name:
// Handling the Random.Next() overloads
public FunctionMetadata? GetFunctionMetadata(KernelPlugin plugin, KernelFunctionMetadata metadata)
{
return plugin.Name switch
{
"RandomPlugin" => metadata.Name switch
{
"Next" when metadata.Parameters.Count == 0 => new FunctionMetadata(metadata.Name)
{
Description = "Returns a non-negative random integer."
},
"Next" when metadata.Parameters.Count == 1 => new FunctionMetadata(metadata.Name)
{
OverrideFunctionName = "NextWithUpperBound",
Description = "Returns a random integer within a specified range.",
Parameters = [
new ParameterMetadata("maxValue")
{
Description = "The exclusive upper bound of the random number returned."
}
]
},
"Next" when metadata.Parameters.Count == 2 => new FunctionMetadata(metadata.Name)
{
OverrideFunctionName = "NextWithRange",
Description = "Returns a random integer within a specified range.",
Parameters = [
new ParameterMetadata("minValue") { Description = "The inclusive lower bound of the random number returned." },
new ParameterMetadata("maxValue") { Description = "The exclusive upper bound of the random number returned." }
]
},
_ => null
},
_ => null
};
}Sample Type and Metadata Provider:
public class DateTimeWrapper
{
public string ToShortDateString()
{
return DateTime.Now.ToShortDateString();
}
public string ToLongDateString()
{
return DateTime.Now.ToLongDateString();
}
public string CurrentTime()
{
return DateTime.Now.ToString("T");
}
}
public class CustomMetadataProvider : IPluginMetadataProvider
{
public PluginMetadata? GetPluginMetadata(KernelPlugin plugin) =>
plugin.Name == "DateTimeWrapper" ? new PluginMetadata
{
Description = "This plugin returns date and time information."
} : null;
public FunctionMetadata? GetFunctionMetadata(KernelPlugin plugin, KernelFunctionMetadata metadata) =>
plugin.Name == "DateTimeWrapper" && metadata.Name == "ToShortDateString" ? new FunctionMetadata(metadata.Name)
{
Description = "Returns the current date in short format (MM/dd/yyyy)."
} : null;
}Usage Example:
kernelBuilder.Plugins.AddFromClrObjectWithMetadata(new DateTimeWrapper(), "DateTimeWrapper");Usage Example:
kernelBuilder.Plugins.AddFromClrTypeWithMetadata<DateTimeWrapper>("DateTimeWrapper");Usage Example:
var qc = new QueueClient(new Uri("https://your-storage.queue.core.windows.net/your-queue"), new DefaultAzureCredential());
kernelBuilder.Plugins.AddFromClrObjectWithMetadata(qc, "Queue");Explore the samples directory for practical examples of using SemanticPluginForge in different scenarios. Each sample includes comprehensive documentation, setup instructions, and focuses on specific framework concepts.
-
DefaultValue: Demonstrates advanced parameter handling including suppression, default values, and context-aware metadata. Shows how to override parameter descriptions and ensure parameters are never resolved from context when suppressed.
-
UseClrType: Shows how to use existing .NET classes as Semantic Kernel plugins without requiring
KernelFunctionattributes. Demonstrates multiple plugin types including custom classes, standard .NET classes likeRandom, and direct Azure SDK integration throughQueueClient. Features function name overriding for handling method overloads. -
AzureAiSearchPlugin: Comprehensive example showing how to create multiple instances of the same plugin class with different metadata configurations for various data sources. Uses mocked data for learning without external dependencies.
Navigate to the samples folder to get started with these examples.
For comprehensive documentation, API reference, and advanced usage examples, visit:
https://lsiddiquee.github.io/SemanticPluginForge/
Need help or have questions? Check out our Support Guide for information on:
- Getting help and reporting issues
- Community discussions
- Contributing guidelines
- Additional resources
Contributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.