Skip to content

Commit d9996fd

Browse files
committed
Add UpsertVectorCollectionData
1 parent fe2d6f3 commit d9996fd

File tree

5 files changed

+45
-2
lines changed

5 files changed

+45
-2
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public interface IKnowledgeService
1515
Task<bool> DeleteVectorCollectionAllData(string collectionName);
1616
Task<bool> CreateVectorCollectionData(string collectionName, VectorCreateModel create);
1717
Task<bool> UpdateVectorCollectionData(string collectionName, VectorUpdateModel update);
18+
Task<bool> UpsertVectorCollectionData(string collectionName, VectorUpdateModel update);
1819
#endregion
1920

2021
#region Graph

src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorCollectionData.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ public class VectorCollectionData
55
public string Id { get; set; }
66
public Dictionary<string, string> Data { get; set; } = new();
77
public double? Score { get; set; }
8+
9+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
810
public float[]? Vector { get; set; }
911
}

src/Infrastructure/BotSharp.Abstraction/VectorStorage/Models/VectorSearchResult.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using BotSharp.Abstraction.Knowledges.Enums;
2-
31
namespace BotSharp.Abstraction.VectorStorage.Models;
42

53
public class VectorSearchResult : VectorCollectionData

src/Infrastructure/BotSharp.OpenAPI/BotSharp.OpenAPI.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
<OutputPath>$(SolutionDir)packages</OutputPath>
1111
</PropertyGroup>
1212

13+
<ItemGroup>
14+
<Compile Remove="packages\**" />
15+
<EmbeddedResource Remove="packages\**" />
16+
<None Remove="packages\**" />
17+
</ItemGroup>
18+
1319
<ItemGroup>
1420
<None Include="..\..\..\arts\Icon.png">
1521
<Pack>True</Pack>

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,42 @@ public async Task<bool> UpdateVectorCollectionData(string collectionName, Vector
164164
}
165165
}
166166

167+
public async Task<bool> UpsertVectorCollectionData(string collectionName, VectorUpdateModel update)
168+
{
169+
try
170+
{
171+
if (string.IsNullOrWhiteSpace(collectionName) || string.IsNullOrWhiteSpace(update.Text) || !Guid.TryParse(update.Id, out var guid))
172+
{
173+
return false;
174+
}
175+
176+
var db = GetVectorDb();
177+
var found = await db.GetCollectionData(collectionName, new List<Guid> { guid },
178+
withVector: true,
179+
withPayload: true);
180+
if (!found.IsNullOrEmpty())
181+
{
182+
if (found.First().Data["text"] == update.Text)
183+
{
184+
// Only update payload
185+
return await db.Upsert(collectionName, guid, found.First().Vector, update.Text, update.Payload);
186+
}
187+
}
188+
189+
var textEmbedding = GetTextEmbedding(collectionName);
190+
var vector = await textEmbedding.GetVectorAsync(update.Text);
191+
var payload = update.Payload ?? new();
192+
payload[KnowledgePayloadName.DataSource] = !string.IsNullOrWhiteSpace(update.DataSource) ? update.DataSource : VectorDataSource.Api;
193+
194+
return await db.Upsert(collectionName, guid, vector, update.Text, payload);
195+
}
196+
catch (Exception ex)
197+
{
198+
_logger.LogWarning($"Error when updating vector collection data. {ex.Message}\r\n{ex.InnerException}");
199+
return false;
200+
}
201+
}
202+
167203
public async Task<bool> DeleteVectorCollectionData(string collectionName, string id)
168204
{
169205
try

0 commit comments

Comments
 (0)