Skip to content

Commit 141c081

Browse files
authored
Merge pull request #5 from cnblogs/regsiter-di-without-iconfiguration
refactor: register DI without IConfiguration
2 parents a2d94c2 + dcebeb7 commit 141c081

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/SemanticKernel.DashScope/DashScopeServiceCollectionExtensions.cs

+20
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,24 @@ public static IKernelBuilder AddDashScopeChatCompletion<T>(
4949
}
5050
return builder.AddDashScopeChatCompletion(serviceId, configureClient, configSectionPath);
5151
}
52+
53+
public static IKernelBuilder AddDashScopeChatCompletion(
54+
this IKernelBuilder builder,
55+
string modelId,
56+
string apiKey,
57+
string? serviceId = null,
58+
Action<HttpClient>? configureClient = null)
59+
{
60+
Func<IServiceProvider, object?, DashScopeChatCompletionService> factory = (serviceProvider, _) =>
61+
{
62+
var options = new DashScopeClientOptions { ModelId = modelId, ApiKey = apiKey };
63+
var httpClient = serviceProvider.GetRequiredService<HttpClient>();
64+
configureClient?.Invoke(httpClient);
65+
return new DashScopeChatCompletionService(options, httpClient);
66+
};
67+
68+
builder.Services.AddHttpClient();
69+
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
70+
return builder;
71+
}
5272
}

0 commit comments

Comments
 (0)