.NET: [BREAKING] Change implementation to use AsFunctionTool extension method to create a function from an agent (#686)

* Change implementation to use AsFunctionTool extension method to create a function from an agent

* Update dotnet/tests/Microsoft.Extensions.AI.Agents.UnitTests/AgentExtensionsTests.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Address code review feedback and add a new sample

* Address some code review feedback

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Mark Wallace
2025-09-15 09:39:38 +01:00
committed by GitHub
Unverified
parent 2a75482777
commit 636e3acf37
8 changed files with 115 additions and 58 deletions
@@ -1,39 +0,0 @@
// Copyright (c) Microsoft. All rights reserved.
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Shared.Diagnostics;
using static Microsoft.Extensions.AI.Agents.OpenTelemetryConsts.GenAI;
namespace Microsoft.Extensions.AI.Agents;
/// <summary>
/// Provides factory methods for creating implementations of <see cref="AIFunction"/> backed by an <see cref="AIAgent" />.
/// </summary>
public static class AgentAIFunctionFactory
{
/// <summary>
/// Creates a <see cref="AIFunction"/> that will invoke the provided Agent.
/// </summary>
/// <param name="agent">The <see cref="Agent" /> to be represented via the created <see cref="AIFunction"/>.</param>
/// <param name="options">Metadata to use to override defaults inferred from <paramref name="agent"/>.</param>
/// <returns>The created <see cref="AIFunction"/> for invoking the <see cref="AIAgent"/>.</returns>
public static AIFunction CreateFromAgent(
AIAgent agent,
AIFunctionFactoryOptions? options = null)
{
Throw.IfNull(agent);
async Task<string> RunAgentAsync(string query, CancellationToken cancellationToken)
{
var response = await agent.RunAsync(query, cancellationToken: cancellationToken).ConfigureAwait(false);
return response.Text;
}
return AIFunctionFactory.Create(RunAgentAsync, options ?? new()
{
Name = agent.Name,
Description = agent.Description,
});
}
}
@@ -1,6 +1,10 @@
// Copyright (c) Microsoft. All rights reserved.
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Extensions.AI.Agents;
@@ -24,4 +28,31 @@ public static class AgentExtensions
EnableSensitiveData = enableSensitiveData ?? false
};
}
/// <summary>
/// Creates a <see cref="AIFunction"/> that will invoke the provided Agent.
/// </summary>
/// <param name="agent">The <see cref="AIAgent" /> to be represented via the created <see cref="AIFunction"/>.</param>
/// <param name="options">Metadata to use to override defaults inferred from <paramref name="agent"/>.</param>
/// <param name="thread">The <see cref="AgentThread"/> to use for the function.</param>
/// <returns>The created <see cref="AIFunction"/> for invoking the <see cref="AIAgent"/>.</returns>
public static AIFunction AsAIFunction(this AIAgent agent, AIFunctionFactoryOptions? options = null, AgentThread? thread = null)
{
Throw.IfNull(agent);
[Description("Invoke an agent to retrieve some information.")]
async Task<string> InvokeAgentAsync(
[Description("Input query to invoke the agent.")] string query,
CancellationToken cancellationToken)
{
var response = await agent.RunAsync(query, thread: thread, cancellationToken: cancellationToken).ConfigureAwait(false);
return response.Text;
}
options ??= new();
options.Name ??= agent.Name;
options.Description ??= agent.Description;
return AIFunctionFactory.Create(InvokeAgentAsync, options);
}
}