Skip to content

Commit 1f14299

Browse files
authored
Merge pull request #474 from iceljc/features/refine-conv-summary
summarize multiple conversations
2 parents 5acc6c8 + 11fa251 commit 1f14299

File tree

5 files changed

+74
-18
lines changed

5 files changed

+74
-18
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,5 @@ Task<bool> SendMessage(string agentId,
5555
/// <returns></returns>
5656
Task UpdateBreakpoint(bool resetStates = false, string? reason = null, params string[] excludedStates);
5757

58-
Task<string> GetConversationSummary(string conversationId);
58+
Task<string> GetConversationSummary(IEnumerable<string> conversationId);
5959
}

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,51 @@ namespace BotSharp.Core.Conversations.Services;
55

66
public partial class ConversationService
77
{
8-
public async Task<string> GetConversationSummary(string conversationId)
8+
public async Task<string> GetConversationSummary(IEnumerable<string> conversationIds)
99
{
10-
if (string.IsNullOrEmpty(conversationId)) return string.Empty;
10+
if (conversationIds.IsNullOrEmpty()) return string.Empty;
1111

1212
var routing = _services.GetRequiredService<IRoutingService>();
1313
var agentService = _services.GetRequiredService<IAgentService>();
1414

15-
var dialogs = _storage.GetDialogs(conversationId);
16-
if (dialogs.IsNullOrEmpty()) return string.Empty;
15+
var contents = new List<string>();
16+
foreach ( var conversationId in conversationIds)
17+
{
18+
if (string.IsNullOrEmpty(conversationId)) continue;
19+
20+
var dialogs = _storage.GetDialogs(conversationId);
21+
22+
if (dialogs.IsNullOrEmpty()) continue;
23+
24+
var content = GetConversationContent(dialogs);
25+
contents.Add(content);
26+
}
1727

1828
var router = await agentService.LoadAgent(AIAssistant);
19-
var prompt = GetPrompt(router);
20-
var summary = await Summarize(router, prompt, dialogs);
29+
var prompt = GetPrompt(router, contents);
30+
var summary = await Summarize(router, prompt);
2131

2232
return summary;
2333
}
2434

25-
private string GetPrompt(Agent agent)
35+
private string GetPrompt(Agent agent, List<string> contents)
2636
{
2737
var template = agent.Templates.First(x => x.Name == "conversation.summary").Content;
2838
var render = _services.GetRequiredService<ITemplateRender>();
29-
return render.Render(template, new Dictionary<string, object> { });
39+
40+
var texts = string.Empty;
41+
for (int i = 0; i < contents.Count; i++)
42+
{
43+
texts += $"[Conversation {i+1}]\r\n{contents[i]}";
44+
}
45+
46+
return render.Render(template, new Dictionary<string, object>
47+
{
48+
{ "texts", texts }
49+
});
3050
}
3151

32-
private async Task<string> Summarize(Agent agent, string prompt, List<RoleDialogModel> dialogs)
52+
private async Task<string> Summarize(Agent agent, string prompt)
3353
{
3454
var provider = "openai";
3555
string? model;
@@ -60,8 +80,29 @@ private async Task<string> Summarize(Agent agent, string prompt, List<RoleDialog
6080
Id = agent.Id,
6181
Name = agent.Name,
6282
Instruction = prompt
63-
}, dialogs);
83+
}, new List<RoleDialogModel>
84+
{
85+
new RoleDialogModel(AgentRole.User, "Please summarize the conversations.")
86+
});
6487

6588
return response.Content;
6689
}
90+
91+
private string GetConversationContent(List<RoleDialogModel> dialogs, int maxDialogCount = 50)
92+
{
93+
var conversation = "";
94+
95+
foreach (var dialog in dialogs.TakeLast(maxDialogCount))
96+
{
97+
var role = dialog.Role;
98+
if (role != AgentRole.User)
99+
{
100+
role = AgentRole.Assistant;
101+
}
102+
103+
conversation += $"{role}: {dialog.Payload ?? dialog.Content}\r\n";
104+
}
105+
106+
return conversation + "\r\n";
107+
}
67108
}
Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
Please summarize the conversation.
1+
Please read each conversation in the [CONVERSATIONS] section and provide a summary.
22

3-
*** Super Important! Please consider the entire conversation. Do not only consider the recent sentences. ***
3+
*** Super Important! Please consider every conversation. Do not only consider the recent sentences. ***
44
** Please do not respond to the latest conversation.
5-
** If there are different topics in the conversation, please summarize each topic in different sentences and list them with bullets.
5+
** If there are different topics in the conversations, please summarize each topic in different sentences and list them in bullets.
66
* Please use concise sentences to summarize each topic.
77
* Please do not include excessive details in the summaries.
8-
* Please use 'user' instead of 'you', 'he' or 'she'.
8+
* Please use 'user' instead of 'you', 'he' or 'she'.
9+
10+
[CONVERSATIONS]
11+
12+
{% for text in texts -%}
13+
{{ text }}{{ "\r\n" }}
14+
{%- endfor %}

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs([FromRoute] string
152152
return result;
153153
}
154154

155-
[HttpGet("/conversation/{conversationId}/summary")]
156-
public async Task<string> GetConversationSummary([FromRoute] string conversationId)
155+
[HttpPost("/conversation/summary")]
156+
public async Task<string> GetConversationSummary([FromBody] ConversationSummaryModel input)
157157
{
158158
var service = _services.GetRequiredService<IConversationService>();
159-
return await service.GetConversationSummary(conversationId);
159+
return await service.GetConversationSummary(input.ConversationIds);
160160
}
161161

162162
[HttpGet("/conversation/{conversationId}/user")]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace BotSharp.OpenAPI.ViewModels.Conversations;
4+
5+
public class ConversationSummaryModel
6+
{
7+
[JsonPropertyName("conversation_ids")]
8+
public List<string> ConversationIds { get; set; } = new List<string>();
9+
}

0 commit comments

Comments
 (0)