Add UpsertVectorCollectionData

This commit is contained in:
Haiping Chen 2024-09-22 15:05:26 -05:00
parent fe2d6f37af
commit d9996fdd9f
5 changed files with 45 additions and 2 deletions

View file

@ -15,6 +15,7 @@ public interface IKnowledgeService
Task<bool> DeleteVectorCollectionAllData(string collectionName);
Task<bool> CreateVectorCollectionData(string collectionName, VectorCreateModel create);
Task<bool> UpdateVectorCollectionData(string collectionName, VectorUpdateModel update);
Task<bool> UpsertVectorCollectionData(string collectionName, VectorUpdateModel update);
#endregion
#region Graph

View file

@ -5,5 +5,7 @@ public class VectorCollectionData
public string Id { get; set; }
public Dictionary<string, string> Data { get; set; } = new();
public double? Score { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public float[]? Vector { get; set; }
}

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.Knowledges.Enums;
namespace BotSharp.Abstraction.VectorStorage.Models;
public class VectorSearchResult : VectorCollectionData

View file

@ -10,6 +10,12 @@
<OutputPath>$(SolutionDir)packages</OutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Remove="packages\**" />
<EmbeddedResource Remove="packages\**" />
<None Remove="packages\**" />
</ItemGroup>
<ItemGroup>
<None Include="..\..\..\arts\Icon.png">
<Pack>True</Pack>

View file

@ -164,6 +164,42 @@ public partial class KnowledgeService
}
}
public async Task<bool> UpsertVectorCollectionData(string collectionName, VectorUpdateModel update)
{
try
{
if (string.IsNullOrWhiteSpace(collectionName) || string.IsNullOrWhiteSpace(update.Text) || !Guid.TryParse(update.Id, out var guid))
{
return false;
}
var db = GetVectorDb();
var found = await db.GetCollectionData(collectionName, new List<Guid> { guid },
withVector: true,
withPayload: true);
if (!found.IsNullOrEmpty())
{
if (found.First().Data["text"] == update.Text)
{
// Only update payload
return await db.Upsert(collectionName, guid, found.First().Vector, update.Text, update.Payload);
}
}
var textEmbedding = GetTextEmbedding(collectionName);
var vector = await textEmbedding.GetVectorAsync(update.Text);
var payload = update.Payload ?? new();
payload[KnowledgePayloadName.DataSource] = !string.IsNullOrWhiteSpace(update.DataSource) ? update.DataSource : VectorDataSource.Api;
return await db.Upsert(collectionName, guid, vector, update.Text, payload);
}
catch (Exception ex)
{
_logger.LogWarning($"Error when updating vector collection data. {ex.Message}\r\n{ex.InnerException}");
return false;
}
}
public async Task<bool> DeleteVectorCollectionData(string collectionName, string id)
{
try