add log settings
This commit is contained in:
parent
5bd15f7a6b
commit
929487c0d6
|
|
@ -6,6 +6,7 @@ public class BotSharpDatabaseSettings : DatabaseBasicSettings
|
|||
public string FileRepository { get; set; }
|
||||
public string BotSharpMongoDb { get; set; }
|
||||
public string TablePrefix { get; set; }
|
||||
public BotSharpLogSetting Log { get; set; }
|
||||
public DbConnectionSetting BotSharp { get; set; }
|
||||
}
|
||||
|
||||
|
|
@ -29,3 +30,9 @@ public class DbConnectionSetting
|
|||
Slavers = new string[0];
|
||||
}
|
||||
}
|
||||
|
||||
public class BotSharpLogSetting
|
||||
{
|
||||
public bool EnableLlmCompletionLog { get; set; }
|
||||
public bool EnableExecutionLog { get; set; }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,8 +38,8 @@ public interface IBotSharpRepository
|
|||
List<Conversation> GetConversations(ConversationFilter filter);
|
||||
void UpdateConversationTitle(string conversationId, string title);
|
||||
List<Conversation> GetLastConversations();
|
||||
void AddExectionLogs(string conversationId, List<string> logs);
|
||||
List<string> GetExectionLogs(string conversationId);
|
||||
void AddExecutionLogs(string conversationId, List<string> logs);
|
||||
List<string> GetExecutionLogs(string conversationId);
|
||||
#endregion
|
||||
|
||||
#region LLM Completion Log
|
||||
|
|
|
|||
|
|
@ -21,6 +21,6 @@ public class ExecutionLogger : IExecutionLogger
|
|||
content = content.Replace("\r\n", " ").Replace("\n", " ");
|
||||
content = Regex.Replace(content, @"\s+", " ");
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
db.AddExectionLogs(conversationId, new List<string> { content });
|
||||
db.AddExecutionLogs(conversationId, new List<string> { content });
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -168,16 +168,6 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
|||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void AddExectionLogs(string conversationId, List<string> logs)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<string> GetExectionLogs(string conversationId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
|
@ -198,6 +188,19 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
|||
}
|
||||
#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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -6,8 +6,6 @@ using BotSharp.Abstraction.Agents.Models;
|
|||
using MongoDB.Driver;
|
||||
using BotSharp.Abstraction.Routing.Models;
|
||||
using BotSharp.Abstraction.Repositories.Filters;
|
||||
using BotSharp.Abstraction.Utilities;
|
||||
using BotSharp.Abstraction.Conversations.Models;
|
||||
|
||||
namespace BotSharp.Core.Repository;
|
||||
|
||||
|
|
@ -789,33 +787,6 @@ public class FileRepository : IBotSharpRepository
|
|||
.Select(g => g.OrderByDescending(x => x.CreatedTime).First())
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void AddExectionLogs(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.AppendAllLines(file, logs);
|
||||
}
|
||||
|
||||
public List<string> GetExectionLogs(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");
|
||||
logs = File.ReadAllLines(file)?.ToList() ?? new List<string>();
|
||||
return logs;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region User
|
||||
|
|
@ -843,9 +814,44 @@ public class FileRepository : IBotSharpRepository
|
|||
}
|
||||
#endregion
|
||||
|
||||
#region Execution Log
|
||||
public void AddExecutionLogs(string conversationId, List<string> logs)
|
||||
{
|
||||
var isEnabled = _dbSettings.Log.EnableExecutionLog;
|
||||
if (!isEnabled) return;
|
||||
|
||||
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.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");
|
||||
logs = File.ReadAllLines(file)?.ToList() ?? new List<string>();
|
||||
return logs;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region LLM Completion Log
|
||||
public void SaveLlmCompletionLog(LlmCompletionLog log)
|
||||
{
|
||||
var isEnabled = _dbSettings.Log.EnableLlmCompletionLog;
|
||||
if (!isEnabled) return;
|
||||
|
||||
var convDir = FindConversationDirectory(log.ConversationId);
|
||||
if (!Directory.Exists(convDir)) return;
|
||||
|
||||
|
|
|
|||
|
|
@ -12,12 +12,14 @@ namespace BotSharp.Plugin.MongoStorage.Repository;
|
|||
|
||||
public class MongoRepository : IBotSharpRepository
|
||||
{
|
||||
private readonly BotSharpDatabaseSettings _dbSettings;
|
||||
private readonly MongoDbContext _dc;
|
||||
private readonly IServiceProvider _services;
|
||||
private UpdateOptions _options;
|
||||
|
||||
public MongoRepository(MongoDbContext dc, IServiceProvider services)
|
||||
public MongoRepository(BotSharpDatabaseSettings dbSettings, MongoDbContext dc, IServiceProvider services)
|
||||
{
|
||||
_dbSettings = dbSettings;
|
||||
_dc = dc;
|
||||
_services = services;
|
||||
_options = new UpdateOptions
|
||||
|
|
@ -782,30 +784,6 @@ public class MongoRepository : IBotSharpRepository
|
|||
UpdatedTime = c.UpdatedTime
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
public void AddExectionLogs(string conversationId, List<string> logs)
|
||||
{
|
||||
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
|
||||
|
||||
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
|
||||
var update = Builders<ExectionLogCollection>.Update
|
||||
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
|
||||
.PushEach(x => x.Logs, logs);
|
||||
|
||||
_dc.ExectionLogs.UpdateOne(filter, update, _options);
|
||||
}
|
||||
|
||||
public List<string> GetExectionLogs(string conversationId)
|
||||
{
|
||||
var logs = new List<string>();
|
||||
if (string.IsNullOrEmpty(conversationId)) return logs;
|
||||
|
||||
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
|
||||
var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault();
|
||||
|
||||
logs = logCollection?.Logs ?? new List<string>();
|
||||
return logs;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region User
|
||||
|
|
@ -863,9 +841,41 @@ public class MongoRepository : IBotSharpRepository
|
|||
}
|
||||
#endregion
|
||||
|
||||
#region Execution Log
|
||||
public void AddExecutionLogs(string conversationId, List<string> logs)
|
||||
{
|
||||
var isEnabled = _dbSettings.Log.EnableExecutionLog;
|
||||
if (!isEnabled) return;
|
||||
|
||||
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
|
||||
|
||||
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
|
||||
var update = Builders<ExectionLogCollection>.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)
|
||||
{
|
||||
var logs = new List<string>();
|
||||
if (string.IsNullOrEmpty(conversationId)) return logs;
|
||||
|
||||
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
|
||||
var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault();
|
||||
|
||||
logs = logCollection?.Logs ?? new List<string>();
|
||||
return logs;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region LLM Completion Log
|
||||
public void SaveLlmCompletionLog(LlmCompletionLog log)
|
||||
{
|
||||
var isEnabled = _dbSettings.Log.EnableLlmCompletionLog;
|
||||
if (!isEnabled) return;
|
||||
|
||||
var completiongLog = new LlmCompletionLogCollection
|
||||
{
|
||||
Id = string.IsNullOrEmpty(log.Id) ? Guid.NewGuid().ToString() : log.Id,
|
||||
|
|
|
|||
|
|
@ -104,6 +104,10 @@
|
|||
"Master": "Data Source=(localdb)\\ProjectModels;Initial Catalog=BotSharp;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False",
|
||||
"Slavers": []
|
||||
},
|
||||
"Log": {
|
||||
"EnableLlmCompletionLog": false,
|
||||
"EnableExecutionLog": true
|
||||
},
|
||||
"FileRepository": "data",
|
||||
"UseCamelCase": true,
|
||||
"Assemblies": [ "BotSharp.Core" ]
|
||||
|
|
|
|||
Loading…
Reference in a new issue