add llm completion log

This commit is contained in:
Jicheng Lu 2023-11-27 17:49:24 -06:00
parent bc5818fc24
commit 7b365d7b84
8 changed files with 81 additions and 20 deletions

View file

@ -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;
}

View file

@ -41,4 +41,8 @@ public interface IBotSharpRepository
void AddExectionLogs(string conversationId, List<string> logs);
List<string> GetExectionLogs(string conversationId);
#endregion
#region LLM Completion Log
void SaveLlmCompletionLog(LlmCompletionLog log);
#endregion
}

View file

@ -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
}

View file

@ -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<Conversation>(json, _options);
if (conv != null && conv.Id == conversationId)
{
return d;
}
}
return null;
return dir;
}
private List<DialogElement> CollectDialogElements(string dialogDir)

View file

@ -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; }
}

View file

@ -1,5 +1,3 @@
using MongoDB.Bson.Serialization.Attributes;
namespace BotSharp.Plugin.MongoStorage;
[BsonIgnoreExtraElements(Inherited = true)]

View file

@ -45,4 +45,7 @@ public class MongoDbContext
public IMongoCollection<UserAgentCollection> UserAgents
=> Database.GetCollection<UserAgentCollection>($"{_collectionPrefix}_UserAgents");
public IMongoCollection<LlmCompletionLogCollection> LlmCompletionLogs
=> Database.GetCollection<LlmCompletionLogCollection>($"{_collectionPrefix}_Llm_Completion_Logs");
}

View file

@ -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
}