IChatClient Interface (Microsoft.Extensions.AI) (original) (raw)

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Represents a chat client.

public interface class IChatClient : IDisposable
public interface IChatClient : IDisposable
type IChatClient = interface
    interface IDisposable
Public Interface IChatClient
Implements IDisposable

Derived

Implements

Remarks

Applications must consider risks such as prompt injection attacks, data sizes, and the number of messages sent to the underlying provider or returned from it. Unless a specific IChatClient implementation explicitly documents safeguards for these concerns, the application is expected to implement appropriate protections.

Unless otherwise specified, all members of IChatClient are thread-safe for concurrent use. It is expected that all implementations of IChatClient support being used by multiple requests concurrently. Instances must not be disposed of while the instance is still in use.

However, implementations of IChatClient might mutate the arguments supplied to GetResponseAsync(IEnumerable, ChatOptions, CancellationToken) and GetStreamingResponseAsync(IEnumerable, ChatOptions, CancellationToken), such as by configuring the options instance. Thus, consumers of the interface either should avoid using shared instances of these arguments for concurrent invocations or should otherwise ensure by construction that no IChatClient instances are used which might employ such mutation. For example, the ConfigureOptions method is provided with a callback that could mutate the supplied options argument, and that should be avoided if using a singleton options instance.

Methods

Name Description
Dispose() Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. (Inherited from IDisposable)
GetResponseAsync(IEnumerable, ChatOptions, CancellationToken) Sends chat messages and returns the response.
GetService(Type, Object) Asks the IChatClient for an object of the specified type serviceType.
GetStreamingResponseAsync(IEnumerable, ChatOptions, CancellationToken) Sends chat messages and streams the response.

Extension Methods

Name Description
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 serviceTypeand throws an exception if one isn't available.
GetRequiredService(IChatClient, Object) Asks the IChatClient for an object of type TServiceand 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.
GetResponseAsync(IChatClient, ChatMessage, ChatOptions, Nullable, CancellationToken) Sends a chat message, requesting a response matching the type T.
GetResponseAsync(IChatClient, ChatMessage, JsonSerializerOptions, ChatOptions, Nullable, CancellationToken) Sends a chat message, requesting a response matching the type T.
GetResponseAsync(IChatClient, IEnumerable, ChatOptions, Nullable, CancellationToken) Sends chat messages, requesting a response matching the type T.
GetResponseAsync(IChatClient, IEnumerable, JsonSerializerOptions, ChatOptions, Nullable, CancellationToken) Sends chat messages, requesting a response matching the type T.
GetResponseAsync(IChatClient, String, ChatOptions, Nullable, CancellationToken) Sends a user chat text message, requesting a response matching the type T.
GetResponseAsync(IChatClient, String, JsonSerializerOptions, ChatOptions, Nullable, CancellationToken) Sends a user chat text message, requesting a response matching the type T.
GetService(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.

Applies to

See also