From 2a456df8465140f1c4026c4a795deb4c3cfc8b65 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 20:40:24 -0500 Subject: [PATCH 1/7] temp save --- .../Conversations/IConversationService.cs | 2 ++ .../BotSharp.Core/BotSharp.Core.csproj | 6 +++- .../Services/ConversationService.Summary.cs | 32 +++++++++++++++++++ .../templates/conversation.summary.liquid | 6 ++++ .../Controllers/ConversationController.cs | 7 ++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs create mode 100644 src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 9df6ae7f6..18cee8fc2 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -53,4 +53,6 @@ Task SendMessage(string agentId, /// Append user init words /// Task UpdateBreakpoint(bool resetStates = false, string? reason = null); + + Task GetConversationSummary(string conversationId); } diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index 0ddc52e83..10e21b3cc 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -1,4 +1,4 @@ - + netstandard2.1 @@ -56,6 +56,7 @@ + @@ -142,6 +143,9 @@ PreserveNewest + + PreserveNewest + PreserveNewest diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs new file mode 100644 index 000000000..53c6470a4 --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -0,0 +1,32 @@ +using BotSharp.Abstraction.Infrastructures.Enums; +using BotSharp.Abstraction.Templating; + +namespace BotSharp.Core.Conversations.Services; + +public partial class ConversationService +{ + public async Task GetConversationSummary(string conversationId) + { + if (string.IsNullOrEmpty(conversationId)) return string.Empty; + + var dialogs = _storage.GetDialogs(conversationId); + + return string.Empty; + } + + private IEnumerable BuildConversationContent(List dialogs) + { + + } + + private string GetPrompt(Agent router, List dialogs) + { + var template = router.Templates.First(x => x.Name == "conversation.summary").Content; + + var render = _services.GetRequiredService(); + return render.Render(template, new Dictionary + { + { "conversation", } + }); + } +} diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid new file mode 100644 index 000000000..3b6bb53fb --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -0,0 +1,6 @@ +Please follow these steps to summarize the conversation: +1. Read the [CONVERSATION] content. +2. Summarize the conversation in one sentence. + +[CONVERSATION] +{{ conversation }} \ No newline at end of file diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index fd66b2254..ef44472a9 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -134,6 +134,13 @@ public async Task GetConversation([FromRoute] string conv return result; } + [HttpGet("/conversation/{conversationId}/summary")] + public async Task GetConversationSummary([FromRoute] string conversationId) + { + var service = _services.GetRequiredService(); + return await service.GetConversationSummary(conversationId); + } + [HttpGet("/conversation/{conversationId}/user")] public async Task GetConversationUser([FromRoute] string conversationId) { From 6b5d77604e635b614f836106817c25c4d3957f8c Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 21:30:57 -0500 Subject: [PATCH 2/7] add conversation summary --- .../Services/ConversationService.Summary.cs | 48 +++++++++++++++---- .../Services/ConversationService.cs | 2 + .../templates/conversation.summary.liquid | 7 +-- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 53c6470a4..00924b931 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -1,4 +1,4 @@ -using BotSharp.Abstraction.Infrastructures.Enums; +using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Templating; namespace BotSharp.Core.Conversations.Services; @@ -9,24 +9,52 @@ public async Task GetConversationSummary(string conversationId) { if (string.IsNullOrEmpty(conversationId)) return string.Empty; + var routing = _services.GetRequiredService(); + var agentService = _services.GetRequiredService(); + var dialogs = _storage.GetDialogs(conversationId); + if (dialogs.IsNullOrEmpty()) return string.Empty; + + var router = await agentService.LoadAgent(AIAssistant); + var content = await routing.GetConversationContent(dialogs); + var prompt = GetPrompt(router, content); + var summary = await Summarize(router, prompt, dialogs); - return string.Empty; + return summary; } - private IEnumerable BuildConversationContent(List dialogs) + private string GetPrompt(Agent agent, string content) { - + var template = agent.Templates.First(x => x.Name == "conversation.summary").Content; + var render = _services.GetRequiredService(); + return render.Render(template, new Dictionary { }); } - private string GetPrompt(Agent router, List dialogs) + private async Task Summarize(Agent agent, string prompt, List dialogs) { - var template = router.Templates.First(x => x.Name == "conversation.summary").Content; + var provider = agent.LlmConfig.Provider; + var model = agent.LlmConfig.Model; - var render = _services.GetRequiredService(); - return render.Render(template, new Dictionary + if (provider == null || model == null) { - { "conversation", } - }); + var agentSettings = _services.GetRequiredService(); + provider = agentSettings.LlmConfig.Provider; + model = agentSettings.LlmConfig.Model; + } + + var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model); + var response = await chatCompletion.GetChatCompletions(new Agent + { + Id = agent.Id, + Name = agent.Name, + Instruction = prompt + }, dialogs); + + return response.Content; + } + + private void SaveState(string summary) + { + _state.SetState("conversation_summary", summary, source: StateSource.Application); } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 4de36b9dc..8e1132263 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -12,6 +12,8 @@ public partial class ConversationService : IConversationService private readonly IConversationStorage _storage; private readonly IConversationStateService _state; private string _conversationId; + private const string AIAssistant = "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a"; + public string ConversationId => _conversationId; public IConversationStateService States => _state; diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid index 3b6bb53fb..a935e497b 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -1,6 +1 @@ -Please follow these steps to summarize the conversation: -1. Read the [CONVERSATION] content. -2. Summarize the conversation in one sentence. - -[CONVERSATION] -{{ conversation }} \ No newline at end of file +Please summarize the conversation. \ No newline at end of file From 953a53c4bc00eb798b48efb418bb39d6a9ea65aa Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 22:19:06 -0500 Subject: [PATCH 3/7] clean code --- .../Conversations/Services/ConversationService.Summary.cs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 00924b931..252d1e7fa 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -1,4 +1,3 @@ -using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Templating; namespace BotSharp.Core.Conversations.Services; @@ -52,9 +51,4 @@ private async Task Summarize(Agent agent, string prompt, List Date: Sun, 26 May 2024 22:20:27 -0500 Subject: [PATCH 4/7] clean code --- .../Conversations/Services/ConversationService.Summary.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 252d1e7fa..d76678cc6 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -15,14 +15,13 @@ public async Task GetConversationSummary(string conversationId) if (dialogs.IsNullOrEmpty()) return string.Empty; var router = await agentService.LoadAgent(AIAssistant); - var content = await routing.GetConversationContent(dialogs); - var prompt = GetPrompt(router, content); + var prompt = GetPrompt(router); var summary = await Summarize(router, prompt, dialogs); return summary; } - private string GetPrompt(Agent agent, string content) + private string GetPrompt(Agent agent) { var template = agent.Templates.First(x => x.Name == "conversation.summary").Content; var render = _services.GetRequiredService(); From b8becca94542c0fabb384df4712f0b72551b67d7 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 22:20:56 -0500 Subject: [PATCH 5/7] minor change --- .../Conversations/Services/ConversationService.Summary.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index d76678cc6..4a547204c 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -30,8 +30,8 @@ private string GetPrompt(Agent agent) private async Task Summarize(Agent agent, string prompt, List dialogs) { - var provider = agent.LlmConfig.Provider; - var model = agent.LlmConfig.Model; + var provider = agent?.LlmConfig?.Provider; + var model = agent?.LlmConfig?.Model; if (provider == null || model == null) { From a5dad4dee48b54b369299b205b8e329d6b79a5b5 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Mon, 27 May 2024 12:12:39 -0500 Subject: [PATCH 6/7] refine summary prompt --- .../Services/ConversationService.Summary.cs | 28 ++++++++++++++----- .../templates/conversation.summary.liquid | 9 +++++- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 4a547204c..25d754d66 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.Templating; namespace BotSharp.Core.Conversations.Services; @@ -30,17 +31,30 @@ private string GetPrompt(Agent agent) private async Task Summarize(Agent agent, string prompt, List dialogs) { - var provider = agent?.LlmConfig?.Provider; - var model = agent?.LlmConfig?.Model; + var provider = "openai"; + string? model; - if (provider == null || model == null) + var providerService = _services.GetRequiredService(); + var modelSettings = providerService.GetProviderModels(provider); + var modelSetting = modelSettings.FirstOrDefault(x => x.Name.IsEqualTo("gpt4-turbo") || x.Name.IsEqualTo("gpt-4o")); + + if (modelSetting != null) + { + model = modelSetting.Name; + } + else { - var agentSettings = _services.GetRequiredService(); - provider = agentSettings.LlmConfig.Provider; - model = agentSettings.LlmConfig.Model; + provider = agent?.LlmConfig?.Provider; + model = agent?.LlmConfig?.Model; + if (provider == null || model == null) + { + var agentSettings = _services.GetRequiredService(); + provider = agentSettings.LlmConfig.Provider; + model = agentSettings.LlmConfig.Model; + } } - var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model); + var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider, model); var response = await chatCompletion.GetChatCompletions(new Agent { Id = agent.Id, diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid index a935e497b..08792af33 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -1 +1,8 @@ -Please summarize the conversation. \ No newline at end of file +Please summarize the conversation. + +*** Super Important! Please consider the entire conversation. Do not only consider the recent sentences. *** +** Please do not respond to the latest conversation. +** If there are different topics in the conversation, please summarize each topic in different sentences and list them in bullets. +* Please use concise sentences to summarize each topic. +* Please do not include excessive details in the summaries. +* Please use 'user' instead of 'you', 'he' or 'she'. \ No newline at end of file From 520b91507c4e5964aab0c5a2bf7abee157061608 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Mon, 27 May 2024 12:51:00 -0500 Subject: [PATCH 7/7] minor change --- .../templates/conversation.summary.liquid | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid index 08792af33..e2ffe5db1 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -2,7 +2,7 @@ Please summarize the conversation. *** Super Important! Please consider the entire conversation. Do not only consider the recent sentences. *** ** Please do not respond to the latest conversation. -** If there are different topics in the conversation, please summarize each topic in different sentences and list them in bullets. +** If there are different topics in the conversation, please summarize each topic in different sentences and list them with bullets. * Please use concise sentences to summarize each topic. * Please do not include excessive details in the summaries. * Please use 'user' instead of 'you', 'he' or 'she'. \ No newline at end of file