Skip to content

.Net: Add functionality to create and import plugins with YAML functions #10840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

grafanaKibana
Copy link
Contributor

@grafanaKibana grafanaKibana commented Mar 6, 2025

Motivation and Context

Initial Proposal

Motivation

When working with JSON and TXT prompt files, developers can easily add them to the kernel’s plugin collection using IKernelBuilderPlugins.AddFromPromptDirectory. In contrast, YAML prompt files require extra steps: manually reading each file, creating functions via CreateFunctionFromPromptYaml, and then adding them with AddFromFunctions. This extra overhead makes using YAML less streamlined.

Description

Introduce a new extension method, AddFromYamlPromptDirectory, that:

  • Accepts a directory containing YAML prompt files.
  • Reads and converts each YAML prompt into a kernel function using CreateFunctionFromPromptYaml.
  • Automatically groups these functions under a single plugin in the kernel.

For example, with the new method, developers could write:

var pluginsPath = Path.Combine(Directory.GetCurrentDirectory(), "Plugins");
foreach (var directory in Directory.EnumerateDirectories(pluginsPath))
{
    kernelBuilder.Plugins.AddFromYamlPromptDirectory(directory, "Plugin Description", promptTemplateFactory: new KernelPromptTemplateFactory());
}

While currently we have to do something like this (Pseudo Code):

var kernel = kernelBuilder.Build();
var pluginsPath = Path.Combine(Directory.GetCurrentDirectory(), "Plugins");
foreach (var directory in Directory.GetDirectories(pluginsPath))
{
    var functions = new List<KernelFunction>();
    foreach (var file in Directory.GetFiles(directory, "*.yaml"))
    {
        var yamlContent = await File.ReadAllTextAsync(file);
        functions.Add(kernel.CreateFunctionFromPromptYaml(yamlContent));
    }
    var pluginName = Path.GetFileName(directory);
    kernelBuilder.Plugins.AddFromFunctions(pluginName, functions: functions);
}

Contribution Checklist

@grafanaKibana
Copy link
Contributor Author

@grafanaKibana please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

@markwallace-microsoft markwallace-microsoft added the .NET Issue or Pull requests regarding .NET code label Mar 6, 2025
@github-actions github-actions bot changed the title Add functionality to create and import plugins with YAML functions .Net: Add functionality to create and import plugins with YAML functions Mar 6, 2025
@grafanaKibana
Copy link
Contributor Author

Currently getting these errors after build, will appreciate help with that since I didn't managed to find solution to fix it:

/Users/nikitareshetnik/Code/semantic-kernel/dotnet/Directory.Build.targets(11,5): error MSB3073: The command "dotnet format --no-restore -v diag Agents.Bedrock.csproj" exited with code 4. [/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Agents/Bedrock/Agents.Bedrock.csproj::TargetFramework=net8.0]
/Users/nikitareshetnik/Code/semantic-kernel/dotnet/Directory.Build.targets(11,5): error MSB3073: The command "dotnet format --no-restore -v diag " exited with code 4. [/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.Yaml/Functions.Yaml.csproj]
/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.UnitTests/OpenApi/RestApiOperationRunnerTests.cs(1841,48): error CS0433: The type 'HttpResponseStream' exists in both 'Microsoft.SemanticKernel.Plugins.OpenApi, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' and 'Microsoft.SemanticKernel.Yaml, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' [/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.UnitTests/Functions.UnitTests.csproj]
/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.UnitTests/OpenApi/RestApiOperationRunnerTests.cs(1864,50): error CS0433: The type 'HttpResponseStream' exists in both 'Microsoft.SemanticKernel.Plugins.OpenApi, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' and 'Microsoft.SemanticKernel.Yaml, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' [/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.UnitTests/Functions.UnitTests.csproj]

@markwallace-microsoft
Copy link
Member

Currently getting these errors after build, will appreciate help with that since I didn't managed to find solution to fix it:

/Users/nikitareshetnik/Code/semantic-kernel/dotnet/Directory.Build.targets(11,5): error MSB3073: The command "dotnet format --no-restore -v diag Agents.Bedrock.csproj" exited with code 4. [/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Agents/Bedrock/Agents.Bedrock.csproj::TargetFramework=net8.0]
/Users/nikitareshetnik/Code/semantic-kernel/dotnet/Directory.Build.targets(11,5): error MSB3073: The command "dotnet format --no-restore -v diag " exited with code 4. [/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.Yaml/Functions.Yaml.csproj]
/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.UnitTests/OpenApi/RestApiOperationRunnerTests.cs(1841,48): error CS0433: The type 'HttpResponseStream' exists in both 'Microsoft.SemanticKernel.Plugins.OpenApi, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' and 'Microsoft.SemanticKernel.Yaml, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' [/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.UnitTests/Functions.UnitTests.csproj]
/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.UnitTests/OpenApi/RestApiOperationRunnerTests.cs(1864,50): error CS0433: The type 'HttpResponseStream' exists in both 'Microsoft.SemanticKernel.Plugins.OpenApi, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' and 'Microsoft.SemanticKernel.Yaml, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' [/Users/nikitareshetnik/Code/semantic-kernel/dotnet/src/Functions/Functions.UnitTests/Functions.UnitTests.csproj]

Hi @grafanaKibana
thanks for working on this. Regarding the errors:

  1. The Bedrock formatting issues is a known problem and we will get this fixed. You will just need to update your branch.
  2. There is a format error in the new code, if you run dotnet format locally it will tell you exactly where this is
  3. I will ask someone on the team about the OpenAPI errors.

SergeyMenshykh and others added 2 commits March 7, 2025 10:55
…r CS0433: The type 'HttpResponseStream' exists in both 'Microsoft.SemanticKernel.Plugins.OpenApi, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' and 'Microsoft.SemanticKernel.Yaml, Version=1.40.1.0, Culture=neutral, PublicKeyToken=null' [/home/runner/work/semantic-kernel/semantic-kernel/dotnet/src/Functions/Functions.UnitTests/Functions.UnitTests.csproj]
@SergeyMenshykh
Copy link
Member

Hi @grafanaKibana, the error CS0433: The type 'HttpResponseStream' exists in both ... errors have been resolved. Please cover the new API with unit tests to increase code coverage to 80% or higher:
image

@grafanaKibana grafanaKibana marked this pull request as ready for review March 7, 2025 21:08
@grafanaKibana grafanaKibana requested a review from a team as a code owner March 7, 2025 21:08
@markwallace-microsoft markwallace-microsoft added this pull request to the merge queue Mar 18, 2025
Merged via the queue into microsoft:main with commit 66f4cce Mar 18, 2025
20 checks passed
@github-project-automation github-project-automation bot moved this from Community PRs to Sprint: Done in Semantic Kernel Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
.NET Issue or Pull requests regarding .NET code
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants