From a4f5af93f72ee574119c683020a408686fd5a832 Mon Sep 17 00:00:00 2001 From: hchen2020 <101423@smsassist.com> Date: Tue, 15 Aug 2023 10:41:34 -0500 Subject: [PATCH 1/2] Add CleanState function. --- .../Conversations/IConversationService.cs | 15 ------------ .../IConversationStateService.cs | 1 + .../Conversations/Models/RoleDialogModel.cs | 4 +++- .../Services/ConversationService.cs | 23 +++++++------------ .../Services/ConversationStateService.cs | 7 ++++-- .../Providers/ChatCompletionProvider.cs | 9 ++++++++ .../ChatbotUiController.cs | 11 +++++---- 7 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 13647bef5..740547a36 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -27,21 +27,6 @@ Task SendMessage(string agentId, Func onMessageReceived, Func onFunctionExecuting); - /// - /// Send message to LLM if frontend passed over the dialog history - /// - /// - /// - /// - /// - /// This delegate is useful when you want to report progress on UI - /// - Task SendMessage(string agentId, - string conversationId, - List wholeDialogs, - Func onMessageReceived, - Func onFunctionExecuting); - List GetDialogHistory(string conversationId, int lastCount = 20); Task CleanHistory(string agentId); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs index 3cac14f85..420f1191b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationStateService.cs @@ -11,5 +11,6 @@ public interface IConversationStateService ConversationState Load(); string GetState(string name); void SetState(string name, string value); + void CleanState(); void Save(); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index d9b047240..dc883adf3 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -23,7 +23,9 @@ public class RoleDialogModel /// When function callback has been executed, system will pass result to LLM again, /// Set this property to True to stop calling LLM. /// - public bool StopSubsequentInteraction { get;set; } + public bool StopSubsequentInteraction { get; set; } + + public bool IsConversationEnd { get; set; } /// /// Channel name diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 68057d35f..8cdfdb231 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Agents.Enums; using BotSharp.Abstraction.Conversations.Models; using BotSharp.Abstraction.Functions; using BotSharp.Abstraction.MLTasks; @@ -79,18 +78,6 @@ public async Task SendMessage(string agentId, string conversationId, var wholeDialogs = GetDialogHistory(conversationId); - var response = await SendMessage(agentId, conversationId, wholeDialogs, - onMessageReceived: onMessageReceived, - onFunctionExecuting: onFunctionExecuting); - - return response; - } - - public async Task SendMessage(string agentId, string conversationId, - List wholeDialogs, - Func onMessageReceived, - Func onFunctionExecuting) - { var converation = await GetConversation(conversationId); // Create conversation if this conversation not exists @@ -109,11 +96,11 @@ public async Task SendMessage(string agentId, string conversationId, stateService.SetConversation(conversationId); stateService.Load(); stateService.SetState("agentId", agentId); - + // load agent var agentService = _services.GetRequiredService(); var agent = await agentService.LoadAgent(agentId); - + // Get relevant domain knowledge /*if (_settings.EnableKnowledgeBase) { @@ -162,6 +149,12 @@ public async Task SendMessage(string agentId, string conversationId, await onMessageReceived(msg); } + + // Clean conversation + if (msg.IsConversationEnd) + { + stateService.CleanState(); + } }); return result; diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index e37c6999f..ea3c26030 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -1,6 +1,4 @@ using BotSharp.Abstraction.Conversations.Models; -using Microsoft.EntityFrameworkCore.Metadata.Internal; -using Microsoft.Extensions.Logging; using System.IO; namespace BotSharp.Core.Conversations.Services; @@ -89,6 +87,11 @@ public void Save() _logger.LogInformation($"Saved state {_conversationId}"); } + public void CleanState() + { + File.Delete(_file); + } + private string GetStorageFile(string conversationId) { var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId); diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index e9506f3dd..e3e146f46 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -124,6 +124,15 @@ public async Task GetChatCompletionsAsync(Agent agent, List x.Role == AgentRole.User) .Select(x => new RoleDialogModel(x.Role, x.Content)) - .ToList(); + .Last(); var conversationService = _services.GetRequiredService(); var result = await conversationService.SendMessage(input.AgentId, - input.ConversationId, - conversations, + input.ConversationId, + conversation, async msg => await OnChunkReceived(outputStream, msg), async fn From 38bf2cdf4ee1fde7fc0cfe21a42a2ffba1598d2c Mon Sep 17 00:00:00 2001 From: hchen2020 <101423@smsassist.com> Date: Tue, 15 Aug 2023 12:21:04 -0500 Subject: [PATCH 2/2] Add GetVectors batch interface. --- .../BotSharp.Abstraction/MLTasks/ITextEmbedding.cs | 1 + .../Plugins/LLamaSharp/TextEmbeddingProvider.cs | 5 +++++ .../Providers/fastTextEmbeddingProvider.cs | 11 +++++++++++ 3 files changed, 17 insertions(+) diff --git a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextEmbedding.cs b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextEmbedding.cs index eb82f0863..8a0df8a5e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextEmbedding.cs +++ b/src/Infrastructure/BotSharp.Abstraction/MLTasks/ITextEmbedding.cs @@ -4,4 +4,5 @@ public interface ITextEmbedding { int Dimension { get; } float[] GetVector(string text); + List GetVectors(List texts); } diff --git a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/TextEmbeddingProvider.cs b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/TextEmbeddingProvider.cs index daa1ee8df..5608971a4 100644 --- a/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/TextEmbeddingProvider.cs +++ b/src/Infrastructure/BotSharp.Core/Plugins/LLamaSharp/TextEmbeddingProvider.cs @@ -26,4 +26,9 @@ public float[] GetVector(string text) return _embedder.GetEmbeddings(text); } + + public List GetVectors(List texts) + { + throw new NotImplementedException(); + } } diff --git a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs index 6ca628da6..f1141a560 100644 --- a/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs +++ b/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs @@ -1,6 +1,7 @@ using BotSharp.Abstraction.MLTasks; using BotSharp.Plugin.MetaAI.Settings; using FastText.NetWrapper; +using System.Collections.Generic; using System.IO; namespace BotSharp.Plugin.MetaAI.Providers; @@ -42,4 +43,14 @@ public float[] GetVector(string text) return _fastText.GetSentenceVector(text); } + + public List GetVectors(List texts) + { + var vectors = new List(); + for (int i = 0; i < texts.Count; i++) + { + vectors.Add(GetVector(texts[i])); + } + return vectors; + } }