Skip to content

Commit 44418d1

Browse files
joslatcrickmanmarkwallace-microsoft
authored
.Net: AgentKernelPluginFactory.CreateFromAgents - enable direct support for agents implicitly (#11443)
### Motivation and Context 1. Why - it helps reduce cognitive overload and reduces the code to write, like in python - solves #11439 2. Need to write too much code to set an agent as a plugin: ``` var agentPlugin = KernelPluginFactory.CreateFromFunctions("AgentPlugin", [ AgentKernelFunctionFactory.CreateFromAgent(this.CreateSalesAssistant()), AgentKernelFunctionFactory.CreateFromAgent(this.CreateRefundAgent()) ]); ``` while in Python: `plugins=[billing_agent, refund_agent],` 3. Scenario Making easier and simpler to write agentic code :) 4. Issue: #11439 ### Description Added the change as an extension method as KernelPluginFactory is in SemanticKernel.Core without any reference to Agents.Core where the method is. Unsure you would like to add a reference to it in the SemanticKernel.Core, but it would be a lot more simpler to implement. Now, the changes have been applied here: https://github.com/microsoft/semantic-kernel/blob/0ff97c7fe73c5175df61ae627271341a32983c07/dotnet/samples/GettingStartedWithAgents/Step08_AgentAsKernelFunction.cs#L71 So it would look instead of the above code as: ``` KernelPlugin agentPlugin = KernelPluginFactoryExtensions.CreateFromFunctions("AgentPlugin", [ this.CreateSalesAssistant(), this.CreateRefundAgent() ]); ``` Which is a little less verbose, and more similar to the Python code. ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x ] The code builds clean without any errors or warnings - [x ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ x] All unit tests pass, and I have added new tests where possible - [x ] I didn't break anyone 😄 --------- Co-authored-by: Chris <[email protected]> Co-authored-by: Mark Wallace <[email protected]>
1 parent cd34dcd commit 44418d1

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

dotnet/samples/GettingStartedWithAgents/Step08_AgentAsKernelFunction.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,12 @@ public async Task RefundAgent()
6868
public async Task MultipleAgents()
6969
{
7070
Kernel kernel = this.CreateKernelWithChatCompletion();
71-
var agentPlugin = KernelPluginFactory.CreateFromFunctions("AgentPlugin",
71+
KernelPlugin agentPlugin = AgentKernelPluginFactory.CreateFromAgents("AgentPlugin",
7272
[
73-
AgentKernelFunctionFactory.CreateFromAgent(this.CreateSalesAssistant()),
74-
AgentKernelFunctionFactory.CreateFromAgent(this.CreateRefundAgent())
73+
this.CreateSalesAssistant(),
74+
this.CreateRefundAgent()
7575
]);
76+
7677
kernel.Plugins.Add(agentPlugin);
7778
kernel.AutoFunctionInvocationFilters.Add(new AutoFunctionInvocationFilter(this.Output));
7879

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
7+
using Microsoft.SemanticKernel.Agents;
8+
9+
namespace Microsoft.SemanticKernel;
10+
11+
/// <summary>
12+
/// Extension methods for creating KernelPlugin instances from agents.
13+
/// </summary>
14+
[Experimental("SKEXP0110")]
15+
public static class AgentKernelPluginFactory
16+
{
17+
/// <summary>
18+
/// Creates a plugin from a collection of agents. Each agent is converted into a KernelFunction via AgentKernelFunctionFactory.
19+
/// </summary>
20+
/// <param name="pluginName">The name for the plugin.</param>
21+
/// <param name="description">A description of the plugin.</param>
22+
/// <param name="agents">A collection of agents to include in the plugin.</param>
23+
/// <returns>A KernelPlugin with functions derived from the provided agents.</returns>
24+
/// <exception cref="ArgumentNullException">Thrown when agents is null.</exception>
25+
public static KernelPlugin CreateFromAgents(string pluginName, string? description, IEnumerable<Agent> agents)
26+
{
27+
if (agents == null)
28+
{
29+
throw new ArgumentNullException(nameof(agents));
30+
}
31+
32+
KernelFunction[] functions = agents
33+
.Select(agent => AgentKernelFunctionFactory.CreateFromAgent(agent))
34+
.ToArray();
35+
36+
return KernelPluginFactory.CreateFromFunctions(pluginName, description, functions);
37+
}
38+
39+
/// <summary>
40+
/// Creates a plugin from an array of agents. Each agent is converted into a KernelFunction via AgentKernelFunctionFactory.
41+
/// </summary>
42+
/// <param name="pluginName">The name for the plugin.</param>
43+
/// <param name="agents">The agents to include in the plugin.</param>
44+
/// <returns>A KernelPlugin with functions derived from the provided agents.</returns>
45+
public static KernelPlugin CreateFromAgents(string pluginName, params Agent[] agents) =>
46+
CreateFromAgents(pluginName, description: null, agents);
47+
}

0 commit comments

Comments
 (0)