Skip to content

Commit

Permalink
Merge pull request #4 from cnblogs/improve-di-registration
Browse files Browse the repository at this point in the history
refactor: simplify DI registration
  • Loading branch information
cnblogs-dudu authored Feb 13, 2024
2 parents 7956cd9 + 8e73712 commit 3d6084b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -28,4 +30,23 @@ public static IKernelBuilder AddDashScopeChatCompletion(
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
return builder;
}

public static IKernelBuilder AddDashScopeChatCompletion<T>(
this IKernelBuilder builder,
string? serviceId = null,
Action<HttpClient>? 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<T>()
.Build();
builder.Services.TryAddSingleton(config);
}
return builder.AddDashScopeChatCompletion(serviceId, configureClient, configSectionPath);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,8 +11,7 @@ public async Task ChatCompletion_InvokePromptAsync_WorksCorrectly()
{
// Arrange
var builder = Kernel.CreateBuilder();
builder.Services.AddSingleton(GetConfiguration());
builder.AddDashScopeChatCompletion();
builder.AddDashScopeChatCompletion<DashScopeChatCompletionTests>();
var kernel = builder.Build();

var prompt = @"<message role=""user"">博客园是什么网站</message>";
Expand All @@ -40,8 +37,7 @@ public async Task ChatCompletion_InvokePromptStreamingAsync_WorksCorrectly()
{
// Arrange
var builder = Kernel.CreateBuilder();
builder.Services.AddSingleton(GetConfiguration());
builder.AddDashScopeChatCompletion();
builder.AddDashScopeChatCompletion<DashScopeChatCompletionTests>();
var kernel = builder.Build();

// Act
Expand All @@ -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<DashScopeChatCompletionTests>()
.Build();
}
}

0 comments on commit 3d6084b

Please sign in to comment.