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 759a14ba66..831b29658a 100644 --- a/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIModule.cs @@ -1,8 +1,22 @@ -using Volo.Abp.Modularity; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Modularity; namespace Volo.Abp.AI; public class AbpAIModule : AbpModule { - + public override void ConfigureServices(ServiceConfigurationContext context) + { + var options = context.Services.ExecutePreConfiguredActions(); + + foreach (var chatClientConfig in options.ChatClients.GetAll()) + { + if (chatClientConfig.Builder == null) + { + throw new AbpException("ChatClientBuilder is not properly configured. Set the Builder."); + } + + context.Services.AddKeyedChatClient(chatClientConfig.Name, provider => chatClientConfig.Builder.Build(provider)); + } + } } \ 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 new file mode 100644 index 0000000000..8354b995db --- /dev/null +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/AbpAIOptions.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.AI; + +public class AbpAIOptions +{ + public ChatClientConfigurationCollection ChatClients { 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 new file mode 100644 index 0000000000..df8be6e04e --- /dev/null +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientConfiguration.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.AI; + +namespace Volo.Abp.AI; + +public class ChatClientConfiguration +{ + public string Name { get; } + public ChatClientBuilder? Builder { get; set; } + + public ChatClientConfiguration(string name) + { + Name = name; + } +} \ 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 new file mode 100644 index 0000000000..cf7e42f50f --- /dev/null +++ b/framework/src/Volo.Abp.AI/Volo/Abp/AI/ChatClientConfigurationCollection.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; + +namespace Volo.Abp.AI; + +public class ChatClientConfigurationCollection +{ + private readonly ConcurrentDictionary _chatClients = new(); + + public void Configure(string name, Action configureAction) + { + if (!_chatClients.TryGetValue(name, out var configuration)) + { + configuration = new ChatClientConfiguration(name); + _chatClients[name] = configuration; + } + + configureAction(configuration); + } + + internal IEnumerable GetAll() + { + return _chatClients.Values; + } +} \ No newline at end of file