Skip to content

Commit e8b654f

Browse files
authored
Merge pull request #222 from iceljc/features/add-llm-completion-log
add llm completion log
2 parents bc5818f + 258fcb9 commit e8b654f

File tree

8 files changed

+107
-21
lines changed

8 files changed

+107
-21
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace BotSharp.Abstraction.Conversations.Models;
2+
3+
public class LlmCompletionLog
4+
{
5+
public string Id { get; set; } = string.Empty;
6+
public string ConversationId { get; set; } = string.Empty;
7+
public string MessageId { get; set; } = string.Empty;
8+
public string AgentId { get; set; } = string.Empty;
9+
public string Prompt { get; set; } = string.Empty;
10+
public string? Response { get; set; }
11+
public DateTime CreateDateTime { get; set; } = DateTime.UtcNow;
12+
}

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ public interface IBotSharpRepository
4141
void AddExectionLogs(string conversationId, List<string> logs);
4242
List<string> GetExectionLogs(string conversationId);
4343
#endregion
44+
45+
#region LLM Completion Log
46+
void SaveLlmCompletionLog(LlmCompletionLog log);
47+
#endregion
4448
}

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,11 @@ public void CreateUser(User user)
197197
throw new NotImplementedException();
198198
}
199199
#endregion
200+
201+
#region LLM Completion Log
202+
public void SaveLlmCompletionLog(LlmCompletionLog log)
203+
{
204+
throw new NotImplementedException();
205+
}
206+
#endregion
200207
}

src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using BotSharp.Abstraction.Routing.Models;
88
using BotSharp.Abstraction.Repositories.Filters;
99
using BotSharp.Abstraction.Utilities;
10+
using BotSharp.Abstraction.Conversations.Models;
1011

1112
namespace BotSharp.Core.Repository;
1213

@@ -559,7 +560,7 @@ public string GetAgentTemplate(string agentId, string templateName)
559560
foreach (var file in Directory.GetFiles(dir))
560561
{
561562
var fileName = file.Split(Path.DirectorySeparatorChar).Last();
562-
var splits = fileName.ToLower().Split('.');
563+
var splits = ParseFileNameByPath(fileName.ToLower());
563564
var name = splits[0];
564565
var extension = splits[1];
565566
if (name.IsEqualTo(templateName) && extension.IsEqualTo(_agentSettings.TemplateFormat))
@@ -617,10 +618,10 @@ public bool DeleteConversation(string conversationId)
617618
{
618619
if (string.IsNullOrEmpty(conversationId)) return false;
619620

620-
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversationId);
621-
if (!Directory.Exists(dir)) return false;
621+
var convDir = FindConversationDirectory(conversationId);
622+
if (string.IsNullOrEmpty(convDir)) return false;
622623

623-
Directory.Delete(dir, true);
624+
Directory.Delete(convDir, true);
624625
return true;
625626
}
626627

@@ -842,6 +843,25 @@ public void CreateUser(User user)
842843
}
843844
#endregion
844845

846+
#region LLM Completion Log
847+
public void SaveLlmCompletionLog(LlmCompletionLog log)
848+
{
849+
var convDir = FindConversationDirectory(log.ConversationId);
850+
if (!Directory.Exists(convDir)) return;
851+
852+
var logDir = Path.Combine(convDir, "llm_prompt_log");
853+
if (!Directory.Exists(logDir))
854+
{
855+
Directory.CreateDirectory(logDir);
856+
}
857+
858+
var index = GetLlmCompletionLogIndex(logDir, log.MessageId);
859+
var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log");
860+
File.WriteAllText(file, JsonSerializer.Serialize(log, _options));
861+
}
862+
#endregion
863+
864+
845865
#region Private methods
846866
private string GetAgentDataDir(string agentId)
847867
{
@@ -934,22 +954,10 @@ private List<AgentResponse> FetchResponses(string fileDir)
934954

935955
private string? FindConversationDirectory(string conversationId)
936956
{
937-
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
938-
939-
foreach (var d in Directory.GetDirectories(dir))
940-
{
941-
var path = Path.Combine(d, "conversation.json");
942-
if (!File.Exists(path)) continue;
943-
944-
var json = File.ReadAllText(path);
945-
var conv = JsonSerializer.Deserialize<Conversation>(json, _options);
946-
if (conv != null && conv.Id == conversationId)
947-
{
948-
return d;
949-
}
950-
}
957+
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversationId);
958+
if (!Directory.Exists(dir)) return null;
951959

952-
return null;
960+
return dir;
953961
}
954962

