Skip to content

Commit 3655935

Browse files
authored
Merge pull request #241 from iceljc/bugfix/refine-llm-prompt-log
Bugfix/refine llm prompt log
2 parents bd26c81 + c479045 commit 3655935

File tree

9 files changed

+107
-17
lines changed

9 files changed

+107
-17
lines changed

src/Infrastructure/BotSharp.Abstraction/Conversations/Models/LlmCompletionLog.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ namespace BotSharp.Abstraction.Conversations.Models;
22

33
public class LlmCompletionLog
44
{
5-
public string Id { get; set; } = string.Empty;
65
public string ConversationId { get; set; } = string.Empty;
76
public string MessageId { get; set; } = string.Empty;
87
public string AgentId { get; set; } = string.Empty;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using BotSharp.Abstraction.Repositories.Filters;
2+
using BotSharp.Abstraction.Repositories.Models;
23
using BotSharp.Abstraction.Users.Models;
34

45
namespace BotSharp.Abstraction.Repositories;
@@ -30,6 +31,7 @@ public interface IBotSharpRepository
3031
void CreateNewConversation(Conversation conversation);
3132
bool DeleteConversation(string conversationId);
3233
List<DialogElement> GetConversationDialogs(string conversationId);
34+
void UpdateConversationDialogElements(string conversationId, List<DialogContentUpdateModel> updateElements);
3335
void AppendConversationDialogs(string conversationId, List<DialogElement> dialogs);
3436
List<StateKeyValue> GetConversationStates(string conversationId);
3537
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace BotSharp.Abstraction.Repositories.Models
2+
{
3+
public class DialogContentUpdateModel
4+
{
5+
public int Index { get; set; }
6+
public string UpdateContent { get; set; }
7+
}
8+
}

src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ public static string RemoveNewLine(this string input)
3737
return input.Replace("\r", " ").Replace("\n", " ").Trim();
3838
}
3939

40-
public static bool IsEqualTo(this string str1, string str2, StringComparison option = StringComparison.OrdinalIgnoreCase)
40+
public static bool IsEqualTo(this string? str1, string? str2, StringComparison option = StringComparison.OrdinalIgnoreCase)
4141
{
42+
if (str1 == null) return str2 == null;
43+
4244
return str1.Equals(str2, option);
4345
}
4446

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using BotSharp.Abstraction.Agents.Models;
22
using BotSharp.Abstraction.Repositories;
33
using BotSharp.Abstraction.Repositories.Filters;
4+
using BotSharp.Abstraction.Repositories.Models;
45
using BotSharp.Abstraction.Users.Models;
56
using Microsoft.EntityFrameworkCore.Infrastructure;
67

@@ -146,6 +147,11 @@ public List<DialogElement> GetConversationDialogs(string conversationId)
146147
throw new NotImplementedException();
147148
}
148149

150+
public void UpdateConversationDialogElements(string conversationId, List<DialogContentUpdateModel> updateElements)
151+
{
152+
throw new NotImplementedException();
153+
}
154+
149155
public List<StateKeyValue> GetConversationStates(string conversationId)
150156
{
151157
throw new NotImplementedException();

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using MongoDB.Driver;
77
using BotSharp.Abstraction.Routing.Models;
88
using BotSharp.Abstraction.Repositories.Filters;
9+
using BotSharp.Abstraction.Utilities;
10+
using BotSharp.Abstraction.Conversations.Models;
11+
using BotSharp.Abstraction.Repositories.Models;
912

1013
namespace BotSharp.Core.Repository;
1114

@@ -650,6 +653,33 @@ public List<DialogElement> GetConversationDialogs(string conversationId)
650653
return dialogs;
651654
}
652655

