diff --git a/framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithTemperature.cs b/framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithTemperature.cs deleted file mode 100644 index e34e45b495..0000000000 --- a/framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/Delegates/ChatClientWithTemperature.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.Extensions.AI; - -namespace Volo.Abp.AI.Delegates; - -public class ChatClientWithTemperature : DelegatingChatClient -{ - private readonly float _temperature; - - public ChatClientWithTemperature(IChatClient innerClient, float temperature) - : base(innerClient) - { - _temperature = temperature; - } - - public override Task GetResponseAsync( - IEnumerable messages, - ChatOptions? options = null, - CancellationToken cancellationToken = default) - { - return base.GetResponseAsync( - messages, - BuildChatOptions(options), - cancellationToken - ); - } - - public override IAsyncEnumerable GetStreamingResponseAsync( - IEnumerable messages, - ChatOptions? options = null, - CancellationToken cancellationToken = default) - { - return base.GetStreamingResponseAsync( - messages, - BuildChatOptions(options), - cancellationToken - ); - } - - private ChatOptions BuildChatOptions(ChatOptions? options) - { - if (options?.Temperature != null) - { - return options; - } - - options ??= new ChatOptions(); - - options.Temperature ??= _temperature; - - return options; - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/IKernel.cs b/framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/IKernelAccessor.cs similarity index 54% rename from framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/IKernel.cs rename to framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/IKernelAccessor.cs index 20a002e682..a63efe8186 100644 --- a/framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/IKernel.cs +++ b/framework/src/Volo.Abp.AI.Abstractions/Volo/Abp/AI/IKernelAccessor.cs @@ -2,8 +2,8 @@ using Microsoft.SemanticKernel; namespace Volo.Abp.AI; -public interface IKernel +public interface IKernelAccessor where TWorkSpace : class { - Kernel Kernel { get; } + Kernel? Kernel { get; } } \ No newline at end of file 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 20a6fd3df8..56341005af 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.SemanticKernel; using Volo.Abp.Modularity; +using Microsoft.Extensions.DependencyInjection.Extensions; namespace Volo.Abp.AI; @@ -11,31 +12,34 @@ namespace Volo.Abp.AI; )] public class AbpAIModule : AbpModule { + + public const string DefaultWorkspaceName = "Default"; + public override void PostConfigureServices(ServiceConfigurationContext context) { var options = context.Services.ExecutePreConfiguredActions(); - foreach (var chatClientConfig in options.ChatClients.Values) + foreach (var workspaceConfig in options.Workspaces.Values) { - if (chatClientConfig.Builder == null) + if (workspaceConfig.ChatClient?.Builder is null) { - throw new AbpException("ChatClientBuilder is not properly configured. Set the Builder property."); + continue; } - foreach (var builderConfigurer in chatClientConfig.BuilderConfigurers) + foreach (var builderConfigurer in workspaceConfig.ChatClient.BuilderConfigurers) { - builderConfigurer.Action(chatClientConfig.Builder); + builderConfigurer.Action(workspaceConfig.ChatClient.Builder!); } context.Services.AddKeyedChatClient( - AbpAIOptions.GetChatClientServiceKeyName(chatClientConfig.Name), - provider => chatClientConfig.Builder.Build(provider) + AbpAIOptions.GetChatClientServiceKeyName(workspaceConfig.ChatClient.Name), + provider => workspaceConfig.ChatClient.Builder!.Build(provider) ); - if (chatClientConfig.Name == ChatClientConfigurationDictionary.DefaultChatClientName) + if (workspaceConfig.ChatClient.Name == DefaultWorkspaceName) { context.Services.AddTransient(sp => sp.GetRequiredKeyedService( - AbpAIOptions.GetChatClientServiceKeyName(chatClientConfig.Name) + AbpAIOptions.GetChatClientServiceKeyName(workspaceConfig.ChatClient.Name) ) ); } @@ -43,31 +47,31 @@ public class AbpAIModule : AbpModule context.Services.TryAddTransient(typeof(IChatClient<>), typeof(TypedChatClient<>)); - foreach (var kernelConfig in options.Kernels.Values) + foreach (var workspaceConfig in options.Workspaces.Values) { - if (kernelConfig.Builder == null) + if (workspaceConfig.Kernel?.Builder is null) { - throw new AbpException("KernelBuilder is not properly configured. Set the Builder property."); + continue; } - foreach (var builderConfigurer in kernelConfig.BuilderConfigurers) + foreach (var builderConfigurer in workspaceConfig.Kernel.BuilderConfigurers) { - builderConfigurer.Action(kernelConfig.Builder); + builderConfigurer.Action(workspaceConfig.Kernel.Builder!); } context.Services.AddKeyedSingleton( - AbpAIOptions.GetKernelServiceKeyName(kernelConfig.Name), - (provider, _) => kernelConfig.Builder.Build()); + AbpAIOptions.GetKernelServiceKeyName(workspaceConfig.Kernel.Name), + (provider, _) => workspaceConfig.Kernel.Builder!.Build()); - if (kernelConfig.Name == KernelConfigurationDictionary.DefaultKernelName) + if (workspaceConfig.Kernel.Name == DefaultWorkspaceName) { context.Services.AddSingleton(sp => sp.GetRequiredKeyedService( - AbpAIOptions.GetKernelServiceKeyName(kernelConfig.Name) + AbpAIOptions.GetKernelServiceKeyName(workspaceConfig.Kernel.Name) ) ); } } - context.Services.TryAddTransient(typeof(IKernel<>), typeof(TypedKernel<>)); + context.Services.TryAddTransient(typeof(IKernelAccessor<>), typeof(TypedKernelAccessor<>)); } } \ 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 41b5840345..17aa2ac971 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIOptions.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIOptions.cs @@ -1,3 +1,6 @@ +using System; +using System.Collections.Generic; + namespace Volo.Abp.AI; public class AbpAIOptions @@ -5,8 +8,7 @@ public class AbpAIOptions public const string ChatClientServiceKeyNamePrefix = "Abp.AI.ChatClient_"; public const string KernelServiceKeyNamePrefix = "Abp.AI.Kernel_"; - public ChatClientConfigurationDictionary ChatClients { get; } = new(); - public KernelConfigurationDictionary Kernels { get; } = new(); + public WorkspaceConfigurationDictionary Workspaces { get; } = new(); public static string GetChatClientServiceKeyName(string name) { @@ -17,4 +19,30 @@ public class AbpAIOptions { return $"{KernelServiceKeyNamePrefix}{name}"; } +} + +public class WorkspaceConfiguration +{ + public ChatClientConfiguration? ChatClient { get; set; } + public KernelConfiguration? Kernel { get; set; } +} + +public class WorkspaceConfigurationDictionary : Dictionary +{ + public void Configure(Action configureAction) + where TWorkSpace : class + { + Configure(WorkspaceNameAttribute.GetWorkspaceName(), configureAction); + } + + public void Configure(string name, Action configureAction) + { + if (!TryGetValue(name, out var configuration)) + { + configuration = new WorkspaceConfiguration(); + this[name] = configuration; + } + + configureAction(configuration); + } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientConfigurationCollection.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientConfigurationCollection.cs deleted file mode 100644 index 418013c85a..0000000000 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientConfigurationCollection.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Volo.Abp.AI; - -public class ChatClientConfigurationDictionary : Dictionary -{ - public static string DefaultChatClientName => "Default"; - - // TODO: Consider to rename to Configure only. - public void ConfigureDefault(Action configureAction) => - Configure(DefaultChatClientName, configureAction); - - public void Configure(Action configureAction) - { - Configure(typeof(T), configureAction); - } - - public void Configure(Type chatClientType, Action configureAction) - { - Configure( - WorkspaceNameAttribute.GetWorkspaceName(chatClientType), - configureAction - ); - } - - public void Configure(string name, Action configureAction) - { - if (!this.TryGetValue(name, out var configuration)) - { - configuration = new ChatClientConfiguration(name); - this[name] = configuration; - } - - configureAction(configuration); - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithTemperatureExtensions.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithTemperatureExtensions.cs deleted file mode 100644 index f0680dcc1a..0000000000 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/Extensions/ChatClientWithTemperatureExtensions.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.Extensions.AI; -using Volo.Abp.AI.Delegates; - -namespace Volo.Abp.AI.Extensions; - -public static class ChatClientWithTemperatureExtensions -{ - public static ChatClientBuilder UseTemperature(this ChatClientBuilder builder, float temperature) - { - return builder.Use(chatClient => new ChatClientWithTemperature(chatClient, temperature)); - } -} \ No newline at end of file diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/KernelConfigurationCollection.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/KernelConfigurationCollection.cs deleted file mode 100644 index 979f425b28..0000000000 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/KernelConfigurationCollection.cs +++ /dev/null @@ -1,38 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Volo.Abp.AI; - -public class KernelConfigurationDictionary : Dictionary -{ - public static string DefaultKernelName => "Default"; - - public void ConfigureDefault(Action configureAction) => - Configure(DefaultKernelName, configureAction); - - public void Configure(Action configureAction) - { - Configure(typeof(TWorkSpace), configureAction); - } - - public void Configure(Type workspaceType, Action configureAction) - { - Configure( - WorkspaceNameAttribute.GetWorkspaceName(workspaceType), - configureAction - ); - } - - public void Configure(string name, Action configureAction) - { - if (!TryGetValue(name, out var configuration)) - { - configuration = new KernelConfiguration(name); - this[name] = configuration; - } - - configureAction(configuration); - } -} - - diff --git a/framework/src/Volo.Abp.AI/Volo/Abp/AI/TypedKernel.cs b/framework/src/Volo.Abp.AI/Volo/Abp/AI/TypedKernel.cs index 2d46136c0b..09d0132676 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/TypedKernel.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/TypedKernel.cs @@ -4,14 +4,14 @@ using Microsoft.SemanticKernel; namespace Volo.Abp.AI; -public class TypedKernel : IKernel +public class TypedKernelAccessor : IKernelAccessor where TWorkSpace : class { - public Kernel Kernel { get; } + public Kernel? Kernel { get; } - public TypedKernel(IServiceProvider serviceProvider) + public TypedKernelAccessor(IServiceProvider serviceProvider) { - Kernel = serviceProvider.GetRequiredKeyedService( + Kernel = serviceProvider.GetKeyedService( AbpAIOptions.GetKernelServiceKeyName( WorkspaceNameAttribute.GetWorkspaceName())); }