2023-11-30 21:03:44 +00:00
|
|
|
using BotSharp.Abstraction.Agents;
|
|
|
|
|
using BotSharp.Abstraction.Agents.Enums;
|
|
|
|
|
|
2023-11-29 23:23:14 +00:00
|
|
|
namespace BotSharp.Logger.Hooks;
|
|
|
|
|
|
2023-11-30 21:03:44 +00:00
|
|
|
public class VerboseLogHook : IContentGeneratingHook
|
2023-11-29 23:23:14 +00:00
|
|
|
{
|
|
|
|
|
private readonly ConversationSetting _convSettings;
|
|
|
|
|
private readonly ILogger<VerboseLogHook> _logger;
|
2023-11-30 21:03:44 +00:00
|
|
|
private readonly IServiceProvider _services;
|
2023-11-29 23:23:14 +00:00
|
|
|
|
2023-11-30 21:03:44 +00:00
|
|
|
public VerboseLogHook(
|
|
|
|
|
ConversationSetting convSettings,
|
|
|
|
|
IServiceProvider serivces,
|
|
|
|
|
ILogger<VerboseLogHook> logger)
|
2023-11-29 23:23:14 +00:00
|
|
|
{
|
|
|
|
|
_convSettings = convSettings;
|
2023-11-30 21:03:44 +00:00
|
|
|
_services = serivces;
|
2023-11-29 23:23:14 +00:00
|
|
|
_logger = logger;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 21:03:44 +00:00
|
|
|
public async Task BeforeGenerating(Agent agent, List<RoleDialogModel> conversations)
|
|
|
|
|
{
|
|
|
|
|
if (!_convSettings.ShowVerboseLog) return;
|
|
|
|
|
|
2024-09-03 20:41:12 +00:00
|
|
|
var dialog = conversations.LastOrDefault();
|
|
|
|
|
if (dialog != null)
|
|
|
|
|
{
|
|
|
|
|
var log = $"{dialog.Role}: {dialog.Content} [msg_id: {dialog.MessageId}] ==>";
|
|
|
|
|
_logger.LogInformation(log);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 17:31:45 +00:00
|
|
|
await Task.CompletedTask;
|
2023-11-30 21:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
|
2023-11-29 23:23:14 +00:00
|
|
|
{
|
2025-03-05 18:29:28 +00:00
|
|
|
if (!_convSettings.ShowVerboseLog || string.IsNullOrEmpty(tokenStats.Prompt)) return;
|
2023-11-29 23:23:14 +00:00
|
|
|
|
2023-11-30 21:03:44 +00:00
|
|
|
var agentService = _services.GetRequiredService<IAgentService>();
|
|
|
|
|
var agent = await agentService.LoadAgent(message.CurrentAgentId);
|
|
|
|
|
|
|
|
|
|
var log = message.Role == AgentRole.Function ?
|
2025-01-09 22:30:25 +00:00
|
|
|
$"[{agent?.Name}]: {message.Indication} {message.FunctionName}({message.FunctionArgs})" :
|
2023-12-27 19:27:52 +00:00
|
|
|
$"[{agent?.Name}]: {message.Content}" + $" <== [msg_id: {message.MessageId}]";
|
2023-11-30 21:03:44 +00:00
|
|
|
|
|
|
|
|
_logger.LogInformation(tokenStats.Prompt);
|
|
|
|
|
_logger.LogInformation(log);
|
2023-11-29 23:23:14 +00:00
|
|
|
}
|
|
|
|
|
}
|