add vector knowledge update

This commit is contained in:
Jicheng Lu 2024-08-22 22:13:40 -05:00
parent 6d0ace09fd
commit 47d7f01562
7 changed files with 75 additions and 8 deletions

View file

@ -10,6 +10,7 @@ public interface IKnowledgeService
Task FeedVectorKnowledge(string collectionName, KnowledgeCreationModel model);
Task<StringIdPagedItems<VectorSearchResult>> GetVectorCollectionData(string collectionName, VectorFilter filter);
Task<bool> DeleteVectorCollectionData(string collectionName, string id);
Task<bool> UpdateVectorCollectionData(string collectionName, VectorUpdateModel update);
Task<GraphSearchResult> SearchGraphKnowledge(string query, GraphSearchOptions options);
Task<KnowledgeSearchResult> SearchKnowledge(string query, string collectionName, VectorSearchOptions vectorOptions, GraphSearchOptions graphOptions);
}

View file

@ -0,0 +1,8 @@
namespace BotSharp.Abstraction.VectorStorage.Models;
public class VectorUpdateModel
{
public string Id { get; set; }
public string Text { get; set; }
public Dictionary<string, string>? Payload { get; set; }
}

View file

@ -2,6 +2,7 @@ using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.VectorStorage.Models;
using BotSharp.OpenAPI.ViewModels.Knowledges;
using System.Reflection.Metadata.Ecma335;
namespace BotSharp.OpenAPI.Controllers;
@ -54,6 +55,20 @@ public class KnowledgeBaseController : ControllerBase
};
}
[HttpPut("/knowledge/vector/{collection}/update")]
public async Task<bool> UpdateVectorKnowledge([FromRoute] string collection, [FromBody] VectorKnowledgeUpdateRequest request)
{
var update = new VectorUpdateModel
{
Id = request.Id,
Text = request.Text,
Payload = request.Payload
};
var updated = await _knowledgeService.UpdateVectorCollectionData(collection, update);
return updated;
}
[HttpDelete("/knowledge/vector/{collection}/data/{id}")]
public async Task<bool> DeleteVectorCollectionData([FromRoute] string collection, [FromRoute] string id)
{

View file

@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
namespace BotSharp.OpenAPI.ViewModels.Knowledges;
public class VectorKnowledgeUpdateRequest
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("text")]
public string Text { get; set; }
[JsonPropertyName("payload")]
public Dictionary<string, string>? Payload { get; set; }
}

View file

@ -16,7 +16,7 @@ public partial class KnowledgeService
}
catch (Exception ex)
{
_logger.LogWarning($"Error when deleting knowledge collection data ({collectionName}-{id}). {ex.Message}\r\n{ex.InnerException}");
_logger.LogWarning($"Error when deleting vector collection data ({collectionName}-{id}). {ex.Message}\r\n{ex.InnerException}");
return false;
}
}

View file

@ -9,12 +9,12 @@ public partial class KnowledgeService
{
try
{
var collections = _settings.Collections.Select(x => x.Name).ToList();
return collections;
var db = GetVectorDb();
return await db.GetCollections();
}
catch (Exception ex)
{
_logger.LogWarning($"Error when getting knowledge collections. {ex.Message}\r\n{ex.InnerException}");
_logger.LogWarning($"Error when getting vector db collections. {ex.Message}\r\n{ex.InnerException}");
return Enumerable.Empty<string>();
}
}
@ -34,7 +34,7 @@ public partial class KnowledgeService
}
catch (Exception ex)
{
_logger.LogWarning($"Error when getting knowledge collection data ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
_logger.LogWarning($"Error when getting vector knowledge collection data ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
return new StringIdPagedItems<VectorSearchResult>();
}
}
@ -55,7 +55,7 @@ public partial class KnowledgeService
}
catch (Exception ex)
{
_logger.LogWarning($"Error when searching knowledge ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
_logger.LogWarning($"Error when searching vector knowledge ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
return new List<VectorSearchResult>();
}
}
@ -73,7 +73,7 @@ public partial class KnowledgeService
}
catch (Exception ex)
{
_logger.LogWarning($"Error when searching graph {query}. {ex.Message}\r\n{ex.InnerException}");
_logger.LogWarning($"Error when searching graph knowledge (Query: {query}). {ex.Message}\r\n{ex.InnerException}");
return new GraphSearchResult();
}
}
@ -99,7 +99,7 @@ public partial class KnowledgeService
}
catch (Exception ex)
{
_logger.LogWarning($"Error when searching knowledge (vector collection: {collectionName}) {query}. {ex.Message}\r\n{ex.InnerException}");
_logger.LogWarning($"Error when searching knowledge (Vector collection: {collectionName}) (Query: {query}). {ex.Message}\r\n{ex.InnerException}");
return new KnowledgeSearchResult();
}
}

View file

@ -0,0 +1,28 @@
using BotSharp.Abstraction.VectorStorage.Models;
namespace BotSharp.Plugin.KnowledgeBase.Services;
public partial class KnowledgeService
{
public async Task<bool> UpdateVectorCollectionData(string collectionName, VectorUpdateModel update)
{
try
{
if (string.IsNullOrWhiteSpace(collectionName) || string.IsNullOrWhiteSpace(update.Text) || !Guid.TryParse(update.Id, out var guid))
{
return false;
}
var textEmbedding = GetTextEmbedding(collectionName);
var vector = await textEmbedding.GetVectorAsync(update.Text);
var db = GetVectorDb();
return await db.Upsert(collectionName, guid, vector, update.Text, update.Payload);
}
catch (Exception ex)
{
_logger.LogWarning($"Error when updating vector collection data. {ex.Message}\r\n{ex.InnerException}");
return false;
}
}
}