BotSharp/src/Infrastructure/BotSharp.Logger/Hooks/VerboseLogHook.cs

46 lines
1.4 KiB
C#
Raw Normal View History

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;
var dialog = conversations.Last();
var log = $"{dialog.Role}: {dialog.Content}";
_logger.LogInformation(log);
}
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
2023-11-29 23:23:14 +00:00
{
if (!_convSettings.ShowVerboseLog) return;
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 ?
$"[{agent.Name}]: {message.FunctionName}({message.FunctionArgs})" :
$"[{agent.Name}]: {message.Content}";
_logger.LogInformation(tokenStats.Prompt);
_logger.LogInformation(log);
2023-11-29 23:23:14 +00:00
}
}