Skip to content

Commit 2619472

Browse files
authored
Merge pull request #750 from iceljc/master
add similar search
2 parents 5f82152 + b548cfe commit 2619472

File tree

11 files changed

+50
-18
lines changed

11 files changed

+50
-18
lines changed

src/Infrastructure/BotSharp.Abstraction/Knowledges/IKnowledgeService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public interface IKnowledgeService
3030
/// <param name="collectionName"></param>
3131
/// <param name="files"></param>
3232
/// <returns></returns>
33-
Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName, IEnumerable<ExternalFileModel> files);
33+
Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName, IEnumerable<ExternalFileModel> files, ChunkOption? option = null);
3434
/// <summary>
3535
/// Save document content to knowledgebase without saving the document
3636
/// </summary>

src/Infrastructure/BotSharp.Abstraction/Knowledges/Models/ChunkOption.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@ public class ChunkOption
1313
public int Conjunction { get; set; }
1414

1515
public bool SplitByWord { get; set; }
16+
17+
18+
public static ChunkOption Default()
19+
{
20+
return new ChunkOption
21+
{
22+
Size = 1024,
23+
Conjunction = 12,
24+
SplitByWord = true,
25+
};
26+
}
1627
}

src/Infrastructure/BotSharp.Abstraction/Plugins/Models/PluginFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ public class PluginFilter
44
{
55
public Pagination Pager { get; set; } = new Pagination();
66
public IEnumerable<string>? Names { get; set; }
7+
public string? SimilarName { get; set; }
78
}
89
}

src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/AgentFilter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public class AgentFilter
44
{
55
public Pagination Pager { get; set; } = new Pagination();
66
public string? AgentName { get; set; }
7+
public string? SimilarName { get; set; }
78
public bool? Disabled { get; set; }
89
public bool? Installed { get; set; }
910
public string? Type { get; set; }

src/Infrastructure/BotSharp.Core.SideCar/BotSharpSideCarPlugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace BotSharp.Core.SideCar;
88
public class BotSharpSideCarPlugin : IBotSharpPlugin
99
{
1010
public string Id => "06e5a276-bba0-45af-9625-889267c341c9";
11-
public string Name => "Side car";
11+
public string Name => "Side Car";
1212
public string Description => "Provides side car for calling agent cluster in conversation";
1313

1414
public SettingsMeta Settings => new SettingsMeta("SideCar");

src/Infrastructure/BotSharp.Core/Plugins/PluginLoader.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Drawing;
55
using System.IO;
66
using System.Reflection;
7+
using System.Text.RegularExpressions;
78
using System.Xml;
89

910
namespace BotSharp.Core.Plugins;
@@ -132,6 +133,12 @@ public PagedItems<PluginDef> GetPagedPlugins(IServiceProvider services, PluginFi
132133
plugins = plugins.Where(x => filter.Names.Any(n => x.Name.IsEqualTo(n))).ToList();
133134
}
134135

136+
if (!string.IsNullOrEmpty(filter.SimilarName))
137+
{
138+
var regex = new Regex(filter.SimilarName, RegexOptions.Compiled | RegexOptions.IgnoreCase);
139+
plugins = plugins.Where(x => regex.IsMatch(x.Name)).ToList();
140+
}
141+
135142
return new PagedItems<PluginDef>
136143
{
137144
Items = plugins.Skip(pager.Offset).Take(pager.Size),

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using BotSharp.Abstraction.Routing.Models;
22
using System.IO;
3+
using System.Text.RegularExpressions;
34

45
namespace BotSharp.Core.Repository
56
{
@@ -62,6 +63,8 @@ public void UpdateAgent(Agent agent, AgentField field)
6263
default:
6364
break;
6465
}
66+
67+
_agents = [];
6568
}
6669

6770
#region Update Agent Fields
@@ -366,6 +369,12 @@ public List<Agent> GetAgents(AgentFilter filter)
366369
query = query.Where(x => x.Name.ToLower() == filter.AgentName.ToLower());
367370
}
368371

372+
if (!string.IsNullOrEmpty(filter.SimilarName))
373+
{
374+
var regex = new Regex(filter.SimilarName, RegexOptions.Compiled | RegexOptions.IgnoreCase);
375+
query = query.Where(x => regex.IsMatch(x.Name));
376+
}
377+
369378
if (filter.Disabled.HasValue)
370379
{
371380
query = query.Where(x => x.Disabled == filter.Disabled);
@@ -478,7 +487,8 @@ public void BulkInsertAgents(List<Agent> agents)
478487
File.WriteAllText(instFile, agent.Instruction);
479488
}
480489
}
481-
Reset();
490+
491+
ResetLocalAgents();
482492
}
483493

