diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs index 53c6470a..00924b93 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.Summary.cs @@ -1,4 +1,4 @@ -using BotSharp.Abstraction.Infrastructures.Enums; +using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Templating; namespace BotSharp.Core.Conversations.Services; @@ -9,24 +9,52 @@ public partial class ConversationService { if (string.IsNullOrEmpty(conversationId)) return string.Empty; + var routing = _services.GetRequiredService(); + var agentService = _services.GetRequiredService(); + var dialogs = _storage.GetDialogs(conversationId); + if (dialogs.IsNullOrEmpty()) return string.Empty; - return string.Empty; + var router = await agentService.LoadAgent(AIAssistant); + var content = await routing.GetConversationContent(dialogs); + var prompt = GetPrompt(router, content); + var summary = await Summarize(router, prompt, dialogs); + + return summary; } - private IEnumerable BuildConversationContent(List dialogs) + private string GetPrompt(Agent agent, string content) { - - } - - private string GetPrompt(Agent router, List dialogs) - { - var template = router.Templates.First(x => x.Name == "conversation.summary").Content; - + var template = agent.Templates.First(x => x.Name == "conversation.summary").Content; var render = _services.GetRequiredService(); - return render.Render(template, new Dictionary + return render.Render(template, new Dictionary { }); + } + + private async Task Summarize(Agent agent, string prompt, List dialogs) + { + var provider = agent.LlmConfig.Provider; + var model = agent.LlmConfig.Model; + + if (provider == null || model == null) { - { "conversation", } - }); + var agentSettings = _services.GetRequiredService(); + 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; + } + + private void SaveState(string summary) + { + _state.SetState("conversation_summary", summary, source: StateSource.Application); } } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index 4de36b9d..8e113226 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -12,6 +12,8 @@ public partial class ConversationService : IConversationService private readonly IConversationStorage _storage; private readonly IConversationStateService _state; private string _conversationId; + private const string AIAssistant = "01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a"; + public string ConversationId => _conversationId; public IConversationStateService States => _state; diff --git a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid index 3b6bb53f..a935e497 100644 --- a/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid +++ b/src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/templates/conversation.summary.liquid @@ -1,6 +1 @@ -Please follow these steps to summarize the conversation: -1. Read the [CONVERSATION] content. -2. Summarize the conversation in one sentence. - -[CONVERSATION] -{{ conversation }} \ No newline at end of file +Please summarize the conversation. \ No newline at end of file