clean execution log

This commit is contained in:
Jicheng Lu 2025-02-20 11:59:59 -06:00
parent 8048524a31
commit 816abe9310
10 changed files with 10 additions and 102 deletions

View file

@ -152,13 +152,6 @@ public interface IBotSharpRepository : IHaveServiceProvider
=> throw new NotImplementedException();
#endregion
#region Execution Log
void AddExecutionLogs(string conversationId, List<string> logs)
=> throw new NotImplementedException();
List<string> GetExecutionLogs(string conversationId)
=> throw new NotImplementedException();
#endregion
#region LLM Completion Log
void SaveLlmCompletionLog(LlmCompletionLog log)
=> throw new NotImplementedException();

View file

@ -1,26 +1,25 @@
using BotSharp.Abstraction.Evaluations;
using BotSharp.Abstraction.Repositories;
using System.Text.RegularExpressions;
namespace BotSharp.Core.Evaluations;
public class ExecutionLogger : IExecutionLogger
{
private readonly BotSharpDatabaseSettings _dbSettings;
private readonly IServiceProvider _services;
private readonly ILogger<ExecutionLogger> _logger;
public ExecutionLogger(
BotSharpDatabaseSettings dbSettings,
IServiceProvider services)
IServiceProvider services,
ILogger<ExecutionLogger> logger)
{
_dbSettings = dbSettings;
_services = services;
_logger = logger;
}
public void Append(string conversationId, string content)
{
content = content.Replace("\r\n", " ").Replace("\n", " ");
content = Regex.Replace(content, @"\s+", " ");
var db = _services.GetRequiredService<IBotSharpRepository>();
db.AddExecutionLogs(conversationId, new List<string> { content });
_logger.LogInformation($"Execution Log: {content}");
}
}

View file

@ -135,18 +135,6 @@ public class BotSharpDbContext : Database, IBotSharpRepository
=> throw new NotImplementedException();
#endregion
#region Execution Log
public void AddExecutionLogs(string conversationId, List<string> logs)
{
throw new NotImplementedException();
}
public List<string> GetExecutionLogs(string conversationId)
{
throw new NotImplementedException();
}
#endregion
#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{

View file

@ -1,40 +1,10 @@
using BotSharp.Abstraction.Loggers.Models;
using Serilog;
using System.IO;
namespace BotSharp.Core.Repository
{
public partial class FileRepository
{
#region Execution Log
public void AddExecutionLogs(string conversationId, List<string> logs)
{
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
var file = Path.Combine(dir, EXECUTION_LOG_FILE);
File.AppendAllLines(file, logs);
}
public List<string> GetExecutionLogs(string conversationId)
{
var logs = new List<string>();
if (string.IsNullOrEmpty(conversationId)) return logs;
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
if (!Directory.Exists(dir)) return logs;
var file = Path.Combine(dir, EXECUTION_LOG_FILE);
logs = File.ReadAllLines(file)?.ToList() ?? new List<string>();
return logs;
}
#endregion
#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{

View file

@ -50,9 +50,6 @@ public partial class FileRepository : IBotSharpRepository
private const string KNOWLEDGE_DOC_FOLDER = "document";
private const string KNOWLEDGE_DOC_META_FILE = "meta.json";
private const string EXECUTION_LOG_FILE = "execution.log";
private const string PLUGIN_CONFIG_FILE = "config.json";
private const string STATS_FOLDER = "stats";
private const string STATS_FILE = "stats.json";

View file

@ -29,7 +29,8 @@ public class CommonContentGeneratingHook : IContentGeneratingHook
MessageId = message.MessageId,
AgentId = message.CurrentAgentId,
Prompt = tokenStats.Prompt,
Response = message.Content
Response = message.Content,
CreateDateTime = DateTime.UtcNow
};
db.SaveLlmCompletionLog(completionLog);

View file

@ -1,7 +0,0 @@
namespace BotSharp.Plugin.MongoStorage.Collections;
public class ExecutionLogDocument : MongoBase
{
public string ConversationId { get; set; } = default!;
public List<string> Logs { get; set; } = [];
}

View file

@ -154,9 +154,6 @@ public class MongoDbContext
public IMongoCollection<ConversationStateDocument> ConversationStates
=> CreateConversationStateIndex();
public IMongoCollection<ExecutionLogDocument> ExectionLogs
=> GetCollectionOrCreate<ExecutionLogDocument>("ExecutionLogs");
public IMongoCollection<LlmCompletionLogDocument> LlmCompletionLogs
=> GetCollectionOrCreate<LlmCompletionLogDocument>("LlmCompletionLogs");

View file

@ -56,13 +56,11 @@ public partial class MongoRepository
var filterConv = Builders<ConversationDocument>.Filter.In(x => x.Id, conversationIds);
var filterDialog = Builders<ConversationDialogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterSates = Builders<ConversationStateDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterExeLog = Builders<ExecutionLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterPromptLog = Builders<LlmCompletionLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterContentLog = Builders<ConversationContentLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var filterStateLog = Builders<ConversationStateLogDocument>.Filter.In(x => x.ConversationId, conversationIds);
var conbTabItems = Builders<CrontabItemDocument>.Filter.In(x => x.ConversationId, conversationIds);
var exeLogDeleted = _dc.ExectionLogs.DeleteMany(filterExeLog);
var promptLogDeleted = _dc.LlmCompletionLogs.DeleteMany(filterPromptLog);
var contentLogDeleted = _dc.ContentLogs.DeleteMany(filterContentLog);
var stateLogDeleted = _dc.StateLogs.DeleteMany(filterStateLog);
@ -71,10 +69,8 @@ public partial class MongoRepository
var cronDeleted = _dc.CrontabItems.DeleteMany(conbTabItems);
var convDeleted = _dc.Conversations.DeleteMany(filterConv);
return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0 || statesDeleted.DeletedCount > 0
|| exeLogDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0
|| contentLogDeleted.DeletedCount > 0 || stateLogDeleted.DeletedCount > 0
|| convDeleted.DeletedCount > 0;
return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0 || statesDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0
|| contentLogDeleted.DeletedCount > 0 || stateLogDeleted.DeletedCount > 0 || convDeleted.DeletedCount > 0;
}
[SideCar]

View file

@ -4,32 +4,6 @@ namespace BotSharp.Plugin.MongoStorage.Repository;
public partial class MongoRepository
{
#region Execution Log
public void AddExecutionLogs(string conversationId, List<string> logs)
{
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
var filter = Builders<ExecutionLogDocument>.Filter.Eq(x => x.ConversationId, conversationId);
var update = Builders<ExecutionLogDocument>.Update
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
.PushEach(x => x.Logs, logs);
_dc.ExectionLogs.UpdateOne(filter, update, _options);
}
public List<string> GetExecutionLogs(string conversationId)
{
List<string> logs = [];
if (string.IsNullOrEmpty(conversationId)) return logs;
var filter = Builders<ExecutionLogDocument>.Filter.Eq(x => x.ConversationId, conversationId);
var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault();
logs = logCollection?.Logs ?? [];
return logs;
}
#endregion
#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{