Merge pull request #750 from iceljc/master

add similar search
This commit is contained in:
iceljc 2024-11-15 10:36:57 -06:00 committed by GitHub
commit 2619472a5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 50 additions and 18 deletions

View file

@ -30,7 +30,7 @@ public interface IKnowledgeService
/// <param name="collectionName"></param>
/// <param name="files"></param>
/// <returns></returns>
Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName, IEnumerable<ExternalFileModel> files);
Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName, IEnumerable<ExternalFileModel> files, ChunkOption? option = null);
/// <summary>
/// Save document content to knowledgebase without saving the document
/// </summary>

View file

@ -13,4 +13,15 @@ public class ChunkOption
public int Conjunction { get; set; }
public bool SplitByWord { get; set; }
public static ChunkOption Default()
{
return new ChunkOption
{
Size = 1024,
Conjunction = 12,
SplitByWord = true,
};
}
}

View file

@ -4,5 +4,6 @@ namespace BotSharp.Abstraction.Plugins.Models
{
public Pagination Pager { get; set; } = new Pagination();
public IEnumerable<string>? Names { get; set; }
public string? SimilarName { get; set; }
}
}

View file

@ -4,6 +4,7 @@ public class AgentFilter
{
public Pagination Pager { get; set; } = new Pagination();
public string? AgentName { get; set; }
public string? SimilarName { get; set; }
public bool? Disabled { get; set; }
public bool? Installed { get; set; }
public string? Type { get; set; }

View file

@ -8,7 +8,7 @@ namespace BotSharp.Core.SideCar;
public class BotSharpSideCarPlugin : IBotSharpPlugin
{
public string Id => "06e5a276-bba0-45af-9625-889267c341c9";
public string Name => "Side car";
public string Name => "Side Car";
public string Description => "Provides side car for calling agent cluster in conversation";
public SettingsMeta Settings => new SettingsMeta("SideCar");

View file

@ -4,6 +4,7 @@ using Microsoft.Extensions.Configuration;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Xml;
namespace BotSharp.Core.Plugins;
@ -132,6 +133,12 @@ public class PluginLoader
plugins = plugins.Where(x => filter.Names.Any(n => x.Name.IsEqualTo(n))).ToList();
}
if (!string.IsNullOrEmpty(filter.SimilarName))
{
var regex = new Regex(filter.SimilarName, RegexOptions.Compiled | RegexOptions.IgnoreCase);
plugins = plugins.Where(x => regex.IsMatch(x.Name)).ToList();
}
return new PagedItems<PluginDef>
{
Items = plugins.Skip(pager.Offset).Take(pager.Size),

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Routing.Models;
using System.IO;
using System.Text.RegularExpressions;
namespace BotSharp.Core.Repository
{
@ -62,6 +63,8 @@ namespace BotSharp.Core.Repository
default:
break;
}
_agents = [];
}
#region Update Agent Fields
@ -366,6 +369,12 @@ namespace BotSharp.Core.Repository
query = query.Where(x => x.Name.ToLower() == filter.AgentName.ToLower());
}
if (!string.IsNullOrEmpty(filter.SimilarName))
{
var regex = new Regex(filter.SimilarName, RegexOptions.Compiled | RegexOptions.IgnoreCase);
query = query.Where(x => regex.IsMatch(x.Name));
}
if (filter.Disabled.HasValue)
{
query = query.Where(x => x.Disabled == filter.Disabled);
@ -478,7 +487,8 @@ namespace BotSharp.Core.Repository
File.WriteAllText(instFile, agent.Instruction);
}
}
Reset();
ResetLocalAgents();
}
public void BulkInsertUserAgents(List<UserAgent> userAgents)
@ -511,7 +521,7 @@ namespace BotSharp.Core.Repository
Thread.Sleep(50);
}
Reset();
ResetLocalAgents();
}
public bool DeleteAgents()
@ -566,7 +576,7 @@ namespace BotSharp.Core.Repository
// Delete agent folder
Directory.Delete(agentDir, true);
Reset();
ResetLocalAgents();
return true;
}
catch
@ -575,7 +585,7 @@ namespace BotSharp.Core.Repository
}
}
private void Reset()
private void ResetLocalAgents()
{
_agents = [];
_userAgents = [];

View file

@ -54,6 +54,7 @@ namespace BotSharp.Core.Repository
Directory.Delete(convDir, true);
}
return true;
}

View file

@ -10,7 +10,8 @@ namespace BotSharp.Plugin.KnowledgeBase.Services;
public partial class KnowledgeService
{
public async Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName, IEnumerable<ExternalFileModel> files)
public async Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName,
IEnumerable<ExternalFileModel> files, ChunkOption? option = null)
{
var res = new UploadKnowledgeResponse
{
@ -48,7 +49,7 @@ public partial class KnowledgeService
{
// Get document info
var (contentType, bytes) = await GetFileInfo(file);
var contents = await GetFileContent(contentType, bytes);
var contents = await GetFileContent(contentType, bytes, option ?? ChunkOption.Default());
// Save document
var fileId = Guid.NewGuid();
@ -369,13 +370,13 @@ public partial class KnowledgeService
}
#region Read doc content
private async Task<IEnumerable<string>> GetFileContent(string contentType, byte[] bytes)
private async Task<IEnumerable<string>> GetFileContent(string contentType, byte[] bytes, ChunkOption option)
{
IEnumerable<string> results = new List<string>();
if (contentType.IsEqualTo(MediaTypeNames.Text.Plain))
{
results = await ReadTxt(bytes);
results = await ReadTxt(bytes, option);
}
else if (contentType.IsEqualTo(MediaTypeNames.Application.Pdf))
{
@ -385,7 +386,7 @@ public partial class KnowledgeService
return results;
}
private async Task<IEnumerable<string>> ReadTxt(byte[] bytes)
private async Task<IEnumerable<string>> ReadTxt(byte[] bytes, ChunkOption option)
{
using var stream = new MemoryStream(bytes);
using var reader = new StreamReader(stream);
@ -393,12 +394,7 @@ public partial class KnowledgeService
reader.Close();
stream.Close();
var lines = TextChopper.Chop(content, new ChunkOption
{
Size = 1024,
Conjunction = 12,
SplitByWord = true,
});
var lines = TextChopper.Chop(content, option);
return lines;
}

View file

@ -298,6 +298,11 @@ public partial class MongoRepository
filters.Add(builder.Eq(x => x.Name, filter.AgentName));
}
if (!string.IsNullOrEmpty(filter.SimilarName))
{
filters.Add(builder.Regex(x => x.Name, new BsonRegularExpression(filter.SimilarName, "i")));
}
if (filter.Disabled.HasValue)
{
filters.Add(builder.Eq(x => x.Disabled, filter.Disabled.Value));

View file

@ -18,7 +18,7 @@ public partial class MongoRepository
public User? GetUserByPhone(string phone)
{
string phoneSecond = string.Empty;
// 如果电话号码长度小于 4直接返回 null
// if phone number length is less than 4, return null
if (phone?.Length < 4)
{
return null;