From 8e737122055dda7de2ba4e1af32a6c16e258fc3f Mon Sep 17 00:00:00 2001 From: dudu Date: Tue, 13 Feb 2024 21:32:20 +0800 Subject: [PATCH] refactor: simplify DI registration --- .../DashScopeServiceCollectionExtensions.cs | 21 +++++++++++++++++++ .../DashScopeChatCompletionTests.cs | 19 +++-------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs b/src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs index a6b0bc9..48fba34 100644 --- a/src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs +++ b/src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs @@ -1,5 +1,7 @@ using Cnblogs.SemanticKernel.Connectors.DashScope; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.SemanticKernel.ChatCompletion; namespace Microsoft.SemanticKernel; @@ -28,4 +30,23 @@ public static IKernelBuilder AddDashScopeChatCompletion( builder.Services.AddKeyedSingleton(serviceId, factory); return builder; } + + public static IKernelBuilder AddDashScopeChatCompletion( + this IKernelBuilder builder, + string? serviceId = null, + Action? configureClient = null, + string configSectionPath = "dashscope") where T : class + { + if (!builder.Services.Any(s => s.ServiceType == typeof(IConfiguration))) + { + IConfiguration config = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddEnvironmentVariables() + .AddJsonFile("appsettings.json", true) + .AddUserSecrets() + .Build(); + builder.Services.TryAddSingleton(config); + } + return builder.AddDashScopeChatCompletion(serviceId, configureClient, configSectionPath); + } } \ No newline at end of file diff --git a/test/SemanticKernel.DashScope.IntegrationTest/DashScopeChatCompletionTests.cs b/test/SemanticKernel.DashScope.IntegrationTest/DashScopeChatCompletionTests.cs index afb43fa..9d98f53 100644 --- a/test/SemanticKernel.DashScope.IntegrationTest/DashScopeChatCompletionTests.cs +++ b/test/SemanticKernel.DashScope.IntegrationTest/DashScopeChatCompletionTests.cs @@ -1,7 +1,5 @@ using System.Diagnostics; using System.Text; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; using Microsoft.SemanticKernel; namespace SemanticKernel.DashScope.IntegrationTest; @@ -13,8 +11,7 @@ public async Task ChatCompletion_InvokePromptAsync_WorksCorrectly() { // Arrange var builder = Kernel.CreateBuilder(); - builder.Services.AddSingleton(GetConfiguration()); - builder.AddDashScopeChatCompletion(); + builder.AddDashScopeChatCompletion(); var kernel = builder.Build(); var prompt = @"博客园是什么网站"; @@ -40,8 +37,7 @@ public async Task ChatCompletion_InvokePromptStreamingAsync_WorksCorrectly() { // Arrange var builder = Kernel.CreateBuilder(); - builder.Services.AddSingleton(GetConfiguration()); - builder.AddDashScopeChatCompletion(); + builder.AddDashScopeChatCompletion(); var kernel = builder.Build(); // Act @@ -52,18 +48,9 @@ public async Task ChatCompletion_InvokePromptStreamingAsync_WorksCorrectly() var sb = new StringBuilder(); await foreach (var message in result) { - Trace.WriteLine(message); + Trace.Write(message); sb.Append(message); } Assert.Contains("博客园", sb.ToString()); } - - private static IConfiguration GetConfiguration() - { - return new ConfigurationBuilder() - .SetBasePath(Directory.GetCurrentDirectory()) - .AddJsonFile("appsettings.json") - .AddUserSecrets() - .Build(); - } } \ No newline at end of file