From decf2516596829f38a0ef40261342dbf549f4ac0 Mon Sep 17 00:00:00 2001 From: dudu Date: Thu, 15 Feb 2024 12:19:11 +0800 Subject: [PATCH] refactor: improve DI registration extensions --- .../DashScopeServiceCollectionExtensions.cs | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs b/src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs index 8c3e5a7..52bfe6c 100644 --- a/src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs +++ b/src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs @@ -11,43 +11,43 @@ public static class DashScopeServiceCollectionExtensions public static IKernelBuilder AddDashScopeChatCompletion( this IKernelBuilder builder, string? serviceId = null, + Action? configureOptions = null, Action? configureClient = null, string configSectionPath = "dashscope") { Func factory = (serviceProvider, _) => serviceProvider.GetRequiredService(); - if (configureClient == null) - { - builder.Services.AddHttpClient(); - } - else - { - builder.Services.AddHttpClient(configureClient); - } + var optionsBuilder = builder.Services.AddOptions().BindConfiguration(configSectionPath); + if (configureOptions != null) optionsBuilder.PostConfigure(configureOptions); + + var httpClientBuilder = configureClient == null + ? builder.Services.AddHttpClient() + : builder.Services.AddHttpClient(configureClient); - builder.Services.AddOptions().BindConfiguration(configSectionPath); builder.Services.AddKeyedSingleton(serviceId, factory); return builder; } public static IKernelBuilder AddDashScopeChatCompletion( this IKernelBuilder builder, + string? modelId = null, + string? apiKey = null, string? serviceId = null, + Action? configureOptions = null, Action? configureClient = null, string configSectionPath = "dashscope") where T : class { - if (!builder.Services.Any(s => s.ServiceType == typeof(IConfiguration))) + builder.Services.AddConfiguration(); + + void AggConfigureOptions(DashScopeClientOptions options) { - IConfiguration config = new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddEnvironmentVariables() - .AddJsonFile("appsettings.json", true) - .AddUserSecrets() - .Build(); - builder.Services.TryAddSingleton(config); + if (!string.IsNullOrEmpty(modelId)) options.ModelId = modelId; + if (!string.IsNullOrEmpty(apiKey)) options.ApiKey = apiKey; + configureOptions?.Invoke(options); } - return builder.AddDashScopeChatCompletion(serviceId, configureClient, configSectionPath); + + return builder.AddDashScopeChatCompletion(serviceId, AggConfigureOptions, configureClient, configSectionPath); } public static IKernelBuilder AddDashScopeChatCompletion( @@ -69,4 +69,20 @@ public static IKernelBuilder AddDashScopeChatCompletion( builder.Services.AddKeyedSingleton(serviceId, factory); return builder; } + + private static IServiceCollection AddConfiguration(this IServiceCollection services) where T : class + { + if (!services.Any(s => s.ServiceType == typeof(IConfiguration))) + { + IConfiguration config = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddEnvironmentVariables() + .AddJsonFile("appsettings.json", true) + .AddUserSecrets() + .Build(); + services.TryAddSingleton(config); + } + + return services; + } } \ No newline at end of file