From 48be12a92085b40c4b5253c99ccab05a6db98301 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sat, 31 Aug 2024 01:05:53 -0500 Subject: [PATCH] relocate text chopper --- .../BotSharp.Abstraction/Knowledges/ITextChopper.cs | 11 ----------- .../Knowledges/Helpers}/TextChopperService.cs | 11 ++++++----- .../Knowledges/Services/KnowledgeService.Create.cs | 5 ++--- .../KnowledgeBasePlugin.cs | 1 - 4 files changed, 8 insertions(+), 20 deletions(-) delete mode 100644 src/Infrastructure/BotSharp.Abstraction/Knowledges/ITextChopper.cs rename src/{Plugins/BotSharp.Plugin.KnowledgeBase/Services => Infrastructure/BotSharp.Core/Knowledges/Helpers}/TextChopperService.cs (78%) diff --git a/src/Infrastructure/BotSharp.Abstraction/Knowledges/ITextChopper.cs b/src/Infrastructure/BotSharp.Abstraction/Knowledges/ITextChopper.cs deleted file mode 100644 index 7d06a8a4..00000000 --- a/src/Infrastructure/BotSharp.Abstraction/Knowledges/ITextChopper.cs +++ /dev/null @@ -1,11 +0,0 @@ -using BotSharp.Abstraction.Knowledges.Models; - -namespace BotSharp.Abstraction.Knowledges; - -/// -/// Chop large content into chunks -/// -public interface ITextChopper -{ - List Chop(string content, ChunkOption option); -} diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/TextChopperService.cs b/src/Infrastructure/BotSharp.Core/Knowledges/Helpers/TextChopperService.cs similarity index 78% rename from src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/TextChopperService.cs rename to src/Infrastructure/BotSharp.Core/Knowledges/Helpers/TextChopperService.cs index 88c77641..68b58539 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/TextChopperService.cs +++ b/src/Infrastructure/BotSharp.Core/Knowledges/Helpers/TextChopperService.cs @@ -1,17 +1,18 @@ +using BotSharp.Abstraction.Knowledges.Models; using System.Text.RegularExpressions; -namespace BotSharp.Plugin.KnowledgeBase.Services; +namespace BotSharp.Core.Knowledges.Helpers; -public class TextChopperService : ITextChopper +public static class TextChopper { - public List Chop(string content, ChunkOption option) + public static List Chop(string content, ChunkOption option) { content = Regex.Replace(content, @"\.{2,}", " "); content = Regex.Replace(content, @"_{2,}", " "); return option.SplitByWord ? ChopByWord(content, option) : ChopByChar(content, option); } - private List ChopByWord(string content, ChunkOption option) + private static List ChopByWord(string content, ChunkOption option) { var chunks = new List(); @@ -34,7 +35,7 @@ public class TextChopperService : ITextChopper return chunks; } - private List ChopByChar(string content, ChunkOption option) + private static List ChopByChar(string content, ChunkOption option) { var chunks = new List(); var currentPos = 0; diff --git a/src/Infrastructure/BotSharp.Core/Knowledges/Services/KnowledgeService.Create.cs b/src/Infrastructure/BotSharp.Core/Knowledges/Services/KnowledgeService.Create.cs index 2927d952..d8b82fb5 100644 --- a/src/Infrastructure/BotSharp.Core/Knowledges/Services/KnowledgeService.Create.cs +++ b/src/Infrastructure/BotSharp.Core/Knowledges/Services/KnowledgeService.Create.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Knowledges.Models; using BotSharp.Abstraction.VectorStorage.Models; +using BotSharp.Core.Knowledges.Helpers; namespace BotSharp.Core.Knowledges.Services; @@ -8,9 +9,7 @@ public partial class KnowledgeService public async Task FeedVectorKnowledge(string collectionName, KnowledgeCreationModel knowledge) { var index = 0; - - var textChopper = _services.GetRequiredService(); - var lines = textChopper.Chop(knowledge.Content, new ChunkOption + var lines = TextChopper.Chop(knowledge.Content, new ChunkOption { Size = 1024, Conjunction = 32, diff --git a/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs b/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs index dd864a73..9a955a5b 100644 --- a/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs +++ b/src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs @@ -20,7 +20,6 @@ public class KnowledgeBasePlugin : IBotSharpPlugin return settingService.Bind("KnowledgeBase"); }); - services.AddScoped(); services.AddSingleton(); services.AddScoped(); services.AddScoped();