Skip to content

Commit 7646030

Browse files
author
Jicheng Lu
committed
add vector storage snapshots
1 parent 0cd2863 commit 7646030

File tree

62 files changed

+403
-27
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+403
-27
lines changed

src/Infrastructure/BotSharp.Abstraction/Files/Utilities/FileUtility.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ public static string BuildFileDataFromFile(IFormFile file)
4747
return $"data:{contentType};base64,{base64}";
4848
}
4949

50+
public static BinaryData BuildBinaryDataFromFile(IFormFile file)
51+
{
52+
using var stream = new MemoryStream();
53+
file.CopyTo(stream);
54+
stream.Position = 0;
55+
var binary = BinaryData.FromStream(stream);
56+
stream.Close();
57+
58+
return binary;
59+
}
60+
5061
public static string GetFileContentType(string fileName)
5162
{
5263
string contentType;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ Task<bool> ImportDocumentContentToKnowledge(string collectionName, string fileNa
6060
Task<FileBinaryDataModel> GetKnowledgeDocumentBinaryData(string collectionName, Guid fileId);
6161
#endregion
6262

63+
#region Snapshot
64+
Task<IEnumerable<VectorCollectionSnapshot>> GetVectorCollectionSnapshots(string collectionName);
65+
Task<VectorCollectionSnapshot?> CreateVectorCollectionSnapshot(string collectionName);
66+
Task<BinaryData> DownloadVectorCollectionSnapshot(string collectionName, string snapshotFileName);
67+
Task<bool> RecoverVectorCollectionFromSnapshot(string collectionName, string snapshotFileName, BinaryData snapshotData);
68+
Task<bool> DeleteVectorCollectionSnapshot(string collectionName, string snapshotName);
69+
#endregion
70+
6371
#region Common
6472
Task<bool> RefreshVectorKnowledgeConfigs(VectorCollectionConfigsModel configs);
6573
#endregion

src/Infrastructure/BotSharp.Abstraction/VectorStorage/IVectorDb.cs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,36 @@ public interface IVectorDb
66
{
77
string Provider { get; }
88

9-
Task<bool> DoesCollectionExist(string collectionName);
10-
Task<IEnumerable<string>> GetCollections();
11-
Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionData(string collectionName, VectorFilter filter);
12-
Task<IEnumerable<VectorCollectionData>> GetCollectionData(string collectionName, IEnumerable<Guid> ids, bool withPayload = false, bool withVector = false);
13-
Task<bool> CreateCollection(string collectionName, int dimension);
14-
Task<bool> DeleteCollection(string collectionName);
15-
Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, object>? payload = null);
16-
Task<IEnumerable<VectorCollectionData>> Search(string collectionName, float[] vector, IEnumerable<string>? fields, int limit = 5, float confidence = 0.5f, bool withVector = false);
17-
Task<bool> DeleteCollectionData(string collectionName, List<Guid> ids);
18-
Task<bool> DeleteCollectionAllData(string collectionName);
9+
Task<bool> DoesCollectionExist(string collectionName)
10+
=> throw new NotImplementedException();
11+
Task<IEnumerable<string>> GetCollections()
12+
=> throw new NotImplementedException();
13+
Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionData(string collectionName, VectorFilter filter)
14+
=> throw new NotImplementedException();
15+
Task<IEnumerable<VectorCollectionData>> GetCollectionData(string collectionName, IEnumerable<Guid> ids,
16+
bool withPayload = false, bool withVector = false)
17+
=> throw new NotImplementedException();
18+
Task<bool> CreateCollection(string collectionName, int dimension)
19+
=> throw new NotImplementedException();
20+
Task<bool> DeleteCollection(string collectionName)
21+
=> throw new NotImplementedException();
22+
Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, object>? payload = null)
23+
=> throw new NotImplementedException();
24+
Task<IEnumerable<VectorCollectionData>> Search(string collectionName, float[] vector, IEnumerable<string>? fields,
25+
int limit = 5, float confidence = 0.5f, bool withVector = false)
26+
=> throw new NotImplementedException();
27+
Task<bool> DeleteCollectionData(string collectionName, List<Guid> ids)
28+
=> throw new NotImplementedException();
29+
Task<bool> DeleteCollectionAllData(string collectionName)
30+
=> throw new NotImplementedException();
31+
Task<IEnumerable<VectorCollectionSnapshot>> GetCollectionSnapshots(string collectionName)
32+
=> throw new NotImplementedException();
33+
Task<VectorCollectionSnapshot?> CreateCollectionShapshot(string collectionName)
34+
=> throw new NotImplementedException();
35+
Task<BinaryData> DownloadCollectionSnapshot(string collectionName, string snapshotFileName)
36+
=> throw new NotImplementedException();
37+
Task<bool> RecoverCollectionFromShapshot(string collectionName, string snapshotFileName, BinaryData snapshotData)
38+
=> throw new NotImplementedException();
39+
Task<bool> DeleteCollectionShapshot(string collectionName, string snapshotName)
40+
=> throw new NotImplementedException();
1941
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace BotSharp.Abstraction.VectorStorage.Models;
2+
3+
public class VectorCollectionSnapshot
4+
{
5+
public string Name { get; set; } = default!;
6+
public long Size { get; set; }
7+
public DateTime CreatedTime { get; set; }
8+
public string? CheckSum { get; set; }
9+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ public DashboardController(IServiceProvider services,
1919
}
2020
#region User Components
2121
[HttpGet("/dashboard/components")]
22-
public async Task<UserDashboardModel> GetComponents()
22+
public async Task<UserDashboardViewModel> GetComponents()
2323
{
2424
var userService = _services.GetRequiredService<IUserService>();
2525
var dashboardProfile = await userService.GetDashboard();
26-
if (dashboardProfile == null) return new UserDashboardModel();
26+
if (dashboardProfile == null) return new();
2727

28-
var result = new UserDashboardModel
28+
var result = new UserDashboardViewModel
2929
{
3030
ConversationList = dashboardProfile.ConversationList.Select(
31-
x => new UserDashboardConversationModel
31+
x => new UserDashboardConversationViewModel
3232
{
3333
Name = x.Name,
3434
ConversationId = x.ConversationId,
@@ -40,7 +40,7 @@ public async Task<UserDashboardModel> GetComponents()
4040
}
4141

4242
[HttpPost("/dashboard/component/conversation")]
43-
public async Task UpdateDashboardConversationInstruction(UserDashboardConversationModel dashConv)
43+
public async Task UpdateDashboardConversationInstruction(UserDashboardConversationViewModel dashConv)
4444
{
4545
if (string.IsNullOrEmpty(dashConv.Name) && string.IsNullOrEmpty(dashConv.Instruction))
4646
{

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

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ public class KnowledgeBaseController : ControllerBase
1212
private readonly IKnowledgeService _knowledgeService;
1313
private readonly IServiceProvider _services;
1414

15-
public KnowledgeBaseController(IKnowledgeService knowledgeService, IServiceProvider services)
15+
public KnowledgeBaseController(
16+
IKnowledgeService knowledgeService,
17+
IServiceProvider services)
1618
{
1719
_knowledgeService = knowledgeService;
1820
_services = services;
@@ -117,6 +119,46 @@ public async Task<bool> DeleteVectorCollectionAllData([FromRoute] string collect
117119
#endregion
118120

119121

122+
#region Snapshot
123+
[HttpGet("/knowledge/vector/{collection}/snapshots")]
124+
public async Task<IEnumerable<VectorCollectionSnapshotViewModel>> GetVectorCollectionSnapshots([FromRoute] string collection)
125+
{
126+
var snapshots = await _knowledgeService.GetVectorCollectionSnapshots(collection);
127+
return snapshots.Select(x => VectorCollectionSnapshotViewModel.From(x));
128+
}
129+
130+
[HttpPost("/knowledge/vector/{collection}/snapshot")]
131+
public async Task<VectorCollectionSnapshotViewModel?> CreateVectorCollectionSnapshot([FromRoute] string collection)
132+
{
133+
var snapshot = await _knowledgeService.CreateVectorCollectionSnapshot(collection);
134+
return VectorCollectionSnapshotViewModel.From(snapshot);
135+
}
136+
137+
[HttpGet("/knowledge/vector/{collection}/snapshot")]
138+
public async Task<IActionResult> GetVectorCollectionSnapshot([FromRoute] string collection, [FromQuery] string snapshotFileName)
139+
{
140+
var snapshot = await _knowledgeService.DownloadVectorCollectionSnapshot(collection, snapshotFileName);
141+
return BuildFileResult(snapshotFileName, snapshot);
142+
}
143+
144+
[HttpPost("/knowledge/vector/{collection}/snapshot/recover")]
145+
public async Task<bool> RecoverVectorCollectionFromSnapshot([FromRoute] string collection, IFormFile snapshotFile)
146+
{
147+
var fileName = snapshotFile.FileName;
148+
var binary = FileUtility.BuildBinaryDataFromFile(snapshotFile);
149+
var done = await _knowledgeService.RecoverVectorCollectionFromSnapshot(collection, fileName, binary);
150+
return done;
151+
}
152+
153+
[HttpDelete("/knowledge/vector/{collection}/snapshot")]
154+
public async Task<bool> DeleteVectorCollectionSnapshots([FromRoute] string collection, [FromBody] DeleteVectorCollectionSnapshotRequest request)
155+
{
156+
var done = await _knowledgeService.DeleteVectorCollectionSnapshot(collection, request.SnapshotName);
157+
return done;
158+
}
159+
#endregion
160+
161+
120162
#region Document
121163
[HttpPost("/knowledge/document/{collection}/upload")]
122164
public async Task<UploadKnowledgeResponse> UploadKnowledgeDocuments([FromRoute] string collection, [FromBody] VectorKnowledgeUploadRequest request)
@@ -187,7 +229,6 @@ public async Task<IActionResult> GetKnowledgeDocument([FromRoute] string collect
187229
#endregion
188230

189231

190-
191232
#region Graph
192233
[HttpPost("/knowledge/graph/search")]
193234
public async Task<GraphKnowledgeViewModel> SearchGraphKnowledge([FromBody] SearchGraphKnowledgeRequest request)
@@ -214,4 +255,13 @@ public async Task<string> RefreshVectorCollectionConfigs([FromBody] VectorCollec
214255
return saved ? "Success" : "Fail";
215256
}
216257
#endregion
258+
259+
#region Private methods
260+
private FileStreamResult BuildFileResult(string fileName, BinaryData fileData)
261+
{
262+
var stream = fileData.ToStream();
263+
stream.Position = 0;
264+
return File(stream, "application/octet-stream", Path.GetFileName(fileName));
265+
}
266+
#endregion
217267
}

0 commit comments

Comments
 (0)