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

142 lines
4.9 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 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)
{
var logs = new List<string>();
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 ?? new List<string>();
return logs;
}
#endregion
#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());
var logElement = new PromptLogMongoElement
{
MessageId = messageId,
AgentId = log.AgentId,
Prompt = log.Prompt,
Response = log.Response,
CreateDateTime = log.CreateDateTime
};
var filter = Builders<LlmCompletionLogDocument>.Filter.Eq(x => x.ConversationId, conversationId);
var update = Builders<LlmCompletionLogDocument>.Update
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
.Push(x => x.Logs, logElement);
_dc.LlmCompletionLogs.UpdateOne(filter, update, _options);
}
#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,
CreateTime = log.CreateTime
};
_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,
CreateTime = x.CreateTime
})
.OrderBy(x => x.CreateTime)
.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,
MessageId = log.MessageId,
2024-02-15 20:43:36 +00:00
States = log.States,
CreateTime = log.CreateTime
};
_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,
MessageId = x.MessageId,
States = x.States,
CreateTime = x.CreateTime
})
.OrderBy(x => x.CreateTime)
.ToList();
return logs;
}
#endregion
2024-01-13 02:13:38 +00:00
}