Share via


AIAgentBuilder.Use Method

Definition

Overloads

Use(Func<AIAgent,AIAgent>)

Adds a factory for an intermediate agent to the agent pipeline.

Use(Func<AIAgent,IServiceProvider,AIAgent>)

Adds a factory for an intermediate agent to the agent pipeline.

Use(Func<IEnumerable<ChatMessage>,AgentThread,AgentRunOptions, Func<IEnumerable<ChatMessage>,AgentThread,AgentRunOptions,CancellationToken, Task>,CancellationToken,Task>)

Adds to the agent pipeline an anonymous delegating agent based on a delegate that provides an implementation for both RunAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken) and RunStreamingAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken).

Use(Func<IEnumerable<ChatMessage>,AgentThread,AgentRunOptions, AIAgent,CancellationToken,Task<AgentRunResponse>>, Func<IEnumerable<ChatMessage>, AgentThread,AgentRunOptions,AIAgent,CancellationToken,IAsyncEnumerable<AgentRunResponseUpdate>>)

Adds to the agent pipeline an anonymous delegating agent based on a delegate that provides an implementation for both RunAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken) and RunStreamingAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken).

Use(Func<AIAgent,AIAgent>)

Adds a factory for an intermediate agent to the agent pipeline.

public Microsoft.Agents.AI.AIAgentBuilder Use(Func<Microsoft.Agents.AI.AIAgent,Microsoft.Agents.AI.AIAgent> agentFactory);
member this.Use : Func<Microsoft.Agents.AI.AIAgent, Microsoft.Agents.AI.AIAgent> -> Microsoft.Agents.AI.AIAgentBuilder
Public Function Use (agentFactory As Func(Of AIAgent, AIAgent)) As AIAgentBuilder

Parameters

agentFactory
Func<AIAgent,AIAgent>

The agent factory function.

Returns

The updated AIAgentBuilder instance.

Exceptions

agentFactory is null.

Applies to

Use(Func<AIAgent,IServiceProvider,AIAgent>)

Adds a factory for an intermediate agent to the agent pipeline.

public Microsoft.Agents.AI.AIAgentBuilder Use(Func<Microsoft.Agents.AI.AIAgent,IServiceProvider,Microsoft.Agents.AI.AIAgent> agentFactory);
member this.Use : Func<Microsoft.Agents.AI.AIAgent, IServiceProvider, Microsoft.Agents.AI.AIAgent> -> Microsoft.Agents.AI.AIAgentBuilder
Public Function Use (agentFactory As Func(Of AIAgent, IServiceProvider, AIAgent)) As AIAgentBuilder

Parameters

agentFactory
Func<AIAgent,IServiceProvider,AIAgent>

The agent factory function.

Returns

The updated AIAgentBuilder instance.

Exceptions

agentFactory is null.

Applies to

Use(Func<IEnumerable<ChatMessage>,AgentThread,AgentRunOptions, Func<IEnumerable<ChatMessage>,AgentThread,AgentRunOptions,CancellationToken, Task>,CancellationToken,Task>)

Adds to the agent pipeline an anonymous delegating agent based on a delegate that provides an implementation for both RunAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken) and RunStreamingAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken).

public Microsoft.Agents.AI.AIAgentBuilder Use(Func<System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage>,Microsoft.Agents.AI.AgentThread?,Microsoft.Agents.AI.AgentRunOptions?,Func<System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage>,Microsoft.Agents.AI.AgentThread?,Microsoft.Agents.AI.AgentRunOptions?,System.Threading.CancellationToken,System.Threading.Tasks.Task>,System.Threading.CancellationToken,System.Threading.Tasks.Task> sharedFunc);
member this.Use : Func<seq<Microsoft.Extensions.AI.ChatMessage>, Microsoft.Agents.AI.AgentThread, Microsoft.Agents.AI.AgentRunOptions, Func<seq<Microsoft.Extensions.AI.ChatMessage>, Microsoft.Agents.AI.AgentThread, Microsoft.Agents.AI.AgentRunOptions, System.Threading.CancellationToken, System.Threading.Tasks.Task>, System.Threading.CancellationToken, System.Threading.Tasks.Task> -> Microsoft.Agents.AI.AIAgentBuilder
Public Function Use (sharedFunc As Func(Of IEnumerable(Of ChatMessage), AgentThread, AgentRunOptions, Func(Of IEnumerable(Of ChatMessage), AgentThread, AgentRunOptions, CancellationToken, Task), CancellationToken, Task)) As AIAgentBuilder

