From b8f013aad2850e4217aec67dca6797c3d229770c Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 11 Sep 2024 20:03:54 -0500 Subject: [PATCH 1/2] use binary data --- .../BotSharp.Abstraction/Files/IFileStorageService.cs | 2 +- .../Storage/LocalFileStorageService.KnowledgeBase.cs | 6 +++--- .../Services/KnowledgeService.Document.cs | 7 ++----- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Files/IFileStorageService.cs b/src/Infrastructure/BotSharp.Abstraction/Files/IFileStorageService.cs index d87da6bd4..cea1bcec7 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Files/IFileStorageService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Files/IFileStorageService.cs @@ -63,7 +63,7 @@ public interface IFileStorageService #endregion #region Knowledge - bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, Stream stream); + bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, BinaryData fileData); /// /// Delete files in a knowledge collection. If fileId is null, remove all files in the collection. diff --git a/src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.KnowledgeBase.cs b/src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.KnowledgeBase.cs index 1cb804a70..eb3d0a545 100644 --- a/src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.KnowledgeBase.cs +++ b/src/Infrastructure/BotSharp.Core/Files/Services/Storage/LocalFileStorageService.KnowledgeBase.cs @@ -5,7 +5,7 @@ namespace BotSharp.Core.Files.Services; public partial class LocalFileStorageService { - public bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, Stream stream) + public bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, BinaryData fileData) { if (string.IsNullOrWhiteSpace(collectionName) || string.IsNullOrWhiteSpace(vectorStoreProvider) @@ -26,9 +26,9 @@ public bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvi var filePath = Path.Combine(dir, fileName); using var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write); - stream.CopyTo(fs); + using var ds = fileData.ToStream(); + ds.CopyTo(fs); fs.Flush(); - fs.Close(); return true; } catch (Exception ex) diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs index 2e614a684..c8e374f31 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs @@ -208,11 +208,8 @@ private async Task> GetFileContent(string contentType, byte[ private bool SaveDocument(string collectionName, string vectorStoreProvider, string fileId, string fileName, byte[] bytes) { var fileStoreage = _services.GetRequiredService(); - using var stream = new MemoryStream(bytes); - stream.Position = 0; - - var saved = fileStoreage.SaveKnowledgeBaseFile(collectionName.CleanStr(), vectorStoreProvider.CleanStr(), fileId, fileName, stream); - stream.Close(); + var data = BinaryData.FromBytes(bytes); + var saved = fileStoreage.SaveKnowledgeBaseFile(collectionName.CleanStr(), vectorStoreProvider.CleanStr(), fileId, fileName, data); return saved; } From af6804a6ad89f8eb472aeacc6e8bb1ca1df710b5 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 11 Sep 2024 20:06:07 -0500 Subject: [PATCH 2/2] sync change --- .../Services/TencentCosService.KnowledgeBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/BotSharp.Plugin.TencentCos/Services/TencentCosService.KnowledgeBase.cs b/src/Plugins/BotSharp.Plugin.TencentCos/Services/TencentCosService.KnowledgeBase.cs index 9d521142f..fecf89423 100644 --- a/src/Plugins/BotSharp.Plugin.TencentCos/Services/TencentCosService.KnowledgeBase.cs +++ b/src/Plugins/BotSharp.Plugin.TencentCos/Services/TencentCosService.KnowledgeBase.cs @@ -4,7 +4,7 @@ namespace BotSharp.Plugin.TencentCos.Services; public partial class TencentCosService { - public bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, Stream stream) + public bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, BinaryData fileData) { throw new NotImplementedException(); }