From ddaffcffb5c001e921cf0a4a36a3a07ef7a24855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Tue, 23 Sep 2025 15:38:24 +0300 Subject: [PATCH] Refactored AI package --- .../Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs | 28 +++++++++++-------- .../Volo.Abp.AI/Volo/Abp/AI/AbpAIOptions.cs | 2 +- .../Volo/Abp/AI/AbpAIWorkspaceOptions.cs | 2 +- .../Volo/Abp/AI/ChatClientConfiguration.cs | 2 -- ...pedKernelAccessor.cs => KernelAccessor.cs} | 4 +-- 5 files changed, 20 insertions(+), 18 deletions(-) rename framework/src/Volo.Abp.AI/Volo/Abp/AI/{TypedKernelAccessor.cs => KernelAccessor.cs} (73%) diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs index f13a6e6f02..7917fc0fbb 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs @@ -5,7 +5,6 @@ using Microsoft.Extensions.AI; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.SemanticKernel; -using Microsoft.SemanticKernel.ChatCompletion; using Volo.Abp.Modularity; namespace Volo.Abp.AI; @@ -17,38 +16,43 @@ public class AbpAIModule : AbpModule { public const string DefaultWorkspaceName = "Default"; - public override void PostConfigureServices(ServiceConfigurationContext context) + public override void ConfigureServices(ServiceConfigurationContext context) { var options = context.Services.ExecutePreConfiguredActions(); context.Services.Configure(workspaceOptions => { - workspaceOptions.ConfiguredWorkspaceNames.UnionWith(options.Workspaces.Select(x => x.Key).ToArray()); + workspaceOptions.ConfiguredWorkspaceNames.AddIfNotContains( + options.Workspaces.Select(x => x.Key) + ); }); + + //TODO: Refactor & merge foreach loops foreach (var workspaceConfig in options.Workspaces.Values) { - if (workspaceConfig.ChatClient?.Builder is null) + if (workspaceConfig.ChatClient.Builder is null) { continue; } foreach (var builderConfigurer in workspaceConfig.ChatClient.BuilderConfigurers) { - builderConfigurer.Action(workspaceConfig.ChatClient.Builder!); + builderConfigurer.Action(workspaceConfig.ChatClient.Builder); } + var serviceName = AbpAIOptions.GetChatClientServiceKeyName(workspaceConfig.Name); + context.Services.AddKeyedChatClient( - AbpAIOptions.GetChatClientServiceKeyName(workspaceConfig.Name), - provider => workspaceConfig.ChatClient.Builder!.Build(provider), + serviceName, + provider => workspaceConfig.ChatClient.Builder.Build(provider), ServiceLifetime.Transient ); if (workspaceConfig.Name == DefaultWorkspaceName) { - context.Services.AddTransient(sp => sp.GetRequiredKeyedService( - AbpAIOptions.GetChatClientServiceKeyName(workspaceConfig.Name) - ) + context.Services.AddTransient( + sp => sp.GetRequiredKeyedService(serviceName) ); } } @@ -57,7 +61,7 @@ public class AbpAIModule : AbpModule foreach (var workspaceConfig in options.Workspaces.Values) { - if (workspaceConfig.Kernel?.Builder is null) + if (workspaceConfig.Kernel.Builder is null) { continue; } @@ -91,6 +95,6 @@ public class AbpAIModule : AbpModule } } - context.Services.TryAddTransient(typeof(IKernelAccessor<>), typeof(TypedKernelAccessor<>)); + context.Services.TryAddTransient(typeof(IKernelAccessor<>), typeof(KernelAccessor<>)); } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIOptions.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIOptions.cs index b5639580d7..9fc4ffb979 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIOptions.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIOptions.cs @@ -1,6 +1,6 @@ namespace Volo.Abp.AI; -public class AbpAIOptions +public class AbpAIOptions //TODO: Rename to AbpAIWorkspaceOptions { public const string ChatClientServiceKeyNamePrefix = "Abp.AI.ChatClient_"; public const string KernelServiceKeyNamePrefix = "Abp.AI.Kernel_"; diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIWorkspaceOptions.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIWorkspaceOptions.cs index 1fad742c04..0e5e2c0720 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIWorkspaceOptions.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIWorkspaceOptions.cs @@ -2,7 +2,7 @@ using System.Collections.Generic; namespace Volo.Abp.AI; -public class AbpAIWorkspaceOptions +public class AbpAIWorkspaceOptions //TODO: Rename to AbpAIOptions { public HashSet ConfiguredWorkspaceNames { get; } = new(); } \ No newline at end of file diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientConfiguration.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientConfiguration.cs index 830d6fd87c..601a6bbe07 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientConfiguration.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientConfiguration.cs @@ -9,8 +9,6 @@ public class ChatClientConfiguration public BuilderConfigurerList BuilderConfigurers { get; } = new(); - // TODO: Base chat client (for inheriting a chat client configuration from some other one) - public void ConfigureBuilder(Action configureAction) { BuilderConfigurers.Add(configureAction); diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/TypedKernelAccessor.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/KernelAccessor.cs similarity index 73% rename from framework/src/Volo.Abp.AI/Volo/Abp/AI/TypedKernelAccessor.cs rename to framework/src/Volo.Abp.AI/Volo/Abp/AI/KernelAccessor.cs index 09d0132676..e0bef8c1f8 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/TypedKernelAccessor.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/KernelAccessor.cs @@ -4,12 +4,12 @@ using Microsoft.SemanticKernel; namespace Volo.Abp.AI; -public class TypedKernelAccessor : IKernelAccessor +public class KernelAccessor : IKernelAccessor where TWorkSpace : class { public Kernel? Kernel { get; } - public TypedKernelAccessor(IServiceProvider serviceProvider) + public KernelAccessor(IServiceProvider serviceProvider) { Kernel = serviceProvider.GetKeyedService( AbpAIOptions.GetKernelServiceKeyName(