Skip to content

Commit

Permalink
Merge pull request #5 from cnblogs/regsiter-di-without-iconfiguration
Browse files Browse the repository at this point in the history
refactor: register DI without IConfiguration
  • Loading branch information
cnblogs-dudu authored Feb 13, 2024
2 parents a2d94c2 + dcebeb7 commit 141c081
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,24 @@ public static IKernelBuilder AddDashScopeChatCompletion<T>(
}
return builder.AddDashScopeChatCompletion(serviceId, configureClient, configSectionPath);
}

public static IKernelBuilder AddDashScopeChatCompletion(
this IKernelBuilder builder,
string modelId,
string apiKey,
string? serviceId = null,
Action<HttpClient>? configureClient = null)
{
Func<IServiceProvider, object?, DashScopeChatCompletionService> factory = (serviceProvider, _) =>
{
var options = new DashScopeClientOptions { ModelId = modelId, ApiKey = apiKey };
var httpClient = serviceProvider.GetRequiredService<HttpClient>();
configureClient?.Invoke(httpClient);
return new DashScopeChatCompletionService(options, httpClient);
};

builder.Services.AddHttpClient();
builder.Services.AddKeyedSingleton<IChatCompletionService>(serviceId, factory);
return builder;
}
}

0 comments on commit 141c081

Please sign in to comment.