diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/LlmCompletionLog.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/LlmCompletionLog.cs new file mode 100644 index 00000000..26f55494 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/LlmCompletionLog.cs @@ -0,0 +1,12 @@ +namespace BotSharp.Abstraction.Conversations.Models; + +public class LlmCompletionLog +{ + public string Id { get; set; } = string.Empty; + public string ConversationId { get; set; } = string.Empty; + public string MessageId { get; set; } = string.Empty; + public string AgentId { get; set; } = string.Empty; + public string Prompt { get; set; } = string.Empty; + public string? Response { get; set; } + public DateTime CreateDateTime { get; set; } = DateTime.UtcNow; +} diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 1598537e..ba8e9d84 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -41,4 +41,8 @@ public interface IBotSharpRepository void AddExectionLogs(string conversationId, List logs); List GetExectionLogs(string conversationId); #endregion + + #region LLM Completion Log + void SaveLlmCompletionLog(LlmCompletionLog log); + #endregion } diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index af1fd014..48c1a780 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -197,4 +197,11 @@ public class BotSharpDbContext : Database, IBotSharpRepository throw new NotImplementedException(); } #endregion + + #region LLM Completion Log + public void SaveLlmCompletionLog(LlmCompletionLog log) + { + throw new NotImplementedException(); + } + #endregion } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index aae9455b..306acc4b 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -7,6 +7,7 @@ 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; @@ -617,10 +618,10 @@ public class FileRepository : IBotSharpRepository { if (string.IsNullOrEmpty(conversationId)) return false; - var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversationId); - if (!Directory.Exists(dir)) return false; + var convDir = FindConversationDirectory(conversationId); + if (string.IsNullOrEmpty(convDir)) return false; - Directory.Delete(dir, true); + Directory.Delete(convDir, true); return true; } @@ -842,6 +843,25 @@ public class FileRepository : IBotSharpRepository } #endregion + #region LLM Completion Log + public void SaveLlmCompletionLog(LlmCompletionLog log) + { + var convDir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, log.ConversationId); + if (!Directory.Exists(convDir)) return; + + var logDir = Path.Combine(convDir, "llm_prompt_log"); + if (!Directory.Exists(logDir)) + { + Directory.CreateDirectory(logDir); + } + + var fileName = string.IsNullOrEmpty(log.Id) ? Guid.NewGuid().ToString() : log.Id; + var file = Path.Combine(logDir, $"{fileName}.log"); + File.WriteAllText(file, JsonSerializer.Serialize(log, _options)); + } + #endregion + + #region Private methods private string GetAgentDataDir(string agentId) { @@ -934,22 +954,10 @@ public class FileRepository : IBotSharpRepository private string? FindConversationDirectory(string conversationId) { - var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); + var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversationId); + if (!Directory.Exists(dir)) return null; - foreach (var d in Directory.GetDirectories(dir)) - { - var path = Path.Combine(d, "conversation.json"); - if (!File.Exists(path)) continue; - - var json = File.ReadAllText(path); - var conv = JsonSerializer.Deserialize(json, _options); - if (conv != null && conv.Id == conversationId) - { - return d; - } - } - - return null; + return dir; } private List CollectDialogElements(string dialogDir) diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/LlmCompletionLogCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/LlmCompletionLogCollection.cs new file mode 100644 index 00000000..26674262 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/LlmCompletionLogCollection.cs @@ -0,0 +1,11 @@ +namespace BotSharp.Plugin.MongoStorage.Collections; + +public class LlmCompletionLogCollection : MongoBase +{ + public string ConversationId { get; set; } + public string MessageId { get; set; } + public string AgentId { get; set; } + public string Prompt { get; set; } + public string? Response { get; set; } + public DateTime CreateDateTime { get; set; } +} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoBase.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoBase.cs index 6d8842bc..0af038a5 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoBase.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoBase.cs @@ -1,5 +1,3 @@ -using MongoDB.Bson.Serialization.Attributes; - namespace BotSharp.Plugin.MongoStorage; [BsonIgnoreExtraElements(Inherited = true)] diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index c8fb7e28..86992094 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -45,4 +45,7 @@ public class MongoDbContext public IMongoCollection UserAgents => Database.GetCollection($"{_collectionPrefix}_UserAgents"); + + public IMongoCollection LlmCompletionLogs + => Database.GetCollection($"{_collectionPrefix}_Llm_Completion_Logs"); } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index 61f3ffd3..553ca34e 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -862,4 +862,22 @@ public class MongoRepository : IBotSharpRepository _dc.Users.InsertOne(userCollection); } #endregion + + #region LLM Completion Log + public void SaveLlmCompletionLog(LlmCompletionLog log) + { + var completiongLog = new LlmCompletionLogCollection + { + Id = string.IsNullOrEmpty(log.Id) ? Guid.NewGuid().ToString() : log.Id, + ConversationId = log.ConversationId, + MessageId = log.MessageId, + AgentId = log.AgentId, + Prompt = log.Prompt, + Response = log.Response, + CreateDateTime = log.CreateDateTime + }; + + _dc.LlmCompletionLogs.InsertOne(completiongLog); + } + #endregion }