Skip to content

Commit d8cd0d7

Browse files
authored
Merge branch 'SciSharp:master' into master
2 parents a09d47a + e2ea78b commit d8cd0d7

File tree

12 files changed

+238
-41
lines changed

12 files changed

+238
-41
lines changed

src/Infrastructure/BotSharp.Abstraction/Files/Models/KnowledgeFileModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public class KnowledgeFileModel
55
public Guid FileId { get; set; }
66
public string FileName { get; set; }
77
public string FileExtension { get; set; }
8+
public string FileSource { get; set; }
89
public string ContentType { get; set; }
910
public string FileUrl { get; set; }
1011
public DocMetaRefData? RefData { get; set; }

src/Infrastructure/BotSharp.Abstraction/Knowledges/Enums/KnowledgePayloadName.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ public static class KnowledgePayloadName
1111
public static string FileId = "fileId";
1212
public static string FileName = "fileName";
1313
public static string FileSource = "fileSource";
14+
public static string FileUrl = "fileUrl";
1415
}

src/Plugins/BotSharp.Plugin.KnowledgeBase/Helpers/TextChopper.cs renamed to src/Infrastructure/BotSharp.Abstraction/Knowledges/Helpers/TextChopper.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Text.RegularExpressions;
22

3-
namespace BotSharp.Plugin.KnowledgeBase.Helpers;
3+
namespace BotSharp.Abstraction.Knowledges.Helpers;
44

55
public static class TextChopper
66
{
@@ -14,18 +14,22 @@ public static List<string> Chop(string content, ChunkOption option)
1414
private static List<string> ChopByWord(string content, ChunkOption option)
1515
{
1616
var chunks = new List<string>();
17-
var words = content.Split(' ').Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
17+
var words = content.Split(' ', StringSplitOptions.RemoveEmptyEntries).Where(x => !string.IsNullOrWhiteSpace(x)).ToList();
1818

1919
var chunk = string.Empty;
2020
for (int i = 0; i < words.Count; i++)
2121
{
22-
chunk += words[i] + " ";
22+
chunk += words[i];
2323
if (chunk.Length > option.Size)
2424
{
2525
chunks.Add(chunk.Trim());
2626
chunk = string.Empty;
2727
i -= option.Conjunction;
2828
}
29+
else
30+
{
31+
chunk += " ";
32+
}
2933
}
3034

3135
if (chunks.IsNullOrEmpty() && !string.IsNullOrEmpty(chunk))

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,37 @@ public interface IKnowledgeService
2222
#endregion
2323

2424
#region Document
25-
Task<UploadKnowledgeResponse> UploadKnowledgeDocuments(string collectionName, IEnumerable<ExternalFileModel> files);
25+
/// <summary>
26+
/// Save documents and their contents to knowledgebase
27+
/// </summary>
28+
/// <param name="collectionName"></param>
29+
/// <param name="files"></param>
30+
/// <returns></returns>
31+
Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName, IEnumerable<ExternalFileModel> files);
32+
/// <summary>
33+
/// Save document content to knowledgebase without saving the document
34+
/// </summary>
35+
/// <param name="collectionName"></param>
36+
/// <param name="fileName"></param>
37+
/// <param name="fileSource"></param>
38+
/// <param name="contents"></param>
39+
/// <param name="refData"></param>
40+
/// <returns></returns>
41+
Task<bool> ImportDocumentContentToKnowledge(string collectionName, string fileName, string fileSource, IEnumerable<string> contents, DocMetaRefData? refData = null);
42+
/// <summary>
43+
/// Delete one document and its related knowledge in the collection
44+
/// </summary>
45+
/// <param name="collectionName"></param>
46+
/// <param name="fileId"></param>
47+
/// <returns></returns>
2648
Task<bool> DeleteKnowledgeDocument(string collectionName, Guid fileId);
49+
/// <summary>
50+
/// Delete all documents and their related knowledge in the collection
51+
/// </summary>
52+
/// <param name="collectionName"></param>
53+
/// <param name="filter"></param>
54+
/// <returns></returns>
55+
Task<bool> DeleteKnowledgeDocuments(string collectionName, KnowledgeFileFilter filter);
2756
Task<PagedItems<KnowledgeFileModel>> GetPagedKnowledgeDocuments(string collectionName, KnowledgeFileFilter filter);
2857
Task<FileBinaryDataModel> GetKnowledgeDocumentBinaryData(string collectionName, Guid fileId);
2958
#endregion

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ public class DocMetaRefData
4242
[JsonPropertyName("name")]
4343
public string Name { get; set; }
4444

45+
[JsonPropertyName("type")]
46+
public string Type { get; set; }
47+
4548
[JsonPropertyName("url")]
4649
public string Url { get; set; }
4750

48-
[JsonPropertyName("json_content")]
51+
[JsonPropertyName("data")]
4952
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
50-
public string? JsonContent { get; set; }
53+
public IDictionary<string, string>? Data { get; set; }
5154
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,15 @@ namespace BotSharp.Abstraction.Knowledges.Models;
33
public class KnowledgeFileFilter : Pagination
44
{
55
public IEnumerable<Guid>? FileIds { get; set; }
6+
7+
public IEnumerable<string>? FileNames { get; set; }
8+
9+
public IEnumerable<string>? ContentTypes { get; set; }
10+
11+
public IEnumerable<string>? FileSources { get; set; }
12+
13+
public KnowledgeFileFilter()
14+
{
15+
16+
}
617
}

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,29 @@ public PagedItems<KnowledgeDocMetaData> GetKnowledgeBaseFileMeta(string collecti
182182
var matched = true;
183183

184184
// Apply filter
185-
if (filter != null && !filter.FileIds.IsNullOrEmpty())
185+
if (filter != null)
186186
{
187-
matched = matched && filter.FileIds.Contains(metaData.FileId);
187+
if (!filter.FileIds.IsNullOrEmpty())
188+
{
189+
matched = matched && filter.FileIds.Contains(metaData.FileId);
190+
}
191+
192+
if (!filter.FileNames.IsNullOrEmpty())
193+
{
194+
matched = matched && filter.FileNames.Contains(metaData.FileName);
195+
}
196+
197+
if (!filter.FileSources.IsNullOrEmpty())
198+
{
199+
matched = matched & filter.FileSources.Contains(metaData.FileSource);
200+
}
201+
202+
if (!filter.ContentTypes.IsNullOrEmpty())
203+
{
204+
matched = matched && filter.ContentTypes.Contains(metaData.ContentType);
205+
}
188206
}
207+
189208

190209
if (!matched) continue;
191210

src/Infrastructure/BotSharp.OpenAPI/Controllers/KnowledgeBaseController.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public async Task<bool> DeleteVectorCollectionAllData([FromRoute] string collect
115115
[HttpPost("/knowledge/document/{collection}/upload")]
116116
public async Task<UploadKnowledgeResponse> UploadKnowledgeDocuments([FromRoute] string collection, [FromBody] VectorKnowledgeUploadRequest request)
117117
{
118-
var response = await _knowledgeService.UploadKnowledgeDocuments(collection, request.Files);
118+
var response = await _knowledgeService.UploadDocumentsToKnowledge(collection, request.Files);
119119
return response;
120120
}
121121

@@ -138,7 +138,7 @@ public async Task<UploadKnowledgeResponse> UploadKnowledgeDocuments([FromRoute]
138138
});
139139
}
140140

141-
var response = await _knowledgeService.UploadKnowledgeDocuments(collection, docs);
141+
var response = await _knowledgeService.UploadDocumentsToKnowledge(collection, docs);
142142
return response;
143143
}
144144

