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

114 lines
3.7 KiB
C#
Raw Normal View History

2024-02-15 20:43:36 +00:00
using BotSharp.Abstraction.Loggers.Models;
2024-01-13 02:13:38 +00:00
namespace BotSharp.Plugin.MongoStorage.Repository;
public partial class MongoRepository
{
#region LLM Completion Log
public void SaveLlmCompletionLog(LlmCompletionLog log)
{
if (log == null) return;
var conversationId = log.ConversationId.IfNullOrEmptyAs(Guid.NewGuid().ToString());
var messageId = log.MessageId.IfNullOrEmptyAs(Guid.NewGuid().ToString());
2025-02-20 18:17:21 +00:00
var data = new LlmCompletionLogDocument
2024-01-13 02:13:38 +00:00
{
2025-02-20 18:17:21 +00:00
Id = Guid.NewGuid().ToString(),
ConversationId = conversationId,
2024-01-13 02:13:38 +00:00
MessageId = messageId,
AgentId = log.AgentId,
Prompt = log.Prompt,
Response = log.Response,
2025-02-20 18:17:21 +00:00
CreatedTime = log.CreatedTime
2024-01-13 02:13:38 +00:00
};
2025-02-20 18:17:21 +00:00
_dc.LlmCompletionLogs.InsertOne(data);
2024-01-13 02:13:38 +00:00
}
#endregion
2024-02-15 20:43:36 +00:00
#region Conversation Content Log
2024-03-01 02:50:39 +00:00
public void SaveConversationContentLog(ContentLogOutputModel log)
2024-02-15 20:43:36 +00:00
{
if (log == null) return;
2024-03-26 21:57:13 +00:00
var found = _dc.Conversations.AsQueryable().FirstOrDefault(x => x.Id == log.ConversationId);
if (found == null) return;
2024-02-15 20:43:36 +00:00
var logDoc = new ConversationContentLogDocument
{
2024-03-26 21:57:13 +00:00
ConversationId = log.ConversationId,
MessageId = log.MessageId,
2024-02-15 20:43:36 +00:00
Name = log.Name,
2024-03-01 02:50:39 +00:00
AgentId = log.AgentId,
2024-02-15 20:43:36 +00:00
Role = log.Role,
2024-02-22 17:25:13 +00:00
Source = log.Source,
2024-02-15 20:43:36 +00:00
Content = log.Content,
CreatedTime = log.CreatedTime
2024-02-15 20:43:36 +00:00
};
_dc.ContentLogs.InsertOne(logDoc);
}
2024-03-01 02:50:39 +00:00
public List<ContentLogOutputModel> GetConversationContentLogs(string conversationId)
2024-02-15 20:43:36 +00:00
{
var logs = _dc.ContentLogs
.AsQueryable()
.Where(x => x.ConversationId == conversationId)
2024-03-01 02:50:39 +00:00
.Select(x => new ContentLogOutputModel
2024-02-15 20:43:36 +00:00
{
ConversationId = x.ConversationId,
MessageId = x.MessageId,
Name = x.Name,
2024-03-01 02:50:39 +00:00
AgentId = x.AgentId,
2024-02-15 20:43:36 +00:00
Role = x.Role,
2024-02-22 17:25:13 +00:00
Source = x.Source,
2024-02-15 20:43:36 +00:00
Content = x.Content,
CreatedTime = x.CreatedTime
2024-02-15 20:43:36 +00:00
})
.OrderBy(x => x.CreatedTime)
2024-02-15 20:43:36 +00:00
.ToList();
return logs;
}
#endregion
#region Conversation State Log
public void SaveConversationStateLog(ConversationStateLogModel log)
{
if (log == null) return;
2024-03-26 21:57:13 +00:00
var found = _dc.Conversations.AsQueryable().FirstOrDefault(x => x.Id == log.ConversationId);
if (found == null) return;
2024-02-15 20:43:36 +00:00
var logDoc = new ConversationStateLogDocument
{
2024-03-26 21:57:13 +00:00
ConversationId = log.ConversationId,
2025-01-29 22:21:19 +00:00
AgentId= log.AgentId,
2024-03-26 21:57:13 +00:00
MessageId = log.MessageId,
2024-02-15 20:43:36 +00:00
States = log.States,
CreatedTime = log.CreatedTime
2024-02-15 20:43:36 +00:00
};
_dc.StateLogs.InsertOne(logDoc);
}
public List<ConversationStateLogModel> GetConversationStateLogs(string conversationId)
{
var logs = _dc.StateLogs
.AsQueryable()
.Where(x => x.ConversationId == conversationId)
.Select(x => new ConversationStateLogModel
{
ConversationId = x.ConversationId,
2025-01-29 22:21:19 +00:00
AgentId = x.AgentId,
2024-02-15 20:43:36 +00:00
MessageId = x.MessageId,
States = x.States,
CreatedTime = x.CreatedTime
2024-02-15 20:43:36 +00:00
})
.OrderBy(x => x.CreatedTime)
2024-02-15 20:43:36 +00:00
.ToList();
return logs;
}
#endregion
2024-01-13 02:13:38 +00:00
}