forked from SciSharp/BotSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConversationService.Summary.cs
More file actions
128 lines (104 loc) · 4.16 KB
/
ConversationService.Summary.cs
File metadata and controls
128 lines (104 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.Settings;
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
public async Task<string> GetConversationSummary(ConversationSummaryModel model)
{
if (model.ConversationIds.IsNullOrEmpty()) return string.Empty;
var routing = _services.GetRequiredService<IRoutingService>();
var agentService = _services.GetRequiredService<IAgentService>();
var contents = new List<string>();
foreach (var conversationId in model.ConversationIds)
{
if (string.IsNullOrEmpty(conversationId)) continue;
var dialogs = await _storage.GetDialogs(conversationId);
if (dialogs.IsNullOrEmpty()) continue;
dialogs = dialogs.Where(x => x.MessageType != MessageTypeName.Notification).ToList();
var content = GetConversationContent(dialogs);
if (string.IsNullOrWhiteSpace(content)) continue;
contents.Add(content);
}
if (contents.IsNullOrEmpty()) return string.Empty;
var agent = await agentService.LoadAgent(model.AgentId);
var prompt = GetPrompt(agent, model.TemplateName, contents);
var summary = await Summarize(agent, prompt);
return summary;
}
private string GetPrompt(Agent agent, string templateName, List<string> contents)
{
var template = agent.Templates.First(x => x.Name == templateName).Content;
var render = _services.GetRequiredService<ITemplateRender>();
var texts = new List<string>();
for (int i = 0; i < contents.Count; i++)
{
texts.Add($"{contents[i]}");
}
return render.Render(template, new Dictionary<string, object>
{
{ "texts", texts }
});
}
private async Task<string> Summarize(Agent agent, string prompt)
{
var provider = "openai";
string? model;
var providerService = _services.GetRequiredService<ILlmProviderService>();
var settingService = _services.GetRequiredService<ISettingService>();
var modelSettings = providerService.GetProviderModels(provider);
var defaultModel = settingService.GetUpgradeModel(Gpt4xModelConstants.GPT_4o);
var modelSetting = modelSettings.FirstOrDefault(x => x.Name.IsEqualTo(defaultModel));
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
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, "Please summarize the conversations.")
});
return response.Content;
}
private string GetConversationContent(List<RoleDialogModel> dialogs, int maxDialogCount = 100)
{
var conversation = "";
foreach (var dialog in dialogs.TakeLast(maxDialogCount))
{
var role = dialog.Role;
if (role == AgentRole.Function) continue;
if (role == AgentRole.User)
{
conversation += $"{role}: {dialog.Payload ?? dialog.Content}\r\n";
}
else
{
role = AgentRole.Assistant;
conversation += $"{role}: {dialog.Content}\r\n";
}
}
if (string.IsNullOrEmpty(conversation))
{
return string.Empty;
}
return conversation + "\r\n";
}
}