@@ -149,14 +149,17 @@ public async Task<bool> DeleteKnowledgeDocument([FromRoute] string collection, [
149149
return response;
150150
}
151151

152-
[HttpPost("/knowledge/document/{collection}/list")]
152+
[HttpDelete("/knowledge/document/{collection}/delete")]
153+
public async Task<bool> DeleteKnowledgeDocuments([FromRoute] string collection, [FromBody] GetKnowledgeDocsRequest request)
154+
{
155+
var response = await _knowledgeService.DeleteKnowledgeDocuments(collection, request);
156+
return response;
157+
}
158+
159+
[HttpPost("/knowledge/document/{collection}/page")]
153160
public async Task<PagedItems<KnowledgeFileViewModel>> GetPagedKnowledgeDocuments([FromRoute] string collection, [FromBody] GetKnowledgeDocsRequest request)
154161
{
155-
var data = await _knowledgeService.GetPagedKnowledgeDocuments(collection, new KnowledgeFileFilter
156-
{
157-
Page = request.Page,
158-
Size = request.Size
159-
});
162+
var data = await _knowledgeService.GetPagedKnowledgeDocuments(collection, request);
160163

161164
return new PagedItems<KnowledgeFileViewModel>
162165
{

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Knowledges/KnowledgeFileViewModel.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ public class KnowledgeFileViewModel
1010
[JsonPropertyName("file_name")]
1111
public string FileName { get; set; }
1212

13+
[JsonPropertyName("file_source")]
14+
public string FileSource { get; set; }
15+
1316
[JsonPropertyName("file_extension")]
1417
public string FileExtension { get; set; }
1518

@@ -29,6 +32,7 @@ public static KnowledgeFileViewModel From(KnowledgeFileModel model)
2932
{
3033
FileId = model.FileId,
3134
FileName = model.FileName,
35+
FileSource = model.FileSource,
3236
FileExtension = model.FileExtension,
3337
ContentType = model.ContentType,
3438
FileUrl = model.FileUrl,

0 commit comments

Comments
 (0)