Skip to content

Commit 89fab12

Browse files
authored
Merge pull request #598 from iceljc/features/refine-vector-db
1. split knowledge menu
2 parents 5029455 + e96b217 commit 89fab12

File tree

10 files changed

+39
-31
lines changed

10 files changed

+39
-31
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public class PluginMenuDef
2525
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
2626
public List<PluginMenuDef>? SubMenu { get; set; }
2727

28-
public PluginMenuDef(string lable, string? link = null, string? icon = null, int weight = 0)
28+
public PluginMenuDef(string label, string? link = null, string? icon = null, int weight = 0)
2929
{
30-
Label = lable;
30+
Label = label;
3131
Link = link;
3232
Icon = icon;
3333
Weight = weight;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IVectorDb
99
Task<IEnumerable<string>> GetCollections();
1010
Task<StringIdPagedItems<VectorCollectionData>> GetCollectionData(string collectionName, VectorFilter filter);
1111
Task CreateCollection(string collectionName, int dim);
12-
Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload = null);
12+
Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload = null);
1313
Task<IEnumerable<VectorCollectionData>> Search(string collectionName, float[] vector, IEnumerable<string>? fields, int limit = 5, float confidence = 0.5f, bool withVector = false);
14-
Task<bool> DeleteCollectionData(string collectionName, string id);
14+
Task<bool> DeleteCollectionData(string collectionName, Guid id);
1515
}

src/Plugins/BotSharp.Plugin.KnowledgeBase/Functions/MemorizeKnowledgeFn.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ public async Task<bool> Execute(RoleDialogModel message)
2929
var collectionName = !string.IsNullOrWhiteSpace(_settings.DefaultCollection) ? _settings.DefaultCollection : KnowledgeCollectionName.BotSharp;
3030
await vectorDb.CreateCollection(collectionName, vector[0].Length);
3131

32-
var id = Guid.NewGuid().ToString();
33-
var result = await vectorDb.Upsert(collectionName, id, vector[0],
34-
args.Question,
32+
var result = await vectorDb.Upsert(collectionName, Guid.NewGuid(), vector[0],
33+
args.Question,
3534
new Dictionary<string, string>
3635
{
3736
{ KnowledgePayloadName.Answer, args.Answer }

src/Plugins/BotSharp.Plugin.KnowledgeBase/KnowledgeBasePlugin.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
2020
return settingService.Bind<KnowledgeBaseSettings>("KnowledgeBase");
2121
});
2222

23-
var a = config["KnowledgeBase"];
24-
2523
services.AddScoped<ITextChopper, TextChopperService>();
2624
services.AddScoped<IKnowledgeService, KnowledgeService>();
2725
services.AddSingleton<IPdf2TextConverter, PigPdf2TextConverter>();
@@ -32,7 +30,14 @@ public void RegisterDI(IServiceCollection services, IConfiguration config)
3230
public bool AttachMenu(List<PluginMenuDef> menu)
3331
{
3432
var section = menu.First(x => x.Label == "Apps");
35-
menu.Add(new PluginMenuDef("Knowledge Base", link: "/page/knowledge-base", icon: "bx bx-book-open", weight: section.Weight + 1));
33+
menu.Add(new PluginMenuDef("Knowledge Base", icon: "bx bx-book-open", weight: section.Weight + 1)
34+
{
35+
SubMenu = new List<PluginMenuDef>
36+
{
37+
new PluginMenuDef("Vector", link: "page/knowledge-base/vector"),
38+
new PluginMenuDef("Graph", link: "page/knowledge-base/graph")
39+
}
40+
});
3641
return true;
3742
}
3843
}

src/Plugins/BotSharp.Plugin.KnowledgeBase/MemVecDb/MemoryVectorDb.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ public async Task<IEnumerable<VectorCollectionData>> Search(string collectionNam
5353
return await Task.FromResult(results);
5454
}
5555

56-
public async Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload = null)
56+
public async Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload = null)
5757
{
5858
_vectors[collectionName].Add(new VecRecord
5959
{
60-
Id = id,
60+
Id = id.ToString(),
6161
Vector = vector,
6262
Text = text
6363
});
6464

6565
return true;
6666
}
6767