484494
public void BulkInsertUserAgents(List<UserAgent> userAgents)
@@ -511,7 +521,7 @@ public void BulkInsertUserAgents(List<UserAgent> userAgents)
511521
Thread.Sleep(50);
512522
}
513523

514-
Reset();
524+
ResetLocalAgents();
515525
}
516526

517527
public bool DeleteAgents()
@@ -566,7 +576,7 @@ public bool DeleteAgent(string agentId)
566576

567577
// Delete agent folder
568578
Directory.Delete(agentDir, true);
569-
Reset();
579+
ResetLocalAgents();
570580
return true;
571581
}
572582
catch
@@ -575,7 +585,7 @@ public bool DeleteAgent(string agentId)
575585
}
576586
}
577587

578-
private void Reset()
588+
private void ResetLocalAgents()
579589
{
580590
_agents = [];
581591
_userAgents = [];

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public bool DeleteConversations(IEnumerable<string> conversationIds)
5454

5555
Directory.Delete(convDir, true);
5656
}
57+
5758
return true;
5859
}
5960

src/Plugins/BotSharp.Plugin.KnowledgeBase/Services/KnowledgeService.Document.cs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace BotSharp.Plugin.KnowledgeBase.Services;
1010

1111
public partial class KnowledgeService
1212
{
13-
public async Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName, IEnumerable<ExternalFileModel> files)
13+
public async Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName,
14+
IEnumerable<ExternalFileModel> files, ChunkOption? option = null)
1415
{
1516
var res = new UploadKnowledgeResponse
1617
{
@@ -48,7 +49,7 @@ public async Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string col
4849
{
4950
// Get document info
5051
var (contentType, bytes) = await GetFileInfo(file);
51-
var contents = await GetFileContent(contentType, bytes);
52+
var contents = await GetFileContent(contentType, bytes, option ?? ChunkOption.Default());
5253

5354
// Save document
5455
var fileId = Guid.NewGuid();
@@ -369,13 +370,13 @@ public async Task<FileBinaryDataModel> GetKnowledgeDocumentBinaryData(string col
369370
}
370371

371372
#region Read doc content
372-
private async Task<IEnumerable<string>> GetFileContent(string contentType, byte[] bytes)
373+
private async Task<IEnumerable<string>> GetFileContent(string contentType, byte[] bytes, ChunkOption option)
373374
{
374375
IEnumerable<string> results = new List<string>();
375376

376377
if (contentType.IsEqualTo(MediaTypeNames.Text.Plain))
377378
{
378-
results = await ReadTxt(bytes);
379+
results = await ReadTxt(bytes, option);
379380
}
380381
else if (contentType.IsEqualTo(MediaTypeNames.Application.Pdf))
381382
{
@@ -385,20 +386,15 @@ private async Task<IEnumerable<string>> GetFileContent(string contentType, byte[
385386
return results;
386387
}
387388

388-
private async Task<IEnumerable<string>> ReadTxt(byte[] bytes)
389+
private async Task<IEnumerable<string>> ReadTxt(byte[] bytes, ChunkOption option)
389390
{
390391
using var stream = new MemoryStream(bytes);
391392
using var reader = new StreamReader(stream);
392393
var content = await reader.ReadToEndAsync();
393394
reader.Close();
394395
stream.Close();
395396

396-
var lines = TextChopper.Chop(content, new ChunkOption
397-
{
398-
Size = 1024,
399-
Conjunction = 12,
400-
SplitByWord = true,
401-
});
397+
var lines = TextChopper.Chop(content, option);
402398
return lines;
403399
}
404400

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,11 @@ public List<Agent> GetAgents(AgentFilter filter)
298298
filters.Add(builder.Eq(x => x.Name, filter.AgentName));
299299
}
300300

301+
if (!string.IsNullOrEmpty(filter.SimilarName))
302+
{
303+
filters.Add(builder.Regex(x => x.Name, new BsonRegularExpression(filter.SimilarName, "i")));
304+
}
305+
301306
if (filter.Disabled.HasValue)
302307
{
303308
filters.Add(builder.Eq(x => x.Disabled, filter.Disabled.Value));

0 commit comments

Comments
 (0)