Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions SemanticKernelPooling/KernelPoolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class KernelPoolManager : IKernelPoolManager

// Dictionary to maintain round-robin indices for each scope
private readonly ConcurrentDictionary<string, int> _scopeIndices = new();
// Dictionary to maintain round-robin indices for each AIServiceProviderType
private readonly ConcurrentDictionary<AIServiceProviderType, int> _providerTypeIndices = new();
private readonly IConfiguration _configuration;

/// <summary>
Expand Down Expand Up @@ -109,7 +111,7 @@ private async Task<KernelWrapper> GetKernelAsync(AIServiceProviderConfiguration
public async Task<KernelWrapper> 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}");

Expand Down Expand Up @@ -138,12 +140,16 @@ public async Task<KernelWrapper> GetKernelByScopeAsync(string scope)
/// <inheritdoc />
public async Task<KernelWrapper> 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;
}
Expand Down