Merge pull request #471 from iceljc/features/add-conversation-summary

Features/add conversation summary
This commit is contained in:
C. Oceania 2024-05-28 20:55:11 -05:00 committed by GitHub
commit 2f5f920e2c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 91 additions and 1 deletions

View file

@ -54,4 +54,6 @@ public interface IConversationService
/// <param name="excludedStates"></param>
/// <returns></returns>
Task UpdateBreakpoint(bool resetStates = false, string? reason = null, params string[] excludedStates);
Task<string> GetConversationSummary(string conversationId);
}

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
@ -56,6 +56,7 @@
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\agent.json" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\instruction.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\.welcome.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\conversation.summary.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.hf.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.naive.liquid" />
<None Remove="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\planner_prompt.sequential.get_remaining_task.liquid" />
@ -142,6 +143,9 @@
<Content Include="data\agents\dfd9b46d-d00c-40af-8a75-3fbdc2b89869\templates\instruction.reviewer.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\agents\01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a\templates\conversation.summary.liquid">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="data\plugins\config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View file

@ -0,0 +1,67 @@
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
public async Task<string> GetConversationSummary(string conversationId)
{
if (string.IsNullOrEmpty(conversationId)) return string.Empty;
var routing = _services.GetRequiredService<IRoutingService>();
var agentService = _services.GetRequiredService<IAgentService>();
var dialogs = _storage.GetDialogs(conversationId);
if (dialogs.IsNullOrEmpty()) return string.Empty;
var router = await agentService.LoadAgent(AIAssistant);
var prompt = GetPrompt(router);
var summary = await Summarize(router, prompt, dialogs);
return summary;
}
private string GetPrompt(Agent agent)
{
var template = agent.Templates.First(x => x.Name == "conversation.summary").Content;
var render = _services.GetRequiredService<ITemplateRender>();
return render.Render(template, new Dictionary<string, object> { });
}
private async Task<string> Summarize(Agent agent, string prompt, List<RoleDialogModel> dialogs)
{
var provider = "openai";
string? model;
var providerService = _services.GetRequiredService<ILlmProviderService>();
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
{
provider = agent?.LlmConfig?.Provider;
model = agent?.LlmConfig?.Model;
if (provider == null || model == null)
{
var agentSettings = _services.GetRequiredService<AgentSettings>();
provider = agentSettings.LlmConfig.Provider;
model = agentSettings.LlmConfig.Model;
}
}
var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider, model);
var response = await chatCompletion.GetChatCompletions(new Agent
{
Id = agent.Id,
Name = agent.Name,
Instruction = prompt
}, dialogs);
return response.Content;
}
}

View file

@ -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;

View file

@ -0,0 +1,8 @@
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 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'.

View file

@ -139,6 +139,13 @@ public class ConversationController : ControllerBase
return result;
}
[HttpGet("/conversation/{conversationId}/summary")]
public async Task<string> GetConversationSummary([FromRoute] string conversationId)
{
var service = _services.GetRequiredService<IConversationService>();
return await service.GetConversationSummary(conversationId);
}
[HttpGet("/conversation/{conversationId}/user")]
public async Task<UserViewModel> GetConversationUser([FromRoute] string conversationId)
{