Parameters

sharedFunc
Func<IEnumerable<ChatMessage>,AgentThread,AgentRunOptions,Func<IEnumerable<ChatMessage>,AgentThread,AgentRunOptions,CancellationToken,Task>,CancellationToken,Task>

A delegate that provides the implementation for both RunAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken) and RunStreamingAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken). This delegate is invoked with the list of messages, the agent thread, the run options, a delegate that represents invoking the inner agent, and a cancellation token. The delegate should be passed whatever messages, thread, options, and cancellation token should be passed along to the next stage in the pipeline. It will handle both the non-streaming and streaming cases.

Returns

The updated AIAgentBuilder instance.

Exceptions

sharedFunc is null.

Remarks

This overload can be used when the anonymous implementation needs to provide pre-processing and/or post-processing, but doesn't need to interact with the results of the operation, which will come from the inner agent.

Applies to

Use(Func<IEnumerable<ChatMessage>,AgentThread,AgentRunOptions, AIAgent,CancellationToken,Task<AgentRunResponse>>, Func<IEnumerable<ChatMessage>, AgentThread,AgentRunOptions,AIAgent,CancellationToken,IAsyncEnumerable<AgentRunResponseUpdate>>)

Adds to the agent pipeline an anonymous delegating agent based on a delegate that provides an implementation for both RunAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken) and RunStreamingAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken).

public Microsoft.Agents.AI.AIAgentBuilder Use(Func<System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage>,Microsoft.Agents.AI.AgentThread?,Microsoft.Agents.AI.AgentRunOptions?,Microsoft.Agents.AI.AIAgent,System.Threading.CancellationToken,System.Threading.Tasks.Task<Microsoft.Agents.AI.AgentRunResponse>>? runFunc, Func<System.Collections.Generic.IEnumerable<Microsoft.Extensions.AI.ChatMessage>,Microsoft.Agents.AI.AgentThread?,Microsoft.Agents.AI.AgentRunOptions?,Microsoft.Agents.AI.AIAgent,System.Threading.CancellationToken,System.Collections.Generic.IAsyncEnumerable<Microsoft.Agents.AI.AgentRunResponseUpdate>>? runStreamingFunc);
member this.Use : Func<seq<Microsoft.Extensions.AI.ChatMessage>, Microsoft.Agents.AI.AgentThread, Microsoft.Agents.AI.AgentRunOptions, Microsoft.Agents.AI.AIAgent, System.Threading.CancellationToken, System.Threading.Tasks.Task<Microsoft.Agents.AI.AgentRunResponse>> * Func<seq<Microsoft.Extensions.AI.ChatMessage>, Microsoft.Agents.AI.AgentThread, Microsoft.Agents.AI.AgentRunOptions, Microsoft.Agents.AI.AIAgent, System.Threading.CancellationToken, System.Collections.Generic.IAsyncEnumerable<Microsoft.Agents.AI.AgentRunResponseUpdate>> -> Microsoft.Agents.AI.AIAgentBuilder
Public Function Use (runFunc As Func(Of IEnumerable(Of ChatMessage), AgentThread, AgentRunOptions, AIAgent, CancellationToken, Task(Of AgentRunResponse)), runStreamingFunc As Func(Of IEnumerable(Of ChatMessage), AgentThread, AgentRunOptions, AIAgent, CancellationToken, IAsyncEnumerable(Of AgentRunResponseUpdate))) As AIAgentBuilder

Parameters

runFunc
Func<IEnumerable<ChatMessage>,AgentThread,AgentRunOptions,AIAgent,CancellationToken,Task<AgentRunResponse>>

A delegate that provides the implementation for RunAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken). When null, runStreamingFunc must be non-null, and the implementation of RunAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken) will use runStreamingFunc for the implementation.

Returns

The updated AIAgentBuilder instance.

Exceptions

Both runFunc and runStreamingFunc are null.

Remarks

One or both delegates can be provided. If both are provided, they will be used for their respective methods: runFunc will provide the implementation of RunAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken), and runStreamingFunc will provide the implementation of RunStreamingAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken). If only one of the delegates is provided, it will be used for both methods. That means that if runFunc is supplied without runStreamingFunc, the implementation of RunStreamingAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken) will employ limited streaming, as it will be operating on the batch output produced by runFunc. And if runStreamingFunc is supplied without runFunc, the implementation of RunAsync(IEnumerable<ChatMessage>, AgentThread, AgentRunOptions, CancellationToken) will be implemented by combining the updates from runStreamingFunc.

Applies to