Skip to content

Commit 28ae362

Browse files
authored
Merge pull request #8 from cnblogs/add-extensions-for-service-collection
refactor: add extensions for IServiceCollection
2 parents 5950df4 + 5bbb21f commit 28ae362

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,30 @@ public static IKernelBuilder AddDashScopeChatCompletion(
1414
Action<DashScopeClientOptions>? configureOptions = null,
1515
Action<HttpClient>? configureClient = null,
1616
string configSectionPath = "dashscope")
17+
{
18+
builder.Services.AddDashScopeChatCompletion(serviceId, configureOptions, configureClient, configSectionPath);
19+
return builder;
20+
}
21+
22+
public static IServiceCollection AddDashScopeChatCompletion(
23+
this IServiceCollection services,
24+
string? serviceId = null,
25+
Action<DashScopeClientOptions>? configureOptions = null,
26+
Action<HttpClient>? configureClient = null,
27+
string configSectionPath = "dashscope")
1728
{
1829
Func<IServiceProvider, object?, DashScopeChatCompletionService> factory = (serviceProvider, _) =>
1930
serviceProvider.GetRequiredService<DashScopeChatCompletionService>();
2031

21-
var optionsBuilder = builder.Services.AddOptions<DashScopeClientOptions>().BindConfiguration(configSectionPath);
32+
var optionsBuilder = services.AddOptions<DashScopeClientOptions>().BindConfiguration(configSectionPath);
2233
if (configureOptions != null) optionsBuilder.PostConfigure(configureOptions);
2334

2435
var httpClientBuilder = configureClient == null
25-
? builder.Services.AddHttpClient<DashScopeChatCompletionService>()
26-
: builder.Services.AddHttpClient<DashScopeChatCompletionService>(configureClient);
36+
? services.AddHttpClient<DashScopeChatCompletionService>()
37+
: services.AddHttpClient<DashScopeChatCompletionService>(configureClient);
2738

28-
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
29-
return builder;
39+
services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
40+
return services;
3041
}
3142

3243
public static IKernelBuilder AddDashScopeChatCompletion<T>(
@@ -38,7 +49,20 @@ public static IKernelBuilder AddDashScopeChatCompletion<T>(
3849
Action<HttpClient>? configureClient = null,
3950
string configSectionPath = "dashscope") where T : class
4051
{
41-
builder.Services.AddConfiguration<T>();
52+
builder.Services.AddDashScopeChatCompletion<T>(modelId, apiKey, serviceId, configureOptions, configureClient);
53+
return builder;
54+
}
55+
56+
public static IServiceCollection AddDashScopeChatCompletion<T>(
57+
this IServiceCollection services,
58+
string? modelId = null,
59+
string? apiKey = null,
60+
string? serviceId = null,
61+
Action<DashScopeClientOptions>? configureOptions = null,
62+
Action<HttpClient>? configureClient = null,
63+
string configSectionPath = "dashscope") where T : class
64+
{
65+
services.AddConfiguration<T>();
4266

4367
void AggConfigureOptions(DashScopeClientOptions options)
4468
{
@@ -47,7 +71,7 @@ void AggConfigureOptions(DashScopeClientOptions options)
4771
configureOptions?.Invoke(options);
4872
}
4973

50-
return builder.AddDashScopeChatCompletion(serviceId, AggConfigureOptions, configureClient, configSectionPath);
74+
return services.AddDashScopeChatCompletion(serviceId, AggConfigureOptions, configureClient, configSectionPath);
5175
}
5276

5377
public static IKernelBuilder AddDashScopeChatCompletion(
@@ -56,6 +80,17 @@ public static IKernelBuilder AddDashScopeChatCompletion(
5680
string apiKey,
5781
string? serviceId = null,
5882
Action<HttpClient>? configureClient = null)
83+
{
84+
builder.Services.AddDashScopeChatCompletion(modelId, apiKey, serviceId, configureClient);
85+
return builder;
86+
}
87+
88+
public static IServiceCollection AddDashScopeChatCompletion(
89+
this IServiceCollection services,
90+
string modelId,
91+
string apiKey,
92+
string? serviceId = null,
93+
Action<HttpClient>? configureClient = null)
5994
{
6095
Func<IServiceProvider, object?, DashScopeChatCompletionService> factory = (serviceProvider, _) =>
6196
{
@@ -65,9 +100,9 @@ public static IKernelBuilder AddDashScopeChatCompletion(
65100
return new DashScopeChatCompletionService(options, httpClient);
66101
};
67102

68-
builder.Services.AddHttpClient();
69-
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
70-
return builder;
103+
services.AddHttpClient();
104+
services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
105+
return services;
71106
}
72107

73108
private static IServiceCollection AddConfiguration<T>(this IServiceCollection services) where T : class

test/SemanticKernel.DashScope.IntegrationTest/DashScopeChatCompletionTests.cs renamed to test/SemanticKernel.DashScope.IntegrationTest/ChatCompletionTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace SemanticKernel.DashScope.IntegrationTest;
66

7-
public class DashScopeChatCompletionTests
7+
public class ChatCompletionTests
88
{
99
[Fact]
1010
public async Task ChatCompletion_InvokePromptAsync_WorksCorrectly()
1111
{
1212
// Arrange
1313
var builder = Kernel.CreateBuilder();
14-
builder.AddDashScopeChatCompletion<DashScopeChatCompletionTests>();
14+
builder.Services.AddDashScopeChatCompletion<ChatCompletionTests>();
1515
var kernel = builder.Build();
1616

1717
var prompt = @"<message role=""user"">博客园是什么网站</message>";
@@ -37,7 +37,7 @@ public async Task ChatCompletion_InvokePromptStreamingAsync_WorksCorrectly()
3737
{
3838
// Arrange
3939
var builder = Kernel.CreateBuilder();
40-
builder.AddDashScopeChatCompletion<DashScopeChatCompletionTests>();
40+
builder.Services.AddDashScopeChatCompletion<ChatCompletionTests>();
4141
var kernel = builder.Build();
4242

4343
// Act

0 commit comments

Comments
 (0)