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

109 lines
3.4 KiB
C#
Raw Normal View History

2024-05-27 17:12:39 +00:00
using BotSharp.Abstraction.MLTasks;
2024-05-27 01:40:24 +00:00
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
2024-05-29 02:50:16 +00:00
public async Task<string> GetConversationSummary(IEnumerable<string> conversationIds)
2024-05-27 01:40:24 +00:00
{
2024-05-29 02:50:16 +00:00
if (conversationIds.IsNullOrEmpty()) return string.Empty;
2024-05-27 01:40:24 +00:00
2024-05-27 02:30:57 +00:00
var routing = _services.GetRequiredService<IRoutingService>();
var agentService = _services.GetRequiredService<IAgentService>();
2024-05-29 02:50:16 +00:00
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);
}
2024-05-27 02:30:57 +00:00
var router = await agentService.LoadAgent(AIAssistant);
2024-05-29 02:50:16 +00:00
var prompt = GetPrompt(router, contents);
var summary = await Summarize(router, prompt);
2024-05-27 01:40:24 +00:00
2024-05-27 02:30:57 +00:00
return summary;
2024-05-27 01:40:24 +00:00
}
2024-05-29 02:50:16 +00:00
private string GetPrompt(Agent agent, List<string> contents)
2024-05-27 01:40:24 +00:00
{
2024-05-27 02:30:57 +00:00
var template = agent.Templates.First(x => x.Name == "conversation.summary").Content;
var render = _services.GetRequiredService<ITemplateRender>();
2024-05-29 02:50:16 +00:00
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 }
});
2024-05-27 01:40:24 +00:00
}
2024-05-29 02:50:16 +00:00
private async Task<string> Summarize(Agent agent, string prompt)
2024-05-27 01:40:24 +00:00
{
2024-05-27 17:12:39 +00:00
var provider = "openai";
string? model;
2024-05-27 01:40:24 +00:00
2024-05-27 17:12:39 +00:00
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
2024-05-27 01:40:24 +00:00
{
2024-05-27 17:12:39 +00:00
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;
}
2024-05-27 02:30:57 +00:00
}
2024-05-27 17:12:39 +00:00
var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider, model);
2024-05-27 02:30:57 +00:00
var response = await chatCompletion.GetChatCompletions(new Agent
{
Id = agent.Id,
Name = agent.Name,
Instruction = prompt
2024-05-29 02:50:16 +00:00
}, new List<RoleDialogModel>
{
new RoleDialogModel(AgentRole.User, "Please summarize the conversations.")
});
2024-05-27 02:30:57 +00:00
return response.Content;
}
2024-05-29 02:50:16 +00:00
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";
}
2024-05-27 01:40:24 +00:00
}