Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,12 @@ public async Task RefundAgent()
public async Task MultipleAgents()
{
Kernel kernel = this.CreateKernelWithChatCompletion();
var agentPlugin = KernelPluginFactory.CreateFromFunctions("AgentPlugin",
KernelPlugin agentPlugin = AgentKernelPluginFactory.CreateFromAgents("AgentPlugin",
[
AgentKernelFunctionFactory.CreateFromAgent(this.CreateSalesAssistant()),
AgentKernelFunctionFactory.CreateFromAgent(this.CreateRefundAgent())
this.CreateSalesAssistant(),
this.CreateRefundAgent()
]);

kernel.Plugins.Add(agentPlugin);
kernel.AutoFunctionInvocationFilters.Add(new AutoFunctionInvocationFilter(this.Output));

Expand Down
47 changes: 47 additions & 0 deletions dotnet/src/Agents/Core/Functions/AgentKernelPluginFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.SemanticKernel.Agents;

namespace Microsoft.SemanticKernel;

/// <summary>
/// Extension methods for creating KernelPlugin instances from agents.
/// </summary>
[Experimental("SKEXP0110")]
public static class AgentKernelPluginFactory
{
/// <summary>
/// Creates a plugin from a collection of agents. Each agent is converted into a KernelFunction via AgentKernelFunctionFactory.
/// </summary>
/// <param name="pluginName">The name for the plugin.</param>
/// <param name="description">A description of the plugin.</param>
/// <param name="agents">A collection of agents to include in the plugin.</param>
/// <returns>A KernelPlugin with functions derived from the provided agents.</returns>
/// <exception cref="ArgumentNullException">Thrown when agents is null.</exception>
public static KernelPlugin CreateFromAgents(string pluginName, string? description, IEnumerable<Agent> agents)
{
if (agents == null)
{
throw new ArgumentNullException(nameof(agents));
}

KernelFunction[] functions = agents
.Select(agent => AgentKernelFunctionFactory.CreateFromAgent(agent))
.ToArray();

return KernelPluginFactory.CreateFromFunctions(pluginName, description, functions);
}

/// <summary>
/// Creates a plugin from an array of agents. Each agent is converted into a KernelFunction via AgentKernelFunctionFactory.
/// </summary>
/// <param name="pluginName">The name for the plugin.</param>
/// <param name="agents">The agents to include in the plugin.</param>
/// <returns>A KernelPlugin with functions derived from the provided agents.</returns>
public static KernelPlugin CreateFromAgents(string pluginName, params Agent[] agents) =>
CreateFromAgents(pluginName, description: null, agents);
}
Loading