656+
public void UpdateConversationDialogElements(string conversationId, List<DialogContentUpdateModel> updateElements)
657+
{
658+
var dialogElements = GetConversationDialogs(conversationId);
659+
if (dialogElements.IsNullOrEmpty() || updateElements.IsNullOrEmpty()) return;
660+
661+
var convDir = FindConversationDirectory(conversationId);
662+
if (!string.IsNullOrEmpty(convDir))
663+
{
664+
var dialogDir = Path.Combine(convDir, "dialogs.txt");
665+
if (File.Exists(dialogDir))
666+
{
667+
var updated = dialogElements.Select((x, idx) =>
668+
{
669+
var found = updateElements.FirstOrDefault(e => e.Index == idx);
670+
if (found != null)
671+
{
672+
x.Content = found.UpdateContent;
673+
}
674+
return x;
675+
}).ToList();
676+
677+
var texts = ParseDialogElements(updated);
678+
File.WriteAllLines(dialogDir, texts);
679+
}
680+
}
681+
}
682+
653683
public void AppendConversationDialogs(string conversationId, List<DialogElement> dialogs)
654684
{
655685
var convDir = FindConversationDirectory(conversationId);
@@ -860,18 +890,24 @@ public List<string> GetExecutionLogs(string conversationId)
860890
#region LLM Completion Log
861891
public void SaveLlmCompletionLog(LlmCompletionLog log)
862892
{
863-
if (log == null || string.IsNullOrEmpty(log.ConversationId)) return;
893+
if (log == null) return;
894+
895+
log.ConversationId = log.ConversationId.IfNullOrEmptyAs(Guid.NewGuid().ToString());
896+
log.MessageId = log.MessageId.IfNullOrEmptyAs(Guid.NewGuid().ToString());
864897

865898
var convDir = FindConversationDirectory(log.ConversationId);
866-
if (string.IsNullOrEmpty(convDir)) return;
899+
if (string.IsNullOrEmpty(convDir))
900+
{
901+
convDir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, log.ConversationId);
902+
Directory.CreateDirectory(convDir);
903+
}
867904

868905
var logDir = Path.Combine(convDir, "llm_prompt_log");
869906
if (!Directory.Exists(logDir))
870907
{
871908
Directory.CreateDirectory(logDir);
872909
}
873910

874-
log.Id = Guid.NewGuid().ToString();
875911
var index = GetNextLlmCompletionLogIndex(logDir, log.MessageId);
876912
var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log");
877913
File.WriteAllText(file, JsonSerializer.Serialize(log, _options));
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1+
using BotSharp.Plugin.MongoStorage.Models;
2+
13
namespace BotSharp.Plugin.MongoStorage.Collections;
24

35
public class LlmCompletionLogDocument : MongoBase
46
{
57
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; }
8+
public List<PromptLogElement> Logs { get; set; }
119
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace BotSharp.Plugin.MongoStorage.Models;
2+
3+
public class PromptLogElement
4+
{
5+
public string MessageId { get; set; }
6+
public string AgentId { get; set; }
7+
public string Prompt { get; set; }
8+
public string? Response { get; set; }
9+
public DateTime CreateDateTime { get; set; }
10+
}

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using BotSharp.Abstraction.Conversations.Models;
33
using BotSharp.Abstraction.Functions.Models;
44
using BotSharp.Abstraction.Repositories.Filters;
5+
using BotSharp.Abstraction.Repositories.Models;
56
using BotSharp.Abstraction.Routing.Models;
67
using BotSharp.Abstraction.Users.Models;
78
using BotSharp.Plugin.MongoStorage.Collections;
@@ -635,7 +636,8 @@ public bool DeleteConversation(string conversationId)
635636
var promptLogDeleted = _dc.LlmCompletionLogs.DeleteMany(filterPromptLog);
636637
var dialogDeleted = _dc.ConversationDialogs.DeleteMany(filterDialog);
637638
var convDeleted = _dc.Conversations.DeleteMany(filterConv);
638-
return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0;
639+
return convDeleted.DeletedCount > 0 || dialogDeleted.DeletedCount > 0
640+
|| exeLogDeleted.DeletedCount > 0 || promptLogDeleted.DeletedCount > 0;
639641
}
640642

641643
public List<DialogElement> GetConversationDialogs(string conversationId)
@@ -651,6 +653,27 @@ public List<DialogElement> GetConversationDialogs(string conversationId)
651653
return formattedDialog ?? new List<DialogElement>();
652654
}
653655

656+
public void UpdateConversationDialogElements(string conversationId, List<DialogContentUpdateModel> updateElements)
657+
{
658+
if (string.IsNullOrEmpty(conversationId) || updateElements.IsNullOrEmpty()) return;
659+
660+
var filterDialog = Builders<ConversationDialogDocument>.Filter.Eq(x => x.ConversationId, conversationId);
661+
var foundDialog = _dc.ConversationDialogs.Find(filterDialog).FirstOrDefault();
662+
if (foundDialog == null || foundDialog.Dialogs.IsNullOrEmpty()) return;
663+
664+
foundDialog.Dialogs = foundDialog.Dialogs.Select((x, idx) =>
665+
{
666+
var found = updateElements.FirstOrDefault(e => e.Index == idx);
667+
if (found != null)
668+
{
669+
x.Content = found.UpdateContent;
670+
}
671+
return x;
672+
}).ToList();
673+
674+
_dc.ConversationDialogs.ReplaceOne(filterDialog, foundDialog);
675+
}
676+
654677
public void AppendConversationDialogs(string conversationId, List<DialogElement> dialogs)
655678
{
656679
if (string.IsNullOrEmpty(conversationId)) return;
@@ -892,20 +915,26 @@ public List<string> GetExecutionLogs(string conversationId)
892915
#region LLM Completion Log
893916
public void SaveLlmCompletionLog(LlmCompletionLog log)
894917
{
895-
if (log == null || string.IsNullOrEmpty(log.ConversationId)) return;
918+
if (log == null) return;
919+
920+
var conversationId = log.ConversationId.IfNullOrEmptyAs(Guid.NewGuid().ToString());
921+
var messageId = log.MessageId.IfNullOrEmptyAs(Guid.NewGuid().ToString());
896922

897-
var completiongLog = new LlmCompletionLogDocument
923+
var logElement = new PromptLogElement
898924
{
899-
Id = string.IsNullOrEmpty(log.Id) ? Guid.NewGuid().ToString() : log.Id,
900-
ConversationId = log.ConversationId,
901-
MessageId = log.MessageId,
925+
MessageId = messageId,
902926
AgentId = log.AgentId,
903927
Prompt = log.Prompt,
904928
Response = log.Response,
905929
CreateDateTime = log.CreateDateTime
906930
};
907931

908-
_dc.LlmCompletionLogs.InsertOne(completiongLog);
932+
var filter = Builders<LlmCompletionLogDocument>.Filter.Eq(x => x.ConversationId, conversationId);
933+
var update = Builders<LlmCompletionLogDocument>.Update
934+
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
935+
.Push(x => x.Logs, logElement);
936+
937+
_dc.LlmCompletionLogs.UpdateOne(filter, update, _options);
909938
}
910939
#endregion
911940
}

0 commit comments

Comments
 (0)