68-
public async Task<bool> DeleteCollectionData(string collectionName, string id)
68+
public async Task<bool> DeleteCollectionData(string collectionName, Guid id)
6969
{
7070
return await Task.FromResult(false);
7171
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public async Task FeedVectorKnowledge(string collectionName, KnowledgeCreationMo
1919
foreach (var line in lines)
2020
{
2121
var vec = await textEmbedding.GetVectorAsync(line);
22-
var id = Guid.NewGuid().ToString();
23-
await db.Upsert(collectionName, id, vec, line);
22+
await db.Upsert(collectionName, Guid.NewGuid(), vec, line);
2423
index++;
2524
Console.WriteLine($"Saved vector {index}/{lines.Count}: {line}\n");
2625
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ public async Task<bool> DeleteVectorCollectionData(string collectionName, string
66
{
77
try
88
{
9+
if (!Guid.TryParse(id, out var guid))
10+
{
11+
return false;
12+
}
13+
914
var db = GetVectorDb();
10-
return await db.DeleteCollectionData(collectionName, id);
15+
return await db.DeleteCollectionData(collectionName, guid);
1116
}
1217
catch (Exception ex)
1318
{

src/Plugins/BotSharp.Plugin.MetaAI/Providers/FaissDb.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public Task<IEnumerable<VectorCollectionData>> Search(string collectionName, flo
3232
throw new NotImplementedException();
3333
}
3434

35-
public Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload = null)
35+
public Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload = null)
3636
{
3737
throw new NotImplementedException();
3838
}
3939

40-
public Task<bool> DeleteCollectionData(string collectionName, string id)
40+
public Task<bool> DeleteCollectionData(string collectionName, Guid id)
4141
{
4242
throw new NotImplementedException();
4343
}

src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ public async Task CreateCollection(string collectionName, int dim)
9292
}
9393
}
9494

95-
public async Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload = null)
95+
public async Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload = null)
9696
{
9797
// Insert vectors
9898
var point = new PointStruct()
9999
{
100100
Id = new PointId()
101101
{
102-
Uuid = id
102+
Uuid = id.ToString()
103103
},
104104
Vectors = vector,
105105
Payload =
@@ -137,7 +137,11 @@ public async Task<IEnumerable<VectorCollectionData>> Search(string collectionNam
137137
return results;
138138
}
139139

140-
var points = await client.SearchAsync(collectionName, vector, limit: (ulong)limit, scoreThreshold: confidence);
140+
var points = await client.SearchAsync(collectionName,
141+
vector,
142+
limit: (ulong)limit,
143+
scoreThreshold: confidence,
144+
vectorsSelector: new WithVectorsSelector { Enable = withVector });
141145

142146
var pickFields = fields != null;
143147
foreach (var point in points)
@@ -174,15 +178,10 @@ public async Task<IEnumerable<VectorCollectionData>> Search(string collectionNam
174178
return results;
175179
}
176180

177-
public async Task<bool> DeleteCollectionData(string collectionName, string id)
181+
public async Task<bool> DeleteCollectionData(string collectionName, Guid id)
178182
{
179-
if (!Guid.TryParse(id, out var guid))
180-
{
181-
return false;
182-
}
183-
184183
var client = GetClient();
185-
var result = await client.DeleteAsync(collectionName, guid);
184+
var result = await client.DeleteAsync(collectionName, id);
186185
return result.Status == UpdateStatus.Completed;
187186
}
188187

src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelMemoryStoreProvider.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using BotSharp.Abstraction.VectorStorage;
33
using BotSharp.Abstraction.VectorStorage.Models;
44
using Microsoft.SemanticKernel.Memory;
5+
using System;
56
using System.Collections.Generic;
67
using System.Threading.Tasks;
78

@@ -62,21 +63,21 @@ public async Task<IEnumerable<VectorCollectionData>> Search(string collectionNam
6263
return resultTexts;
6364
}
6465

65-
public async Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload)
66+
public async Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload)
6667
{
6768
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
6869
await _memoryStore.UpsertAsync(collectionName, MemoryRecord.LocalRecord(id.ToString(), text, null, vector));
6970
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
7071
return true;
7172
}
7273

73-
public async Task<bool> DeleteCollectionData(string collectionName, string id)
74+
public async Task<bool> DeleteCollectionData(string collectionName, Guid id)
7475
{
7576
var exist = await _memoryStore.DoesCollectionExistAsync(collectionName);
7677

7778
if (exist)
7879
{
79-
await _memoryStore.RemoveAsync(collectionName, id);
80+
await _memoryStore.RemoveAsync(collectionName, id.ToString());
8081
return true;
8182
}
8283
return false;

0 commit comments

Comments
 (0)