Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e73712

Browse files
refactor: simplify DI registration
1 parent 7956cd9 commit 8e73712

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

‎src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Cnblogs.SemanticKernel.Connectors.DashScope;
2+
using Microsoft.Extensions.Configuration;
23
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.DependencyInjection.Extensions;
35
using Microsoft.SemanticKernel.ChatCompletion;
46

57
namespace Microsoft.SemanticKernel;
@@ -28,4 +30,23 @@ public static IKernelBuilder AddDashScopeChatCompletion(
2830
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
2931
return builder;
3032
}
33+
34+
public static IKernelBuilder AddDashScopeChatCompletion<T>(
35+
this IKernelBuilder builder,
36+
string? serviceId = null,
37+
Action<HttpClient>? configureClient = null,
38+
string configSectionPath = "dashscope") where T : class
39+
{
40+
if (!builder.Services.Any(s => s.ServiceType == typeof(IConfiguration)))
41+
{
42+
IConfiguration config = new ConfigurationBuilder()
43+
.SetBasePath(Directory.GetCurrentDirectory())
44+
.AddEnvironmentVariables()
45+
.AddJsonFile("appsettings.json", true)
46+
.AddUserSecrets<T>()
47+
.Build();
48+
builder.Services.TryAddSingleton(config);
49+
}
50+
return builder.AddDashScopeChatCompletion(serviceId, configureClient, configSectionPath);
51+
}
3152
}

‎test/SemanticKernel.DashScope.IntegrationTest/DashScopeChatCompletionTests.cs‎

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Diagnostics;
22
using System.Text;
3-
using Microsoft.Extensions.Configuration;
4-
using Microsoft.Extensions.DependencyInjection;
53
using Microsoft.SemanticKernel;
64

75
namespace SemanticKernel.DashScope.IntegrationTest;
@@ -13,8 +11,7 @@ public async Task ChatCompletion_InvokePromptAsync_WorksCorrectly()
1311
{
1412
// Arrange
1513
var builder = Kernel.CreateBuilder();
16-
builder.Services.AddSingleton(GetConfiguration());
17-
builder.AddDashScopeChatCompletion();
14+
builder.AddDashScopeChatCompletion<DashScopeChatCompletionTests>();
1815
var kernel = builder.Build();
1916

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

4743
// Act
@@ -52,18 +48,9 @@ public async Task ChatCompletion_InvokePromptStreamingAsync_WorksCorrectly()
5248
var sb = new StringBuilder();
5349
await foreach (var message in result)
5450
{
55-
Trace.WriteLine(message);
51+
Trace.Write(message);
5652
sb.Append(message);
5753
}
5854
Assert.Contains("博客园", sb.ToString());
5955
}
60-
61-
private static IConfiguration GetConfiguration()
62-
{
63-
return new ConfigurationBuilder()
64-
.SetBasePath(Directory.GetCurrentDirectory())
65-
.AddJsonFile("appsettings.json")
66-
.AddUserSecrets<DashScopeChatCompletionTests>()
67-
.Build();
68-
}
6956
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /