optimize summary

This commit is contained in:
nick.yi 2025-05-29 15:14:44 +08:00
parent d222a120ac
commit 5bfb8f8734
5 changed files with 38 additions and 20 deletions

View file

@ -1,4 +1,3 @@
using BotSharp.Abstraction.Loggers.Models;
using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Repositories.Filters;
namespace BotSharp.Abstraction.Conversations; namespace BotSharp.Abstraction.Conversations;
@ -53,7 +52,7 @@ public interface IConversationService
/// <returns></returns> /// <returns></returns>
Task UpdateBreakpoint(bool resetStates = false, string? reason = null, params string[] excludedStates); Task UpdateBreakpoint(bool resetStates = false, string? reason = null, params string[] excludedStates);
Task<string> GetConversationSummary(IEnumerable<string> conversationId); Task<string> GetConversationSummary(ConversationSummaryModel model);
Task<Conversation> GetConversationRecordOrCreateNew(string agentId); Task<Conversation> GetConversationRecordOrCreateNew(string agentId);

View file

@ -0,0 +1,27 @@
using System.Text.Json.Serialization;
namespace BotSharp.Abstraction.Models;
public class ConversationSummaryModel
{
[JsonPropertyName("conversation_ids")]
public IEnumerable<string> ConversationIds { get; set; } = new List<string>();
private string _agentId;
[JsonPropertyName("agent_id")]
public string AgentId
{
get => _agentId ?? BuiltInAgentId.AIAssistant;
set => _agentId = value;
}
private string _templateName;
[JsonPropertyName("template_name")]
public string TemplateName
{
get => _templateName ?? "conversation.summary";
set => _templateName = value;
}
}

View file

@ -1,20 +1,21 @@
using BotSharp.Abstraction.Conversations.Enums; using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.MLTasks; using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.Templating; using BotSharp.Abstraction.Templating;
namespace BotSharp.Core.Conversations.Services; namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService public partial class ConversationService
{ {
public async Task<string> GetConversationSummary(IEnumerable<string> conversationIds) public async Task<string> GetConversationSummary(ConversationSummaryModel model)
{ {
if (conversationIds.IsNullOrEmpty()) return string.Empty; if (model.ConversationIds.IsNullOrEmpty()) return string.Empty;
var routing = _services.GetRequiredService<IRoutingService>(); var routing = _services.GetRequiredService<IRoutingService>();
var agentService = _services.GetRequiredService<IAgentService>(); var agentService = _services.GetRequiredService<IAgentService>();
var contents = new List<string>(); var contents = new List<string>();
foreach ( var conversationId in conversationIds) foreach (var conversationId in model.ConversationIds)
{ {
if (string.IsNullOrEmpty(conversationId)) continue; if (string.IsNullOrEmpty(conversationId)) continue;
@ -31,16 +32,16 @@ public partial class ConversationService
if (contents.IsNullOrEmpty()) return string.Empty; if (contents.IsNullOrEmpty()) return string.Empty;
var router = await agentService.LoadAgent(AIAssistant); var agent = await agentService.LoadAgent(model.AgentId);
var prompt = GetPrompt(router, contents); var prompt = GetPrompt(agent, model.TemplateName, contents);
var summary = await Summarize(router, prompt); var summary = await Summarize(agent, prompt);
return summary; return summary;
} }
private string GetPrompt(Agent agent, List<string> contents) private string GetPrompt(Agent agent, string templateName, List<string> contents)
{ {
var template = agent.Templates.First(x => x.Name == "conversation.summary").Content; var template = agent.Templates.First(x => x.Name == templateName).Content;
var render = _services.GetRequiredService<ITemplateRender>(); var render = _services.GetRequiredService<ITemplateRender>();
var texts = new List<string>(); var texts = new List<string>();

View file

@ -185,7 +185,7 @@ public class ConversationController : ControllerBase
public async Task<string> GetConversationSummary([FromBody] ConversationSummaryModel input) public async Task<string> GetConversationSummary([FromBody] ConversationSummaryModel input)
{ {
var service = _services.GetRequiredService<IConversationService>(); var service = _services.GetRequiredService<IConversationService>();
return await service.GetConversationSummary(input.ConversationIds); return await service.GetConversationSummary(input);
} }
[HttpPut("/conversation/{conversationId}/update-title")] [HttpPut("/conversation/{conversationId}/update-title")]

View file

@ -1,9 +0,0 @@
using System.Text.Json.Serialization;
namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class ConversationSummaryModel
{
[JsonPropertyName("conversation_ids")]
public List<string> ConversationIds { get; set; } = new List<string>();
}