|
| 1 | +// Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Microsoft.SemanticKernel; |
| 8 | + |
| 9 | +/** |
| 10 | + * This example shows how to create a mutable <see cref="KernelPlugin"/>. |
| 11 | + */ |
| 12 | +// ReSharper disable once InconsistentNaming |
| 13 | +public static class Example73_MutableKernelPlugin |
| 14 | +{ |
| 15 | + /// <summary> |
| 16 | + /// Show how to create a mutable <see cref="KernelPlugin"/>. |
| 17 | + /// </summary> |
| 18 | + public static async Task RunAsync() |
| 19 | + { |
| 20 | + var plugin = new MutableKernelPlugin("Plugin"); |
| 21 | + plugin.AddFunction(KernelFunctionFactory.CreateFromMethod(() => "Plugin.Function", "Function")); |
| 22 | + |
| 23 | + var kernel = new Kernel(); |
| 24 | + kernel.Plugins.Add(plugin); |
| 25 | + |
| 26 | + var result = await kernel.InvokeAsync(kernel.Plugins["Plugin"]["Function"]); |
| 27 | + |
| 28 | + Console.WriteLine($"Result: {result}"); |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Provides an <see cref="KernelPlugin"/> implementation around a collection of functions. |
| 33 | + /// </summary> |
| 34 | + public class MutableKernelPlugin : KernelPlugin |
| 35 | + { |
| 36 | + /// <summary>The collection of functions associated with this plugin.</summary> |
| 37 | + private readonly Dictionary<string, KernelFunction> _functions; |
| 38 | + |
| 39 | + /// <summary>Initializes the new plugin from the provided name, description, and function collection.</summary> |
| 40 | + /// <param name="name">The name for the plugin.</param> |
| 41 | + /// <param name="description">A description of the plugin.</param> |
| 42 | + /// <param name="functions">The initial functions to be available as part of the plugin.</param> |
| 43 | + /// <exception cref="ArgumentNullException"><paramref name="functions"/> contains a null function.</exception> |
| 44 | + /// <exception cref="ArgumentException"><paramref name="functions"/> contains two functions with the same name.</exception> |
| 45 | + public MutableKernelPlugin(string name, string? description = null, IEnumerable<KernelFunction>? functions = null) : base(name, description) |
| 46 | + { |
| 47 | + this._functions = new Dictionary<string, KernelFunction>(StringComparer.OrdinalIgnoreCase); |
| 48 | + if (functions is not null) |
| 49 | + { |
| 50 | + foreach (KernelFunction f in functions) |
| 51 | + { |
| 52 | + ArgumentNullException.ThrowIfNull(f); |
| 53 | + this._functions.Add(f.Name, f); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /// <inheritdoc/> |
| 59 | + public override int FunctionCount => this._functions.Count; |
| 60 | + |
| 61 | + /// <inheritdoc/> |
| 62 | + public override bool TryGetFunction(string name, [NotNullWhen(true)] out KernelFunction? function) => |
| 63 | + this._functions.TryGetValue(name, out function); |
| 64 | + |
| 65 | + /// <summary>Adds a function to the plugin.</summary> |
| 66 | + /// <param name="function">The function to add.</param> |
| 67 | + /// <exception cref="ArgumentNullException"><paramref name="function"/> is null.</exception> |
| 68 | + /// <exception cref="ArgumentNullException"><paramref name="function"/>'s <see cref="KernelFunction.Name"/> is null.</exception> |
| 69 | + /// <exception cref="ArgumentException">A function with the same <see cref="KernelFunction.Name"/> already exists in this plugin.</exception> |
| 70 | + public void AddFunction(KernelFunction function) |
| 71 | + { |
| 72 | + ArgumentNullException.ThrowIfNull(function); |
| 73 | + this._functions.Add(function.Name, function); |
| 74 | + } |
| 75 | + |
| 76 | + /// <inheritdoc/> |
| 77 | + public override IEnumerator<KernelFunction> GetEnumerator() => this._functions.Values.GetEnumerator(); |
| 78 | + } |
| 79 | +} |
0 commit comments