From 94f81cb173338ea9984834a59072a409128af1f5 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 19 Dec 2023 22:20:08 -0600 Subject: [PATCH] refine llm prompt log --- .../Utilities/StringExtensions.cs | 4 +++- .../BotSharp.Core/Repository/FileRepository.cs | 12 ++++++++++-- .../Repository/MongoRepository.cs | 10 ++++++---- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs index a7ffe5af..625a1f4b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Utilities/StringExtensions.cs @@ -37,8 +37,10 @@ public static class StringExtensions return input.Replace("\r", " ").Replace("\n", " ").Trim(); } - public static bool IsEqualTo(this string str1, string str2, StringComparison option = StringComparison.OrdinalIgnoreCase) + public static bool IsEqualTo(this string? str1, string? str2, StringComparison option = StringComparison.OrdinalIgnoreCase) { + if (str1 == null) return str2 == null; + return str1.Equals(str2, option); } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 81126616..4c5833f2 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -6,6 +6,8 @@ using BotSharp.Abstraction.Agents.Models; 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; @@ -860,10 +862,15 @@ public class FileRepository : IBotSharpRepository #region LLM Completion Log public void SaveLlmCompletionLog(LlmCompletionLog log) { - if (log == null || string.IsNullOrEmpty(log.ConversationId)) return; + if (log == null) return; + log.ConversationId = log.ConversationId.IfNullOrEmptyAs(Guid.Empty.ToString()); var convDir = FindConversationDirectory(log.ConversationId); - if (string.IsNullOrEmpty(convDir)) return; + if (string.IsNullOrEmpty(convDir)) + { + convDir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, log.ConversationId); + Directory.CreateDirectory(convDir); + } var logDir = Path.Combine(convDir, "llm_prompt_log"); if (!Directory.Exists(logDir)) @@ -872,6 +879,7 @@ public class FileRepository : IBotSharpRepository } log.Id = Guid.NewGuid().ToString(); + log.MessageId = log.MessageId.IfNullOrEmptyAs(Guid.NewGuid().ToString()); var index = GetNextLlmCompletionLogIndex(logDir, log.MessageId); var file = Path.Combine(logDir, $"{log.MessageId}.{index}.log"); File.WriteAllText(file, JsonSerializer.Serialize(log, _options)); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index 89c583c5..a523e8c6 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -4,8 +4,10 @@ using BotSharp.Abstraction.Functions.Models; using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Routing.Models; using BotSharp.Abstraction.Users.Models; +using BotSharp.Abstraction.Utilities; using BotSharp.Plugin.MongoStorage.Collections; using BotSharp.Plugin.MongoStorage.Models; +using System.Reflection.Emit; namespace BotSharp.Plugin.MongoStorage.Repository; @@ -892,13 +894,13 @@ public class MongoRepository : IBotSharpRepository #region LLM Completion Log public void SaveLlmCompletionLog(LlmCompletionLog log) { - if (log == null || string.IsNullOrEmpty(log.ConversationId)) return; + if (log == null) return; var completiongLog = new LlmCompletionLogDocument { - Id = string.IsNullOrEmpty(log.Id) ? Guid.NewGuid().ToString() : log.Id, - ConversationId = log.ConversationId, - MessageId = log.MessageId, + Id = log.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString()), + ConversationId = log.ConversationId.IfNullOrEmptyAs(Guid.Empty.ToString()), + MessageId = log.MessageId.IfNullOrEmptyAs(Guid.NewGuid().ToString()), AgentId = log.AgentId, Prompt = log.Prompt, Response = log.Response,