Skip to content

Commit 929487c

Browse files
author
Jicheng Lu
committed
add log settings
1 parent 5bd15f7 commit 929487c

File tree

7 files changed

+95
-65
lines changed

7 files changed

+95
-65
lines changed

src/Infrastructure/BotSharp.Abstraction/Repositories/BotSharpDatabaseSettings.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class BotSharpDatabaseSettings : DatabaseBasicSettings
66
public string FileRepository { get; set; }
77
public string BotSharpMongoDb { get; set; }
88
public string TablePrefix { get; set; }
9+
public BotSharpLogSetting Log { get; set; }
910
public DbConnectionSetting BotSharp { get; set; }
1011
}
1112

@@ -29,3 +30,9 @@ public DbConnectionSetting()
2930
Slavers = new string[0];
3031
}
3132
}
33+
34+
public class BotSharpLogSetting
35+
{
36+
public bool EnableLlmCompletionLog { get; set; }
37+
public bool EnableExecutionLog { get; set; }
38+
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public interface IBotSharpRepository
3838
List<Conversation> GetConversations(ConversationFilter filter);
3939
void UpdateConversationTitle(string conversationId, string title);
4040
List<Conversation> GetLastConversations();
41-
void AddExectionLogs(string conversationId, List<string> logs);
42-
List<string> GetExectionLogs(string conversationId);
41+
void AddExecutionLogs(string conversationId, List<string> logs);
42+
List<string> GetExecutionLogs(string conversationId);
4343
#endregion
4444

4545
#region LLM Completion Log

src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public void Append(string conversationId, string content)
2121
content = content.Replace("\r\n", " ").Replace("\n", " ");
2222
content = Regex.Replace(content, @"\s+", " ");
2323
var db = _services.GetRequiredService<IBotSharpRepository>();
24-
db.AddExectionLogs(conversationId, new List<string> { content });
24+
db.AddExecutionLogs(conversationId, new List<string> { content });
2525
}
2626
}

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,31 +168,34 @@ public void UpdateConversationStatus(string conversationId, string status)
168168
{
169169
throw new NotImplementedException();
170170
}
171+
#endregion
172+
171173

172-
public void AddExectionLogs(string conversationId, List<string> logs)
174+
#region User
175+
public User? GetUserByEmail(string email)
173176
{
174177
throw new NotImplementedException();
175178
}
176179

177-
public List<string> GetExectionLogs(string conversationId)
180+
public User? GetUserById(string id)
178181
{
179182
throw new NotImplementedException();
180183
}
181-
#endregion
182-
183184

184-
#region User
185-
public User? GetUserByEmail(string email)
185+
public void CreateUser(User user)
186186
{
187187
throw new NotImplementedException();
188188
}
189+
#endregion
189190

190-
public User? GetUserById(string id)
191+
192+
#region Execution Log
193+
public void AddExecutionLogs(string conversationId, List<string> logs)
191194
{
192195
throw new NotImplementedException();
193196
}
194197

195-
public void CreateUser(User user)
198+
public List<string> GetExecutionLogs(string conversationId)
196199
{
197200
throw new NotImplementedException();
198201
}

src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
using MongoDB.Driver;
77
using BotSharp.Abstraction.Routing.Models;
88
using BotSharp.Abstraction.Repositories.Filters;
9-
using BotSharp.Abstraction.Utilities;
10-
using BotSharp.Abstraction.Conversations.Models;
119

1210
namespace BotSharp.Core.Repository;
1311

@@ -789,33 +787,6 @@ public List<Conversation> GetLastConversations()
789787
.Select(g => g.OrderByDescending(x => x.CreatedTime).First())
790788
.ToList();
791789
}
792-
793-
public void AddExectionLogs(string conversationId, List<string> logs)
794-
{
795-
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
796-
797-
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
798-
if (!Directory.Exists(dir))
799-
{
800-
Directory.CreateDirectory(dir);
801-
}
802-
803-
var file = Path.Combine(dir, "execution.log");
804-
File.AppendAllLines(file, logs);
805-
}
806-
807-
public List<string> GetExectionLogs(string conversationId)
808-
{
809-
var logs = new List<string>();
810-
if (string.IsNullOrEmpty(conversationId)) return logs;
811-
812-
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
813-
if (!Directory.Exists(dir)) return logs;
814-
815-
var file = Path.Combine(dir, "execution.log");
816-
logs = File.ReadAllLines(file)?.ToList() ?? new List<string>();
817-
return logs;
818-
}
819790
#endregion
820791

821792
#region User
@@ -843,9 +814,44 @@ public void CreateUser(User user)
843814
}
844815
#endregion
845816

