From ed8b3aa717580330ae29d3a22b6c2547c11d0db3 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Fri, 21 Feb 2025 11:37:36 -0600 Subject: [PATCH] add llm configs --- .../MLTasks/ILlmProviderService.cs | 1 + .../MLTasks/Settings/LlmConfigOptions.cs | 9 ++++ .../MLTasks/Settings/LlmProviderSetting.cs | 6 +-- .../Infrastructures/LlmProviderService.cs | 46 +++++++++++++++++++ .../Controllers/LlmProviderController.cs | 7 +++ 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmConfigOptions.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ILlmProviderService.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ILlmProviderService.cs index 201f1342..2a06ece6 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ILlmProviderService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ILlmProviderService.cs @@ -8,4 +8,5 @@ public interface ILlmProviderService List GetProviders(); LlmModelSetting GetProviderModel(string provider, string id, bool? multiModal = null, bool? realTime = false, bool imageGenerate = false); List GetProviderModels(string provider); + List GetLlmConfigs(LlmConfigOptions? options = null); } diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmConfigOptions.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmConfigOptions.cs new file mode 100644 index 00000000..81668059 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmConfigOptions.cs @@ -0,0 +1,9 @@ +namespace BotSharp.Abstraction.MLTasks.Settings; + +public class LlmConfigOptions +{ + public LlmModelType? Type { get; set; } + public bool? MultiModal { get; set; } + public bool? RealTime { get; set; } + public bool? ImageGeneration { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmProviderSetting.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmProviderSetting.cs index b3a9d28e..705a9129 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmProviderSetting.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/Settings/LlmProviderSetting.cs @@ -2,11 +2,9 @@ namespace BotSharp.Abstraction.MLTasks.Settings; public class LlmProviderSetting { - public string Provider { get; set; } - = "azure-openai"; + public string Provider { get; set; } = "azure-openai"; - public List Models { get; set; } - = new List(); + public List Models { get; set; } = []; public override string ToString() { diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderService.cs index e1934f60..6d62c227 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderService.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/LlmProviderService.cs @@ -103,4 +103,50 @@ public class LlmProviderService : ILlmProviderService return modelSetting; } + + + public List GetLlmConfigs(LlmConfigOptions? options = null) + { + var settingService = _services.GetRequiredService(); + var providers = settingService.Bind>($"LlmProviders"); + var configs = new List(); + + if (providers.IsNullOrEmpty()) return configs; + + if (options == null) return providers ?? []; + + foreach (var provider in providers) + { + var models = provider.Models ?? []; + if (options.Type.HasValue) + { + models = models.Where(x => x.Type == options.Type.Value).ToList(); + } + + if (options.MultiModal.HasValue) + { + models = models.Where(x => x.MultiModal == options.MultiModal.Value).ToList(); + } + + if (options.ImageGeneration.HasValue) + { + models = models.Where(x => x.ImageGeneration == options.ImageGeneration.Value).ToList(); + } + + if (options.RealTime.HasValue) + { + models = models.Where(x => x.RealTime == options.RealTime.Value).ToList(); + } + + if (models.IsNullOrEmpty()) + { + continue; + } + + provider.Models = models; + configs.Add(provider); + } + + return configs; + } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LlmProviderController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LlmProviderController.cs index 02282b85..6b87bec2 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LlmProviderController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LlmProviderController.cs @@ -27,4 +27,11 @@ public class LlmProviderController : ControllerBase var list = _llmProvider.GetProviderModels(provider); return list.Where(x => x.Type == LlmModelType.Chat); } + + [HttpGet("/llm-configs")] + public List GetLlmConfigs([FromQuery] LlmConfigOptions options) + { + var configs = _llmProvider.GetLlmConfigs(options); + return configs; + } }