add knowledge collection info

This commit is contained in:
Jicheng Lu 2024-08-06 16:02:02 -05:00
parent a015b4bfd0
commit 93646a01ae
10 changed files with 98 additions and 1 deletions

View file

@ -11,4 +11,8 @@ public interface IKnowledgeService
Task EmbedKnowledge(KnowledgeCreationModel knowledge);
Task<string> GetKnowledges(KnowledgeRetrievalModel retrievalModel);
Task<List<RetrievedResult>> GetAnswer(KnowledgeRetrievalModel retrievalModel);
#region List
Task<KnowledgeCollectionInfo> GetKnowledgeCollectionInfo(string collectionName);
#endregion
}

View file

@ -0,0 +1,7 @@
namespace BotSharp.Abstraction.Knowledges.Models;
public class KnowledgeCollectionInfo
{
public ulong DataCount { get; set; }
public ulong VectorCount { get; set; }
}

View file

@ -1,8 +1,11 @@
using BotSharp.Abstraction.Knowledges.Models;
namespace BotSharp.Abstraction.VectorStorage;
public interface IVectorDb
{
Task<List<string>> GetCollections();
Task<KnowledgeCollectionInfo> GetCollectionInfo(string collectionName);
Task CreateCollection(string collectionName, int dim);
Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? payload = null);
Task<List<string>> Search(string collectionName, float[] vector, string returnFieldName, int limit = 5, float confidence = 0.5f);

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.Knowledges.Settings;
using BotSharp.OpenAPI.ViewModels.Knowledges;
using Microsoft.AspNetCore.Http;
namespace BotSharp.OpenAPI.Controllers;
@ -90,4 +91,12 @@ public class KnowledgeBaseController : ControllerBase
return Ok(new { count = files.Count, size });
}
[HttpGet("/knowledge/info")]
public async Task<KnowledgeCollectionInfoViewModel> GetKnowledgeCollectionInfo([FromQuery] string collectionName)
{
var info = await _knowledgeService.GetKnowledgeCollectionInfo(collectionName);
return KnowledgeCollectionInfoViewModel.ToViewModel(info);
}
}

View file

@ -0,0 +1,22 @@
using BotSharp.Abstraction.Knowledges.Models;
using System.Text.Json.Serialization;
namespace BotSharp.OpenAPI.ViewModels.Knowledges;
public class KnowledgeCollectionInfoViewModel
{
[JsonPropertyName("data_count")]
public ulong DataCount { get; set; }
[JsonPropertyName("vector_count")]
public ulong VectorCount { get; set; }
public static KnowledgeCollectionInfoViewModel ToViewModel(KnowledgeCollectionInfo info)
{
return new KnowledgeCollectionInfoViewModel
{
DataCount = info.DataCount,
VectorCount = info.VectorCount
};
}
}

View file

@ -18,6 +18,20 @@ public class MemVectorDatabase : IVectorDb
return _collections.Select(x => x.Key).ToList();
}
public async Task<KnowledgeCollectionInfo> GetCollectionInfo(string collectionName)
{
if (_vectors.TryGetValue(collectionName, out var info))
{
info = new List<VecRecord>();
}
return new KnowledgeCollectionInfo
{
DataCount = (ulong)(info?.Count ?? 0),
VectorCount = (ulong)(info?.Count(x => x.Vector != null && x.Vector.Length > 0) ?? 0)
};
}
public async Task<List<string>> Search(string collectionName, float[] vector, string returnFieldName, int limit = 5, float confidence = 0.5f)
{
if (!_vectors.ContainsKey(collectionName))

View file

@ -0,0 +1,18 @@
namespace BotSharp.Plugin.KnowledgeBase.Services;
public partial class KnowledgeService
{
public async Task<KnowledgeCollectionInfo> GetKnowledgeCollectionInfo(string collectionName)
{
try
{
var db = GetVectorDb();
return await db.GetCollectionInfo(collectionName);
}
catch (Exception ex)
{
_logger.LogWarning($"Error when getting knowledge collectio info. {ex.Message}\r\n{ex.InnerException}");
return new KnowledgeCollectionInfo();
}
}
}

View file

@ -5,14 +5,17 @@ public partial class KnowledgeService : IKnowledgeService
private readonly IServiceProvider _services;
private readonly KnowledgeBaseSettings _settings;
private readonly ITextChopper _textChopper;
private readonly ILogger<KnowledgeService> _logger;
public KnowledgeService(IServiceProvider services,
KnowledgeBaseSettings settings,
ITextChopper textChopper)
ITextChopper textChopper,
ILogger<KnowledgeService> logger)
{
_services = services;
_settings = settings;
_textChopper = textChopper;
_logger = logger;
}
public async Task EmbedKnowledge(KnowledgeCreationModel knowledge)

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.VectorStorage;
using System;
using System.Collections.Generic;
@ -12,6 +13,11 @@ public class FaissDb : IVectorDb
throw new NotImplementedException();
}
public Task<KnowledgeCollectionInfo> GetCollectionInfo(string collectionName)
{
throw new NotImplementedException();
}
public Task<List<string>> GetCollections()
{
throw new NotImplementedException();

View file

@ -1,3 +1,4 @@
using BotSharp.Abstraction.Knowledges.Models;
using Qdrant.Client;
using Qdrant.Client.Grpc;
@ -38,6 +39,16 @@ public class QdrantDb : IVectorDb
return collections.ToList();
}
public async Task<KnowledgeCollectionInfo> GetCollectionInfo(string collectionName)
{
var info = await GetClient().GetCollectionInfoAsync(collectionName);
return new KnowledgeCollectionInfo
{
DataCount = info.PointsCount,
VectorCount = info.VectorsCount
};
}
public async Task CreateCollection(string collectionName, int dim)
{
var collections = await GetCollections();