Share via


AIAgentExtensions.AsAIFunction Method

Definition

Creates an AIFunction that runs the provided AIAgent.

public static Microsoft.Extensions.AI.AIFunction AsAIFunction(this Microsoft.Agents.AI.AIAgent agent, Microsoft.Extensions.AI.AIFunctionFactoryOptions? options = default, Microsoft.Agents.AI.AgentThread? thread = default);
static member AsAIFunction : Microsoft.Agents.AI.AIAgent * Microsoft.Extensions.AI.AIFunctionFactoryOptions * Microsoft.Agents.AI.AgentThread -> Microsoft.Extensions.AI.AIFunction
<Extension()>
Public Function AsAIFunction (agent As AIAgent, Optional options As AIFunctionFactoryOptions = Nothing, Optional thread As AgentThread = Nothing) As AIFunction

Parameters

agent
AIAgent

The AIAgent to be represented as an invocable function.

options
AIFunctionFactoryOptions

Optional metadata to customize the function representation, such as name and description. If not provided, defaults will be inferred from the agent's properties.

thread
AgentThread

Optional AgentThread to use for function invocations. If not provided, a new thread will be created for each function call, which may not preserve conversation context.

Returns

An AIFunction that can be used as a tool by other agents or AI models to invoke this agent.

Exceptions

agent is null.

Remarks

This extension method enables agents to participate in function calling scenarios, where they can be invoked as tools by other agents or AI models. The resulting function accepts a query string as input and returns the agent's response as a string, making it compatible with standard function calling interfaces used by AI models.

The resulting AIFunction is stateful, referencing both the agent and the optional thread. Especially if a specific thread is provided, avoid using the resulting function concurrently in multiple conversations or in requests where the parallel function calls may result in concurrent usage of the thread, as that could lead to undefined and unpredictable behavior.

Applies to