2024-02-15 20:43:36 +00:00
|
|
|
using BotSharp.Abstraction.Loggers.Models;
|
|
|
|
|
|
2023-11-29 23:23:14 +00:00
|
|
|
public class CommonContentGeneratingHook : IContentGeneratingHook
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
|
|
|
|
|
public CommonContentGeneratingHook(IServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task AfterGenerated(RoleDialogModel message, TokenStatsModel tokenStats)
|
|
|
|
|
{
|
|
|
|
|
SaveLlmCompletionLog(message, tokenStats);
|
|
|
|
|
await Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveLlmCompletionLog(RoleDialogModel message, TokenStatsModel tokenStats)
|
|
|
|
|
{
|
|
|
|
|
var convSettings = _services.GetRequiredService<ConversationSetting>();
|
|
|
|
|
if (!convSettings.EnableLlmCompletionLog) return;
|
|
|
|
|
|
|
|
|
|
var db = _services.GetRequiredService<IBotSharpRepository>();
|
|
|
|
|
var state = _services.GetRequiredService<IConversationStateService>();
|
|
|
|
|
|
|
|
|
|
var completionLog = new LlmCompletionLog
|
|
|
|
|
{
|
|
|
|
|
ConversationId = state.GetConversationId(),
|
|
|
|
|
MessageId = message.MessageId,
|
|
|
|
|
AgentId = message.CurrentAgentId,
|
|
|
|
|
Prompt = tokenStats.Prompt,
|
2025-02-20 17:59:59 +00:00
|
|
|
Response = message.Content,
|
|
|
|
|
CreateDateTime = DateTime.UtcNow
|
2023-11-29 23:23:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
db.SaveLlmCompletionLog(completionLog);
|
|
|
|
|
}
|
|
|
|
|
}
|