From 4b6c32586cb4a4bebb4649ce298b04afc3710a67 Mon Sep 17 00:00:00 2001 From: hchen Date: Thu, 14 Sep 2023 11:42:48 -0500 Subject: [PATCH 1/2] Set default ModelName as null. --- .../Conversations/IConversationStateService.cs | 2 +- .../Conversations/Models/IncomingMessageModel.cs | 2 +- .../Agents/Services/AgentService.GetAgents.cs | 2 +- .../Services/ConversationStateService.cs | 10 +++++++++- .../Infrastructures/CompletionProvider.cs | 11 +++++++---- src/Infrastructure/BotSharp.Core/Routing/Router.cs | 5 ++--- src/Infrastructure/BotSharp.Core/Routing/Simulator.cs | 5 +++++ .../Controllers/ConversationController.cs | 8 ++++---- .../Providers/ChatCompletionProvider.cs | 6 +++--- .../Providers/GPT4CompletionProvider.cs | 7 ------- .../BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs | 4 ++-- 11 files changed, 35 insertions(+), 27 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs index b28d662d..8818eb03 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs @@ -8,7 +8,7 @@ public interface IConversationStateService ConversationState Load(string conversationId); string GetState(string name, string defaultValue = ""); ConversationState GetStates(); - void SetState(string name, string value); + IConversationStateService SetState(string name, string value); void CleanState(); void Save(); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/IncomingMessageModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/IncomingMessageModel.cs index bdc8f3a2..b0186d8a 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/IncomingMessageModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/IncomingMessageModel.cs @@ -12,7 +12,7 @@ public class IncomingMessageModel /// Model name /// [JsonPropertyName("model")] - public virtual string ModelName { get; set; } = "gpt-3.5-turbo"; + public virtual string? ModelName { get; set; } = null; /// /// The sampling temperature to use that controls the apparent creativity of generated completions. diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index 259f126c..a1b832e4 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -1,6 +1,5 @@ using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Repositories; -using System.IO; namespace BotSharp.Core.Agents.Services; @@ -17,6 +16,7 @@ public partial class AgentService return query.ToList(); } + [MemoryCache(10 * 60)] public async Task GetAgent(string id) { var db = _services.GetRequiredService(); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index d28c0bbf..cfbf1bf6 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -29,7 +29,7 @@ public class ConversationStateService : IConversationStateService, IDisposable _states = new ConversationState(); } - public void SetState(string name, string value) + public IConversationStateService SetState(string name, string value) { var hooks = _services.GetServices(); string preValue = _states.ContainsKey(name) ? _states[name] : ""; @@ -43,6 +43,8 @@ public class ConversationStateService : IConversationStateService, IDisposable hook.OnStateChanged(name, preValue, currentValue).Wait(); } } + + return this; } public ConversationState Load(string conversationId) @@ -118,6 +120,12 @@ public class ConversationStateService : IConversationStateService, IDisposable { _states[name] = defaultValue ?? ""; } + + if (_states[name] == null) + { + return defaultValue; + } + return _states[name]; } diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs index 18ecae76..f5dd62db 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/CompletionProvider.cs @@ -4,13 +4,16 @@ namespace BotSharp.Core.Infrastructures; public class CompletionProvider { - public static IChatCompletion GetChatCompletion(IServiceProvider services) + public static IChatCompletion GetChatCompletion(IServiceProvider services, string? model = null) { var completions = services.GetServices(); - // var settings = services.GetRequiredService(); - // completions.FirstOrDefault(x => x.GetType().FullName.EndsWith(settings.ChatCompletion)); + var state = services.GetRequiredService(); - var model = state.GetState("model", "gpt-3.5-turbo"); + if (model == null) + { + model = state.GetState("model", "gpt-3.5-turbo"); + } + return completions.FirstOrDefault(x => x.ModelName == model); } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Router.cs b/src/Infrastructure/BotSharp.Core/Routing/Router.cs index 44269e26..4f750f89 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Router.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Router.cs @@ -1,4 +1,3 @@ -using Aspects.Cache; using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Routing.Models; @@ -53,11 +52,11 @@ public class Router : IAgentRouting public RoutingItem GetRecordByName(string name) { - return GetRoutingRecords().First(x => x.Name.ToLower() == name.ToLower()); + return GetRoutingRecords().FirstOrDefault(x => x.Name.ToLower() == name.ToLower()); } public RoutingItem GetRecordByAgentId(string id) { - return GetRoutingRecords().First(x => x.AgentId == id); + return GetRoutingRecords().FirstOrDefault(x => x.AgentId == id); } } diff --git a/src/Infrastructure/BotSharp.Core/Routing/Simulator.cs b/src/Infrastructure/BotSharp.Core/Routing/Simulator.cs index 54d9ddc3..77905af2 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Simulator.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Simulator.cs @@ -134,6 +134,11 @@ public class Simulator private void SaveStateByArgs(JsonDocument args) { + if (args == null) + { + return; + } + var stateService = _services.GetRequiredService(); if (args.RootElement is JsonElement root) { diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index eabfb666..ac64b87a 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -44,10 +44,10 @@ public class ConversationController : ControllerBase, IApiAdapter { var conv = _services.GetRequiredService(); conv.SetConversationId(conversationId, input.States); - conv.States.SetState("channel", input.Channel); - conv.States.SetState("model", input.ModelName); - conv.States.SetState("temperature", input.Temperature.ToString()); - conv.States.SetState("sampling_factor", input.SamplingFactor.ToString()); + conv.States.SetState("channel", input.Channel) + .SetState("model", input.ModelName) + .SetState("temperature", input.Temperature.ToString()) + .SetState("sampling_factor", input.SamplingFactor.ToString()); var response = new MessageResponseModel(); var stackMsg = new List(); diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index 5ad763d9..65604967 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -20,9 +20,9 @@ namespace BotSharp.Plugin.AzureOpenAI.Providers; public class ChatCompletionProvider : IChatCompletion { - private readonly AzureOpenAiSettings _settings; - private readonly IServiceProvider _services; - private readonly ILogger _logger; + protected readonly AzureOpenAiSettings _settings; + protected readonly IServiceProvider _services; + protected readonly ILogger _logger; public virtual string ModelName => "gpt-3.5-turbo"; diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/GPT4CompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/GPT4CompletionProvider.cs index 21fa7572..bb9dd480 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/GPT4CompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/GPT4CompletionProvider.cs @@ -8,19 +8,12 @@ namespace BotSharp.Plugin.AzureOpenAI.Providers; public class GPT4CompletionProvider : ChatCompletionProvider { - private readonly AzureOpenAiSettings _settings; - private readonly IServiceProvider _services; - private readonly ILogger _logger; - public override string ModelName => "gpt-4"; public GPT4CompletionProvider(AzureOpenAiSettings settings, ILogger logger, IServiceProvider services) : base(settings, logger, services) { - _settings = settings; - _logger = logger; - _services = services; } protected override (OpenAIClient, string) GetClient() diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs index b8f178a4..ea784148 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs @@ -78,8 +78,8 @@ public class ChatbotUiController : ControllerBase, IApiAdapter conv.SetConversationId(input.ConversationId, input.States); conv.States.SetState("model", input.ModelName); conv.States.SetState("channel", "webchat"); - conv.States.SetState("temperature", "0.5"); - conv.States.SetState("sampling_factor", "0.5"); + conv.States.SetState("temperature", input.Temperature.ToString()); + conv.States.SetState("sampling_factor", input.SamplingFactor.ToString()); var result = await conv.SendMessage(input.AgentId, message, From 18e318b2a3e6266ec0250c08adf2534e21da41d1 Mon Sep 17 00:00:00 2001 From: hchen Date: Thu, 14 Sep 2023 20:19:32 -0500 Subject: [PATCH 2/2] disable cache in local --- .../Agents/Services/AgentService.GetAgents.cs | 2 ++ src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs | 7 ++++++- src/Infrastructure/BotSharp.Core/Routing/Router.cs | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index a1b832e4..f65b7748 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -16,7 +16,9 @@ public partial class AgentService return query.ToList(); } +#if !DEBUG [MemoryCache(10 * 60)] +#endif public async Task GetAgent(string id) { var db = _services.GetRequiredService(); diff --git a/src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs b/src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs index 3bc7cc5d..bd2df051 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/RouteToAgentFn.cs @@ -96,7 +96,7 @@ public class RouteToAgentFn : IFunctionCallback { // Add field to args message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "missing_fields", missingFields); - message.ExecutionResult = $"missing some information"; + message.ExecutionResult = $"missing some information: [{string.Join(',', missingFields)}]"; // Handle redirect if (!string.IsNullOrEmpty(routingRule.RedirectTo)) @@ -107,6 +107,11 @@ public class RouteToAgentFn : IFunctionCallback // Add redirected agent message.FunctionArgs = AppendPropertyToArgs(message.FunctionArgs, "redirect_to", agent.Name); } + else + { + // back to router + agentId = message.CurrentAgentId; + } } return missingFields.Any(); diff --git a/src/Infrastructure/BotSharp.Core/Routing/Router.cs b/src/Infrastructure/BotSharp.Core/Routing/Router.cs index 4f750f89..1194cc4d 100644 --- a/src/Infrastructure/BotSharp.Core/Routing/Router.cs +++ b/src/Infrastructure/BotSharp.Core/Routing/Router.cs @@ -28,7 +28,9 @@ public class Router : IAgentRouting return await agentService.LoadAgent(AgentId); } +#if !DEBUG [MemoryCache(10 * 60)] +#endif public RoutingItem[] GetRoutingRecords() { var db = _services.GetRequiredService();