Share via


FunctionInvokingChatClient Class

Definition

A delegating chat client that invokes functions defined on ChatOptions. Include this in a chat pipeline to resolve function calls automatically.

public ref class FunctionInvokingChatClient : Microsoft::Extensions::AI::DelegatingChatClient
public class FunctionInvokingChatClient : Microsoft.Extensions.AI.DelegatingChatClient
type FunctionInvokingChatClient = class
    inherit DelegatingChatClient
Public Class FunctionInvokingChatClient
Inherits DelegatingChatClient
Inheritance
FunctionInvokingChatClient

Remarks

When this client receives a FunctionCallContent in a chat response from its inner IChatClient, it responds by invoking the corresponding AIFunction defined in Tools (or in AdditionalTools), producing a FunctionResultContent that it sends back to the inner client. This loop is repeated until there are no more function calls to make, or until another stop condition is met, such as hitting MaximumIterationsPerRequest.

If a requested function is an AIFunctionDeclaration but not an AIFunction, the FunctionInvokingChatClient will not attempt to invoke it, and instead allow that FunctionCallContent to pass back out to the caller. It is then that caller's responsibility to create the appropriate FunctionResultContent for that call and send it back as part of a subsequent request.

Further, if a requested function is an ApprovalRequiredAIFunction, the FunctionInvokingChatClient will not attempt to invoke it directly. Instead, it will replace that FunctionCallContent with a FunctionApprovalRequestContent that wraps the FunctionCallContent and indicates that the function requires approval before it can be invoked. The caller is then responsible for responding to that approval request by sending a corresponding FunctionApprovalResponseContent in a subsequent request. The FunctionInvokingChatClient will then process that approval response and invoke the function as appropriate.

Due to the nature of interactions with an underlying IChatClient, if any FunctionCallContent is received for a function that requires approval, all received FunctionCallContent in that same response will also require approval, even if they were not ApprovalRequiredAIFunction instances. If this is a concern, consider requesting that multiple tool call requests not be made in a single response, by setting AllowMultipleToolCalls to false.

A FunctionInvokingChatClient instance is thread-safe for concurrent use so long as the AIFunction instances employed as part of the supplied ChatOptions are also safe. The AllowConcurrentInvocation property can be used to control whether multiple function invocation requests as part of the same request are invocable concurrently, but even with that set to false (the default), multiple concurrent requests to this same instance and using the same tools could result in those tools being used concurrently (one per request). For example, a function that accesses the HttpContext of a specific ASP.NET web request should only be used as part of a single ChatOptions at a time, and only with AllowConcurrentInvocation set to false, in case the inner client decided to issue multiple invocation requests to that same function.

Constructors

FunctionInvokingChatClient(IChatClient, ILoggerFactory, IServiceProvider)

Initializes a new instance of the FunctionInvokingChatClient class.

Properties

AdditionalTools

Gets or sets a collection of additional tools the client is able to invoke.

AllowConcurrentInvocation

Gets or sets a value indicating whether to allow concurrent invocation of functions.

CurrentContext

Gets or sets the FunctionInvocationContext for the current function invocation.

FunctionInvocationServices

Gets the IServiceProvider specified when constructing the FunctionInvokingChatClient, if any.

FunctionInvoker

Gets or sets a delegate used to invoke AIFunction instances.

IncludeDetailedErrors

Gets or sets a value indicating whether detailed exception information should be included in the chat history when calling the underlying IChatClient.

InnerClient

Gets the inner IChatClient.

(Inherited from DelegatingChatClient)
MaximumConsecutiveErrorsPerRequest

Gets or sets the maximum number of consecutive iterations that are allowed to fail with an error.

MaximumIterationsPerRequest

Gets or sets the maximum number of iterations per request.

TerminateOnUnknownCalls

Gets or sets a value indicating whether a request to call an unknown function should terminate the function calling loop.

Methods

CreateResponseMessages(ReadOnlySpan<FunctionInvokingChatClient.FunctionInvocationResult>)

Creates one or more response messages for function invocation results.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

(Inherited from DelegatingChatClient)
Dispose(Boolean)

Provides a mechanism for releasing unmanaged resources.

(Inherited from DelegatingChatClient)
GetResponseAsync(IEnumerable<ChatMessage>, ChatOptions, CancellationToken)

Sends chat messages and returns the response.

GetService(Type, Object)

Asks the IChatClient for an object of the specified type serviceType.

(Inherited from DelegatingChatClient)
GetStreamingResponseAsync(IEnumerable<ChatMessage>, ChatOptions, CancellationToken)

Sends chat messages and streams the response.

InvokeFunctionAsync(FunctionInvocationContext, CancellationToken)

This method will invoke the function within the try block.

Extension Methods

AsBuilder(IChatClient)

Creates a new ChatClientBuilder using innerClient as its inner client.

GetRequiredService(IChatClient, Type, Object)

Asks the IChatClient for an object of the specified type serviceType and throws an exception if one isn't available.

GetRequiredService<TService>(IChatClient, Object)

Asks the IChatClient for an object of type TService and throws an exception if one isn't available.

GetResponseAsync(IChatClient, ChatMessage, ChatOptions, CancellationToken)

Sends a chat message and returns the response messages.

GetResponseAsync(IChatClient, String, ChatOptions, CancellationToken)

Sends a user chat text message and returns the response messages.

GetService<TService>(IChatClient, Object)

Asks the IChatClient for an object of type TService.

GetStreamingResponseAsync(IChatClient, ChatMessage, ChatOptions, CancellationToken)

Sends a chat message and streams the response messages.

GetStreamingResponseAsync(IChatClient, String, ChatOptions, CancellationToken)

Sends a user chat text message and streams the response messages.

GetResponseAsync<T>(IChatClient, ChatMessage, ChatOptions, Nullable<Boolean>, CancellationToken)

Sends a chat message, requesting a response matching the type T.

GetResponseAsync<T>(IChatClient, ChatMessage, JsonSerializerOptions, ChatOptions, Nullable<Boolean>, CancellationToken)

Sends a chat message, requesting a response matching the type T.

GetResponseAsync<T>(IChatClient, IEnumerable<ChatMessage>, ChatOptions, Nullable<Boolean>, CancellationToken)

Sends chat messages, requesting a response matching the type T.

GetResponseAsync<T>(IChatClient, IEnumerable<ChatMessage>, JsonSerializerOptions, ChatOptions, Nullable<Boolean>, CancellationToken)

Sends chat messages, requesting a response matching the type T.

GetResponseAsync<T>(IChatClient, String, ChatOptions, Nullable<Boolean>, CancellationToken)

Sends a user chat text message, requesting a response matching the type T.

GetResponseAsync<T>(IChatClient, String, JsonSerializerOptions, ChatOptions, Nullable<Boolean>, CancellationToken)

Sends a user chat text message, requesting a response matching the type T.

Applies to