summarize multiple conversations
This commit is contained in:
parent
5acc6c89e8
commit
0b56a18aa6
|
|
@ -55,5 +55,5 @@ public interface IConversationService
|
|||
/// <returns></returns>
|
||||
Task UpdateBreakpoint(bool resetStates = false, string? reason = null, params string[] excludedStates);
|
||||
|
||||
Task<string> GetConversationSummary(string conversationId);
|
||||
Task<string> GetConversationSummary(IEnumerable<string> conversationId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,31 +5,51 @@ namespace BotSharp.Core.Conversations.Services;
|
|||
|
||||
public partial class ConversationService
|
||||
{
|
||||
public async Task<string> GetConversationSummary(string conversationId)
|
||||
public async Task<string> GetConversationSummary(IEnumerable<string> conversationIds)
|
||||
{
|
||||
if (string.IsNullOrEmpty(conversationId)) return string.Empty;
|
||||
if (conversationIds.IsNullOrEmpty()) 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 contents = new List<string>();
|
||||
foreach ( var conversationId in conversationIds)
|
||||
{
|
||||
if (string.IsNullOrEmpty(conversationId)) continue;
|
||||
|
||||
var dialogs = _storage.GetDialogs(conversationId);
|
||||
|
||||
if (dialogs.IsNullOrEmpty()) continue;
|
||||
|
||||
var content = GetConversationContent(dialogs);
|
||||
contents.Add(content);
|
||||
}
|
||||
|
||||
var router = await agentService.LoadAgent(AIAssistant);
|
||||
var prompt = GetPrompt(router);
|
||||
var summary = await Summarize(router, prompt, dialogs);
|
||||
var prompt = GetPrompt(router, contents);
|
||||
var summary = await Summarize(router, prompt);
|
||||
|
||||
return summary;
|
||||
}
|
||||
|
||||
private string GetPrompt(Agent agent)
|
||||
private string GetPrompt(Agent agent, List<string> contents)
|
||||
{
|
||||
var template = agent.Templates.First(x => x.Name == "conversation.summary").Content;
|
||||
var render = _services.GetRequiredService<ITemplateRender>();
|
||||
return render.Render(template, new Dictionary<string, object> { });
|
||||
|
||||
var texts = string.Empty;
|
||||
for (int i = 0; i < contents.Count; i++)
|
||||
{
|
||||
texts += $"[Conversation {i+1}]\r\n{contents[i]}";
|
||||
}
|
||||
|
||||
return render.Render(template, new Dictionary<string, object>
|
||||
{
|
||||
{ "texts", texts }
|
||||
});
|
||||
}
|
||||
|
||||
private async Task<string> Summarize(Agent agent, string prompt, List<RoleDialogModel> dialogs)
|
||||
private async Task<string> Summarize(Agent agent, string prompt)
|
||||
{
|
||||
var provider = "openai";
|
||||
string? model;
|
||||
|
|
@ -60,8 +80,29 @@ public partial class ConversationService
|
|||
Id = agent.Id,
|
||||
Name = agent.Name,
|
||||
Instruction = prompt
|
||||
}, dialogs);
|
||||
}, new List<RoleDialogModel>
|
||||
{
|
||||
new RoleDialogModel(AgentRole.User, "Please summarize the conversations.")
|
||||
});
|
||||
|
||||
return response.Content;
|
||||
}
|
||||
|
||||
private string GetConversationContent(List<RoleDialogModel> dialogs, int maxDialogCount = 50)
|
||||
{
|
||||
var conversation = "";
|
||||
|
||||
foreach (var dialog in dialogs.TakeLast(maxDialogCount))
|
||||
{
|
||||
var role = dialog.Role;
|
||||
if (role != AgentRole.User)
|
||||
{
|
||||
role = AgentRole.Assistant;
|
||||
}
|
||||
|
||||
conversation += $"{role}: {dialog.Payload ?? dialog.Content}\r\n";
|
||||
}
|
||||
|
||||
return conversation + "\r\n";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,14 @@
|
|||
Please summarize the conversation.
|
||||
Please read each conversation in the [CONVERSATION] section and provide a summary.
|
||||
|
||||
*** Super Important! Please consider the entire conversation. Do not only consider the recent sentences. ***
|
||||
*** Super Important! Please consider every 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.
|
||||
** If there are different topics in the conversations, 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'.
|
||||
* Please use 'user' instead of 'you', 'he' or 'she'.
|
||||
|
||||
[CONVERSATIONS]
|
||||
|
||||
{% for text in texts -%}
|
||||
{{ text }}{{ "\r\n\r\n" }}
|
||||
{%- endfor %}
|
||||
|
|
@ -152,11 +152,11 @@ public class ConversationController : ControllerBase
|
|||
return result;
|
||||
}
|
||||
|
||||
[HttpGet("/conversation/{conversationId}/summary")]
|
||||
public async Task<string> GetConversationSummary([FromRoute] string conversationId)
|
||||
[HttpPost("/conversation/summary")]
|
||||
public async Task<string> GetConversationSummary([FromBody] ConversationSummaryModel input)
|
||||
{
|
||||
var service = _services.GetRequiredService<IConversationService>();
|
||||
return await service.GetConversationSummary(conversationId);
|
||||
return await service.GetConversationSummary(input.ConversationIds);
|
||||
}
|
||||
|
||||
[HttpGet("/conversation/{conversationId}/user")]
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace BotSharp.OpenAPI.ViewModels.Conversations;
|
||||
|
||||
public class ConversationSummaryModel
|
||||
{
|
||||
[JsonPropertyName("conversation_ids")]
|
||||
public List<string> ConversationIds { get; set; } = new List<string>();
|
||||
}
|
||||
Loading…
Reference in a new issue