| File: ChatCompletion\ChatClientBuilderServiceCollectionExtensions.cs | Web Access |
| Project: src\src\Libraries\Microsoft.Extensions.AI\Microsoft.Extensions.AI.csproj (Microsoft.Extensions.AI) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System; using Microsoft.Extensions.AI; using Microsoft.Shared.Diagnostics; namespace Microsoft.Extensions.DependencyInjection; /// <summary>Provides extension methods for registering <see cref="IChatClient"/> with a <see cref="IServiceCollection"/>.</summary> public static class ChatClientBuilderServiceCollectionExtensions { /// <summary>Registers a singleton <see cref="IChatClient"/> in the <see cref="IServiceCollection"/>.</summary> /// <param name="serviceCollection">The <see cref="IServiceCollection"/> to which the client should be added.</param> /// <param name="innerClient">The inner <see cref="IChatClient"/> that represents the underlying backend.</param> /// <param name="lifetime">The service lifetime for the client. Defaults to <see cref="ServiceLifetime.Singleton"/>.</param> /// <returns>A <see cref="ChatClientBuilder"/> that can be used to build a pipeline around the inner client.</returns> /// <remarks>The client is registered as a singleton service.</remarks> /// <exception cref="ArgumentNullException"><paramref name="serviceCollection"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"><paramref name="innerClient"/> is <see langword="null"/>.</exception> public static ChatClientBuilder AddChatClient( this IServiceCollection serviceCollection, IChatClient innerClient, ServiceLifetime lifetime = ServiceLifetime.Singleton) { _ = Throw.IfNull(serviceCollection); _ = Throw.IfNull(innerClient); return AddChatClient(serviceCollection, _ => innerClient, lifetime); } /// <summary>Registers a singleton <see cref="IChatClient"/> in the <see cref="IServiceCollection"/>.</summary> /// <param name="serviceCollection">The <see cref="IServiceCollection"/> to which the client should be added.</param> /// <param name="innerClientFactory">A callback that produces the inner <see cref="IChatClient"/> that represents the underlying backend.</param> /// <param name="lifetime">The service lifetime for the client. Defaults to <see cref="ServiceLifetime.Singleton"/>.</param> /// <returns>A <see cref="ChatClientBuilder"/> that can be used to build a pipeline around the inner client.</returns> /// <remarks>The client is registered as a singleton service.</remarks> /// <exception cref="ArgumentNullException"><paramref name="serviceCollection"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"><paramref name="innerClientFactory"/> is <see langword="null"/>.</exception> public static ChatClientBuilder AddChatClient( this IServiceCollection serviceCollection, Func<IServiceProvider, IChatClient> innerClientFactory, ServiceLifetime lifetime = ServiceLifetime.Singleton) { _ = Throw.IfNull(serviceCollection); _ = Throw.IfNull(innerClientFactory); var builder = new ChatClientBuilder(innerClientFactory); serviceCollection.Add(new ServiceDescriptor(typeof(IChatClient), builder.Build, lifetime)); return builder; } /// <summary>Registers a keyed singleton <see cref="IChatClient"/> in the <see cref="IServiceCollection"/>.</summary> /// <param name="serviceCollection">The <see cref="IServiceCollection"/> to which the client should be added.</param> /// <param name="serviceKey">The key with which to associate the client.</param> /// <param name="innerClient">The inner <see cref="IChatClient"/> that represents the underlying backend.</param> /// <param name="lifetime">The service lifetime for the client. Defaults to <see cref="ServiceLifetime.Singleton"/>.</param> /// <returns>A <see cref="ChatClientBuilder"/> that can be used to build a pipeline around the inner client.</returns> /// <remarks>The client is registered as a scoped service.</remarks> /// <exception cref="ArgumentNullException"><paramref name="serviceCollection"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"><paramref name="innerClient"/> is <see langword="null"/>.</exception> public static ChatClientBuilder AddKeyedChatClient( this IServiceCollection serviceCollection, object? serviceKey, IChatClient innerClient, ServiceLifetime lifetime = ServiceLifetime.Singleton) { _ = Throw.IfNull(serviceCollection); _ = Throw.IfNull(innerClient); return AddKeyedChatClient(serviceCollection, serviceKey, _ => innerClient, lifetime); } /// <summary>Registers a keyed singleton <see cref="IChatClient"/> in the <see cref="IServiceCollection"/>.</summary> /// <param name="serviceCollection">The <see cref="IServiceCollection"/> to which the client should be added.</param> /// <param name="serviceKey">The key with which to associate the client.</param> /// <param name="innerClientFactory">A callback that produces the inner <see cref="IChatClient"/> that represents the underlying backend.</param> /// <param name="lifetime">The service lifetime for the client. Defaults to <see cref="ServiceLifetime.Singleton"/>.</param> /// <returns>A <see cref="ChatClientBuilder"/> that can be used to build a pipeline around the inner client.</returns> /// <remarks>The client is registered as a scoped service.</remarks> /// <exception cref="ArgumentNullException"><paramref name="serviceCollection"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentNullException"><paramref name="innerClientFactory"/> is <see langword="null"/>.</exception> public static ChatClientBuilder AddKeyedChatClient( this IServiceCollection serviceCollection, object? serviceKey, Func<IServiceProvider, IChatClient> innerClientFactory, ServiceLifetime lifetime = ServiceLifetime.Singleton) { _ = Throw.IfNull(serviceCollection); _ = Throw.IfNull(innerClientFactory); var builder = new ChatClientBuilder(innerClientFactory); serviceCollection.Add(new ServiceDescriptor(typeof(IChatClient), serviceKey, factory: (services, serviceKey) => builder.Build(services), lifetime)); return builder; } }