From 12e7d32be16f4106a25440cbf274d6c072cf2fe2 Mon Sep 17 00:00:00 2001 From: hchen2020 <101423@smsassist.com> Date: Wed, 19 Jul 2023 17:30:23 -0500 Subject: [PATCH] Update IConversationCompletionHook. --- .../ConversationCompletionHookBase.cs | 23 +++++++++++++++++++ .../IConversationCompletionHook.cs | 8 +++++-- .../Conversations/IConversationService.cs | 1 + .../Services/ConversationService.cs | 19 +++++++++++++-- .../Providers/ChatCompletionProvider.cs | 6 ++--- .../ChatbotUiController.cs | 12 ++++++++-- .../ViewModels/OpenAiMessageInput.cs | 3 +++ 7 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationCompletionHookBase.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationCompletionHookBase.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationCompletionHookBase.cs new file mode 100644 index 00000000..cdd30b99 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/ConversationCompletionHookBase.cs @@ -0,0 +1,23 @@ +using BotSharp.Abstraction.Conversations.Models; + +namespace BotSharp.Abstraction.Conversations; + +public abstract class ConversationCompletionHookBase +{ + protected Agent _agent; + public Agent Agent => _agent; + + protected Conversation _conversation; + public Conversation Conversation => _conversation; + + protected List _dialogs; + public List Dialogs => _dialogs; + + public IConversationCompletionHook SetContexts(Agent agent, Conversation conversation, List dialogs) + { + _agent = agent; + _conversation = conversation; + _dialogs = dialogs; + return this as IConversationCompletionHook; + } +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationCompletionHook.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationCompletionHook.cs index be68b6d6..866b5fc9 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationCompletionHook.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationCompletionHook.cs @@ -4,6 +4,10 @@ namespace BotSharp.Abstraction.Conversations; public interface IConversationCompletionHook { - Task BeforeCompletion(Agent agent, List conversations); - Task AfterCompletion(Agent agent, string response); + Agent Agent { get; } + Conversation Conversation { get; } + List Dialogs { get; } + IConversationCompletionHook SetContexts(Agent agent, Conversation conversation, List dialogs); + Task BeforeCompletion(); + Task AfterCompletion(string response); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 72d2b3ae..8d87c51e 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -5,6 +5,7 @@ namespace BotSharp.Abstraction.Conversations; public interface IConversationService { Task NewConversation(Conversation conversation); + Task GetConversation(string id); Task> GetConversations(); Task DeleteConversation(string id); Task SendMessage(string agentId, string conversationId, RoleDialogModel lastDalog); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 5f5c1d33..9b820f2a 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -28,6 +28,16 @@ public class ConversationService : IConversationService throw new NotImplementedException(); } + public async Task GetConversation(string id) + { + var db = _services.GetRequiredService(); + var query = from sess in db.Conversation + where sess.Id == id + orderby sess.CreatedTime descending + select sess.ToConversation(); + return query.FirstOrDefault(); + } + public async Task> GetConversations() { var db = _services.GetRequiredService(); @@ -77,6 +87,7 @@ public class ConversationService : IConversationService public async Task SendMessage(string agentId, string conversationId, List wholeDialogs) { var agent = await _services.GetRequiredService().GetAgent(agentId); + var converation = await GetConversation(conversationId); // Get relevant domain knowledge if (_settings.EnableKnowledgeBase) @@ -94,14 +105,18 @@ public class ConversationService : IConversationService // Before chat completion hook var hooks = _services.GetServices().ToList(); - hooks.ForEach(hook => hook.BeforeCompletion(agent, wholeDialogs)); + hooks.ForEach(hook => + { + hook.SetContexts(agent, converation, wholeDialogs) + .BeforeCompletion(); + }); var response = await chatCompletion.GetChatCompletionsAsync(agent, wholeDialogs); // After chat completion hook hooks.ForEach(async hook => { - response = await hook.AfterCompletion(agent, response); + response = await hook.AfterCompletion(response); }); return response; diff --git a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs index cb11d9a5..2b1e3f23 100644 --- a/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs +++ b/src/Plugins/BotSharp.Plugin.AzureOpenAI/Providers/ChatCompletionProvider.cs @@ -51,11 +51,11 @@ public class ChatCompletionProvider : IChatCompletion if (!string.IsNullOrEmpty(sampleText)) { var lines = sampleText.Split('\n'); - for (int i = 0; i < lines.Length; i++) + for (int i = 0; i < lines.Length; i += 3) { var line = lines[i]; - var role = line.Substring(0, line.IndexOf(' ') - 1); - var content = line.Substring(line.IndexOf(' ') + 1); + var role = line.Substring(0, line.IndexOf(' ') - 1).Trim(); + var content = line.Substring(line.IndexOf(' ') + 1).Trim(); samples.Add(new RoleDialogModel { diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs index 18975e5b..8cefdeef 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs @@ -59,7 +59,7 @@ public class ChatbotUiController : ControllerBase, IApiAdapter Response.Headers.Add(HeaderNames.Connection, "keep-alive"); var outputStream = Response.Body; - var conversations = input.Messages.Skip(1).Select(x => new RoleDialogModel + var conversations = input.Messages.Select(x => new RoleDialogModel { Role = x.Role, Text = x.Content @@ -67,7 +67,15 @@ public class ChatbotUiController : ControllerBase, IApiAdapter var conv = _services.GetRequiredService(); - var result = await conv.SendMessage("", "", conversations.Last()); + // Check if this conversation exists + var converation = await conv.GetConversation(input.ConversationId); + var sess = new Conversation + { + AgentId = input.AgentId + }; + sess = await conv.NewConversation(sess); + + var result = await conv.SendMessage(input.AgentId, input.ConversationId, conversations); await OnChunkReceived(outputStream, result); await OnEventCompleted(outputStream); diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs index 2cbbf286..8bff419d 100644 --- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs +++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs @@ -6,11 +6,14 @@ namespace BotSharp.Plugin.ChatbotUI.ViewModels; public class OpenAiMessageInput { + public string AgentId { get; set; } + public string ConversationId { get; set; } public string Model { get; set; } = string.Empty; public List Messages { get; set; } = new List(); [JsonPropertyName("max_tokens")] public int MaxTokens { get; set; } = 4000; public bool Stream { get; set; } = true; + public string? SystemPrompt { get; set; } public float Temperature { get; set; } = 0.9f; public override string ToString()