Merge pull request #222 from iceljc/features/add-llm-completion-log
add llm completion log
This commit is contained in:
commit
e8b654f820
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
@ -41,4 +41,8 @@ public interface IBotSharpRepository
|
||||||
void AddExectionLogs(string conversationId, List<string> logs);
|
void AddExectionLogs(string conversationId, List<string> logs);
|
||||||
List<string> GetExectionLogs(string conversationId);
|
List<string> GetExectionLogs(string conversationId);
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region LLM Completion Log
|
||||||
|
void SaveLlmCompletionLog(LlmCompletionLog log);
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -197,4 +197,11 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region LLM Completion Log
|
||||||
|
public void SaveLlmCompletionLog(LlmCompletionLog log)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ using MongoDB.Driver;
|
||||||
using BotSharp.Abstraction.Routing.Models;
|
using BotSharp.Abstraction.Routing.Models;
|
||||||
using BotSharp.Abstraction.Repositories.Filters;
|
using BotSharp.Abstraction.Repositories.Filters;
|
||||||
using BotSharp.Abstraction.Utilities;
|
using BotSharp.Abstraction.Utilities;
|
||||||
|
using BotSharp.Abstraction.Conversations.Models;
|
||||||
|
|
||||||
namespace BotSharp.Core.Repository;
|
namespace BotSharp.Core.Repository;
|
||||||
|
|
||||||
|
|
@ -559,7 +560,7 @@ public class FileRepository : IBotSharpRepository
|
||||||
foreach (var file in Directory.GetFiles(dir))
|
foreach (var file in Directory.GetFiles(dir))
|
||||||
{
|
{
|
||||||
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
|
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
|
||||||
var splits = fileName.ToLower().Split('.');
|
var splits = ParseFileNameByPath(fileName.ToLower());
|
||||||
var name = splits[0];
|
var name = splits[0];
|
||||||
var extension = splits[1];
|
var extension = splits[1];
|
||||||
if (name.IsEqualTo(templateName) && extension.IsEqualTo(_agentSettings.TemplateFormat))
|
if (name.IsEqualTo(templateName) && extension.IsEqualTo(_agentSettings.TemplateFormat))
|
||||||
|
|
@ -617,10 +618,10 @@ public class FileRepository : IBotSharpRepository
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(conversationId)) return false;
|
if (string.IsNullOrEmpty(conversationId)) return false;
|
||||||
|
|
||||||
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversationId);
|
var convDir = FindConversationDirectory(conversationId);
|
||||||
if (!Directory.Exists(dir)) return false;
|
if (string.IsNullOrEmpty(convDir)) return false;
|
||||||
|
|
||||||
Directory.Delete(dir, true);
|
Directory.Delete(convDir, true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -842,6 +843,25 @@ public class FileRepository : IBotSharpRepository
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region LLM Completion Log
|
||||||
|
public void SaveLlmCompletionLog(LlmCompletionLog log)
|
||||||
|
{
|
||||||
|
var convDir = FindConversationDirectory(log.ConversationId);
|
||||||
|
if (!Directory.Exists(convDir)) return;
|
||||||
|
|
||||||
|
var logDir = Path.Combine(convDir, "llm_prompt_log");
|
||||||
|
if (!Directory.Exists(logDir))
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(logDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
var index = GetLlmCompletionLogIndex(logDir, log.MessageId);
|
||||||
|
var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log");
|
||||||
|
File.WriteAllText(file, JsonSerializer.Serialize(log, _options));
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Private methods
|
#region Private methods
|
||||||
private string GetAgentDataDir(string agentId)
|
private string GetAgentDataDir(string agentId)
|
||||||
{
|
{
|
||||||
|
|
@ -934,22 +954,10 @@ public class FileRepository : IBotSharpRepository
|
||||||
|
|
||||||
private string? FindConversationDirectory(string conversationId)
|
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))
|
return 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<DialogElement> CollectDialogElements(string dialogDir)
|
private List<DialogElement> CollectDialogElements(string dialogDir)
|
||||||
|
|
@ -1000,5 +1008,30 @@ public class FileRepository : IBotSharpRepository
|
||||||
}
|
}
|
||||||
return states;
|
return states;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int GetLlmCompletionLogIndex(string logDir, string id)
|
||||||
|
{
|
||||||
|
var files = Directory.GetFiles(logDir);
|
||||||
|
if (files.IsNullOrEmpty())
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var logIndexes = files.Where(file =>
|
||||||
|
{
|
||||||
|
var fileName = ParseFileNameByPath(file);
|
||||||
|
return fileName[0].IsEqualTo(id);
|
||||||
|
}).Select(file =>
|
||||||
|
{
|
||||||
|
var fileName = ParseFileNameByPath(file);
|
||||||
|
return int.Parse(fileName[1]);
|
||||||
|
}).ToList();
|
||||||
|
|
||||||
|
return logIndexes.IsNullOrEmpty() ? 0 : logIndexes.Max() + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string[] ParseFileNameByPath(string path, string separator = ".")
|
||||||
|
{
|
||||||
|
var name = path.Split(Path.DirectorySeparatorChar).Last();
|
||||||
|
return name.Split(separator);
|
||||||
|
}
|
||||||
#endregion
|
#endregion
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,3 @@
|
||||||
using MongoDB.Bson.Serialization.Attributes;
|
|
||||||
|
|
||||||
namespace BotSharp.Plugin.MongoStorage;
|
namespace BotSharp.Plugin.MongoStorage;
|
||||||
|
|
||||||
[BsonIgnoreExtraElements(Inherited = true)]
|
[BsonIgnoreExtraElements(Inherited = true)]
|
||||||
|
|
|
||||||
|
|
@ -45,4 +45,7 @@ public class MongoDbContext
|
||||||
|
|
||||||
public IMongoCollection<UserAgentCollection> UserAgents
|
public IMongoCollection<UserAgentCollection> UserAgents
|
||||||
=> Database.GetCollection<UserAgentCollection>($"{_collectionPrefix}_UserAgents");
|
=> Database.GetCollection<UserAgentCollection>($"{_collectionPrefix}_UserAgents");
|
||||||
|
|
||||||
|
public IMongoCollection<LlmCompletionLogCollection> LlmCompletionLogs
|
||||||
|
=> Database.GetCollection<LlmCompletionLogCollection>($"{_collectionPrefix}_Llm_Completion_Logs");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -862,4 +862,22 @@ public class MongoRepository : IBotSharpRepository
|
||||||
_dc.Users.InsertOne(userCollection);
|
_dc.Users.InsertOne(userCollection);
|
||||||
}
|
}
|
||||||
#endregion
|
#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
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue