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 decf251

Browse files
refactor: improve DI registration extensions
1 parent c915950 commit decf251

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

‎src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs‎

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,43 @@ public static class DashScopeServiceCollectionExtensions
1111
public static IKernelBuilder AddDashScopeChatCompletion(
1212
this IKernelBuilder builder,
1313
string? serviceId = null,
14+
Action<DashScopeClientOptions>? configureOptions = null,
1415
Action<HttpClient>? configureClient = null,
1516
string configSectionPath = "dashscope")
1617
{
1718
Func<IServiceProvider, object?, DashScopeChatCompletionService> factory = (serviceProvider, _) =>
1819
serviceProvider.GetRequiredService<DashScopeChatCompletionService>();
1920

20-
if (configureClient == null)
21-
{
22-
builder.Services.AddHttpClient<DashScopeChatCompletionService>();
23-
}
24-
else
25-
{
26-
builder.Services.AddHttpClient<DashScopeChatCompletionService>(configureClient);
27-
}
21+
var optionsBuilder = builder.Services.AddOptions<DashScopeClientOptions>().BindConfiguration(configSectionPath);
22+
if (configureOptions != null) optionsBuilder.PostConfigure(configureOptions);
23+
24+
var httpClientBuilder = configureClient == null
25+
? builder.Services.AddHttpClient<DashScopeChatCompletionService>()
26+
: builder.Services.AddHttpClient<DashScopeChatCompletionService>(configureClient);
2827

29-
builder.Services.AddOptions<DashScopeClientOptions>().BindConfiguration(configSectionPath);
3028
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
3129
return builder;
3230
}
3331

3432
public static IKernelBuilder AddDashScopeChatCompletion<T>(
3533
this IKernelBuilder builder,
34+
string? modelId = null,
35+
string? apiKey = null,
3636
string? serviceId = null,
37+
Action<DashScopeClientOptions>? configureOptions = null,
3738
Action<HttpClient>? configureClient = null,
3839
string configSectionPath = "dashscope") where T : class
3940
{
40-
if (!builder.Services.Any(s => s.ServiceType == typeof(IConfiguration)))
41+
builder.Services.AddConfiguration<T>();
42+
43+
void AggConfigureOptions(DashScopeClientOptions options)
4144
{
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);
45+
if (!string.IsNullOrEmpty(modelId)) options.ModelId = modelId;
46+
if (!string.IsNullOrEmpty(apiKey)) options.ApiKey = apiKey;
47+
configureOptions?.Invoke(options);
4948
}
50-
return builder.AddDashScopeChatCompletion(serviceId, configureClient, configSectionPath);
49+
50+
return builder.AddDashScopeChatCompletion(serviceId, AggConfigureOptions, configureClient, configSectionPath);
5151
}
5252

5353
public static IKernelBuilder AddDashScopeChatCompletion(
@@ -69,4 +69,20 @@ public static IKernelBuilder AddDashScopeChatCompletion(
6969
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
7070
return builder;
7171
}
72+
73+
private static IServiceCollection AddConfiguration<T>(this IServiceCollection services) where T : class
74+
{
75+
if (!services.Any(s => s.ServiceType == typeof(IConfiguration)))
76+
{
77+
IConfiguration config = new ConfigurationBuilder()
78+
.SetBasePath(Directory.GetCurrentDirectory())
79+
.AddEnvironmentVariables()
80+
.AddJsonFile("appsettings.json", true)
81+
.AddUserSecrets<T>()
82+
.Build();
83+
services.TryAddSingleton(config);
84+
}
85+
86+
return services;
87+
}
7288
}

0 commit comments

Comments
(0)

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