diff --git a/SemanticKernelPooling/KernelPoolManager.cs b/SemanticKernelPooling/KernelPoolManager.cs index 5961f54..02f9c07 100644 --- a/SemanticKernelPooling/KernelPoolManager.cs +++ b/SemanticKernelPooling/KernelPoolManager.cs @@ -19,6 +19,8 @@ public class KernelPoolManager : IKernelPoolManager // Dictionary to maintain round-robin indices for each scope private readonly ConcurrentDictionary _scopeIndices = new(); + // Dictionary to maintain round-robin indices for each AIServiceProviderType + private readonly ConcurrentDictionary _providerTypeIndices = new(); private readonly IConfiguration _configuration; /// @@ -109,7 +111,7 @@ private async Task GetKernelAsync(AIServiceProviderConfiguration public async Task GetKernelByNameAsync(string uniqueName) { //get the service configuration - var config = AIConfigurations.First(c => c.UniqueName == uniqueName); + var config = AIConfigurations.FirstOrDefault(c => c.UniqueName == uniqueName); if (config == null) throw new InvalidOperationException($"No configuration found for kernel pool name {uniqueName}"); @@ -138,12 +140,16 @@ public async Task GetKernelByScopeAsync(string scope) /// public async Task GetKernelAsync(AIServiceProviderType aiServiceProviderType) { - //get the service configuration by type - var config = AIConfigurations.First(c => c.ServiceType == aiServiceProviderType); - if (config == null) + //get all the service configurations by type + var configs = AIConfigurations.Where(c => c.ServiceType == aiServiceProviderType).ToList(); + if (configs.Count == 0) throw new InvalidOperationException($"No configuration found for kernel pool type {aiServiceProviderType}"); - var kernelWrapper = await GetKernelAsync(config); + // Use round-robin selection to choose a configuration + var index = _providerTypeIndices.AddOrUpdate(aiServiceProviderType, 0, (_, oldValue) => (oldValue + 1)% configs.Count); + var selectedConfig = configs[index]; + + var kernelWrapper = await GetKernelAsync(selectedConfig); return kernelWrapper; }