955963
private List<DialogElement> CollectDialogElements(string dialogDir)
@@ -1000,5 +1008,30 @@ private List<StateKeyValue> CollectConversationStates(string stateDir)
10001008
}
10011009
return states;
10021010
}
1011+
1012+
private int GetLlmCompletionLogIndex(string logDir, string id)
1013+
{
1014+
var files = Directory.GetFiles(logDir);
1015+
if (files.IsNullOrEmpty())
1016+
return 0;
1017+
1018+
var logIndexes = files.Where(file =>
1019+
{
1020+
var fileName = ParseFileNameByPath(file);
1021+
return fileName[0].IsEqualTo(id);
1022+
}).Select(file =>
1023+
{
1024+
var fileName = ParseFileNameByPath(file);
1025+
return int.Parse(fileName[1]);
1026+
}).ToList();
1027+
1028+
return logIndexes.IsNullOrEmpty() ? 0 : logIndexes.Max() + 1;
1029+
}
1030+
1031+
private string[] ParseFileNameByPath(string path, string separator = ".")
1032+
{
1033+
var name = path.Split(Path.DirectorySeparatorChar).Last();
1034+
return name.Split(separator);
1035+
}
10031036
#endregion
10041037
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace BotSharp.Plugin.MongoStorage.Collections;
2+
3+
public class LlmCompletionLogCollection : MongoBase
4+
{
5+
public string ConversationId { get; set; }
6+
public string MessageId { get; set; }
7+
public string AgentId { get; set; }
8+
public string Prompt { get; set; }
9+
public string? Response { get; set; }
10+
public DateTime CreateDateTime { get; set; }
11+
}

src/Plugins/BotSharp.Plugin.MongoStorage/MongoBase.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using MongoDB.Bson.Serialization.Attributes;
2-
31
namespace BotSharp.Plugin.MongoStorage;
42

53
[BsonIgnoreExtraElements(Inherited = true)]

src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ public IMongoCollection<UserCollection> Users
4545

4646
public IMongoCollection<UserAgentCollection> UserAgents
4747
=> Database.GetCollection<UserAgentCollection>($"{_collectionPrefix}_UserAgents");
48+
49+
public IMongoCollection<LlmCompletionLogCollection> LlmCompletionLogs
50+
=> Database.GetCollection<LlmCompletionLogCollection>($"{_collectionPrefix}_Llm_Completion_Logs");
4851
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,4 +862,22 @@ public void CreateUser(User user)
862862
_dc.Users.InsertOne(userCollection);
863863
}
864864
#endregion
865+
866+
#region LLM Completion Log
867+
public void SaveLlmCompletionLog(LlmCompletionLog log)
868+
{
869+
var completiongLog = new LlmCompletionLogCollection
870+
{
871+
Id = string.IsNullOrEmpty(log.Id) ? Guid.NewGuid().ToString() : log.Id,
872+
ConversationId = log.ConversationId,
873+
MessageId = log.MessageId,
874+
AgentId = log.AgentId,
875+
Prompt = log.Prompt,
876+
Response = log.Response,
877+
CreateDateTime = log.CreateDateTime
878+
};
879+
880+
_dc.LlmCompletionLogs.InsertOne(completiongLog);
881+
}
882+
#endregion
865883
}

0 commit comments

Comments
 (0)