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

54 lines
1.8 KiB
C#
Raw Normal View History

2024-05-27 01:40:24 +00:00
using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
public async Task<string> GetConversationSummary(string conversationId)
{
if (string.IsNullOrEmpty(conversationId)) return string.Empty;
2024-05-27 02:30:57 +00:00
var routing = _services.GetRequiredService<IRoutingService>();
var agentService = _services.GetRequiredService<IAgentService>();
2024-05-27 01:40:24 +00:00
var dialogs = _storage.GetDialogs(conversationId);
2024-05-27 02:30:57 +00:00
if (dialogs.IsNullOrEmpty()) return string.Empty;
var router = await agentService.LoadAgent(AIAssistant);
2024-05-27 03:20:27 +00:00
var prompt = GetPrompt(router);
2024-05-27 02:30:57 +00:00
var summary = await Summarize(router, prompt, dialogs);
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-27 03:20:27 +00:00
private string GetPrompt(Agent agent)
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>();
return render.Render(template, new Dictionary<string, object> { });
2024-05-27 01:40:24 +00:00
}
2024-05-27 02:30:57 +00:00
private async Task<string> Summarize(Agent agent, string prompt, List<RoleDialogModel> dialogs)
2024-05-27 01:40:24 +00:00
{
2024-05-27 03:20:56 +00:00
var provider = agent?.LlmConfig?.Provider;
var model = agent?.LlmConfig?.Model;
2024-05-27 01:40:24 +00:00
2024-05-27 02:30:57 +00:00
if (provider == null || model == null)
2024-05-27 01:40:24 +00:00
{
2024-05-27 02:30:57 +00:00
var agentSettings = _services.GetRequiredService<AgentSettings>();
provider = agentSettings.LlmConfig.Provider;
model = agentSettings.LlmConfig.Model;
}
var chatCompletion = CompletionProvider.GetChatCompletion(_services, provider: provider, model: model);
var response = await chatCompletion.GetChatCompletions(new Agent
{
Id = agent.Id,
Name = agent.Name,
Instruction = prompt
}, dialogs);
return response.Content;
}
2024-05-27 01:40:24 +00:00
}