817+
#region Execution Log
818+
public void AddExecutionLogs(string conversationId, List<string> logs)
819+
{
820+
var isEnabled = _dbSettings.Log.EnableExecutionLog;
821+
if (!isEnabled) return;
822+
823+
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
824+
825+
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
826+
if (!Directory.Exists(dir))
827+
{
828+
Directory.CreateDirectory(dir);
829+
}
830+
831+
var file = Path.Combine(dir, "execution.log");
832+
File.AppendAllLines(file, logs);
833+
}
834+
835+
public List<string> GetExecutionLogs(string conversationId)
836+
{
837+
var logs = new List<string>();
838+
if (string.IsNullOrEmpty(conversationId)) return logs;
839+
840+
var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId);
841+
if (!Directory.Exists(dir)) return logs;
842+
843+
var file = Path.Combine(dir, "execution.log");
844+
logs = File.ReadAllLines(file)?.ToList() ?? new List<string>();
845+
return logs;
846+
}
847+
#endregion
848+
846849
#region LLM Completion Log
847850
public void SaveLlmCompletionLog(LlmCompletionLog log)
848851
{
852+
var isEnabled = _dbSettings.Log.EnableLlmCompletionLog;
853+
if (!isEnabled) return;
854+
849855
var convDir = FindConversationDirectory(log.ConversationId);
850856
if (!Directory.Exists(convDir)) return;
851857

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ namespace BotSharp.Plugin.MongoStorage.Repository;
1212

1313
public class MongoRepository : IBotSharpRepository
1414
{
15+
private readonly BotSharpDatabaseSettings _dbSettings;
1516
private readonly MongoDbContext _dc;
1617
private readonly IServiceProvider _services;
1718
private UpdateOptions _options;
1819

19-
public MongoRepository(MongoDbContext dc, IServiceProvider services)
20+
public MongoRepository(BotSharpDatabaseSettings dbSettings, MongoDbContext dc, IServiceProvider services)
2021
{
22+
_dbSettings = dbSettings;
2123
_dc = dc;
2224
_services = services;
2325
_options = new UpdateOptions
@@ -782,30 +784,6 @@ public List<Conversation> GetLastConversations()
782784
UpdatedTime = c.UpdatedTime
783785
}).ToList();
784786
}
785-
786-
public void AddExectionLogs(string conversationId, List<string> logs)
787-
{
788-
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
789-
790-
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
791-
var update = Builders<ExectionLogCollection>.Update
792-
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
793-
.PushEach(x => x.Logs, logs);
794-
795-
_dc.ExectionLogs.UpdateOne(filter, update, _options);
796-
}
797-
798-
public List<string> GetExectionLogs(string conversationId)
799-
{
800-
var logs = new List<string>();
801-
if (string.IsNullOrEmpty(conversationId)) return logs;
802-
803-
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
804-
var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault();
805-
806-
logs = logCollection?.Logs ?? new List<string>();
807-
return logs;
808-
}
809787
#endregion
810788

811789
#region User
@@ -863,9 +841,41 @@ public void CreateUser(User user)
863841
}
864842
#endregion
865843

844+
#region Execution Log
845+
public void AddExecutionLogs(string conversationId, List<string> logs)
846+
{
847+
var isEnabled = _dbSettings.Log.EnableExecutionLog;
848+
if (!isEnabled) return;
849+
850+
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
851+
852+
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
853+
var update = Builders<ExectionLogCollection>.Update
854+
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
855+
.PushEach(x => x.Logs, logs);
856+
857+
_dc.ExectionLogs.UpdateOne(filter, update, _options);
858+
}
859+
860+
public List<string> GetExecutionLogs(string conversationId)
861+
{
862+
var logs = new List<string>();
863+
if (string.IsNullOrEmpty(conversationId)) return logs;
864+
865+
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
866+
var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault();
867+
868+
logs = logCollection?.Logs ?? new List<string>();
869+
return logs;
870+
}
871+
#endregion
872+
866873
#region LLM Completion Log
867874
public void SaveLlmCompletionLog(LlmCompletionLog log)
868875
{
876+
var isEnabled = _dbSettings.Log.EnableLlmCompletionLog;
877+
if (!isEnabled) return;
878+
869879
var completiongLog = new LlmCompletionLogCollection
870880
{
871881
Id = string.IsNullOrEmpty(log.Id) ? Guid.NewGuid().ToString() : log.Id,

src/WebStarter/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@
104104
"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",
105105
"Slavers": []
106106
},
107+
"Log": {
108+
"EnableLlmCompletionLog": false,
109+
"EnableExecutionLog": true
110+
},
107111
"FileRepository": "data",
108112
"UseCamelCase": true,
109113
"Assemblies": [ "BotSharp.Core" ]

0 commit comments

Comments
 (0)