Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ Task<bool> SendMessage(string agentId,
/// <returns></returns>
Task UpdateBreakpoint(bool resetStates = false, string? reason = null, params string[] excludedStates);

Task<string> GetConversationSummary(string conversationId);
Task<string> GetConversationSummary(IEnumerable<string> conversationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,8 +80,29 @@ private async Task<string> Summarize(Agent agent, string prompt, List<RoleDialog
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";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
Please summarize the conversation.
Please read each conversation in the [CONVERSATIONS] 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" }}
{%- endfor %}
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs([FromRoute] string
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")]
Expand Down
Original file line number Diff line number Diff line change
@@ -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>();
}