add create / delete vector collection
This commit is contained in:
parent
f4e20ac64c
commit
3fd7f749ca
|
|
@ -5,6 +5,9 @@ namespace BotSharp.Abstraction.Knowledges;
|
|||
|
||||
public interface IKnowledgeService
|
||||
{
|
||||
#region Vector
|
||||
Task<bool> CreateVectorCollection(string collectionName, int dimension);
|
||||
Task<bool> DeleteVectorCollection(string collectionName);
|
||||
Task<IEnumerable<string>> GetVectorCollections();
|
||||
Task<IEnumerable<VectorSearchResult>> SearchVectorKnowledge(string query, string collectionName, VectorSearchOptions options);
|
||||
Task FeedVectorKnowledge(string collectionName, KnowledgeCreationModel model);
|
||||
|
|
@ -12,6 +15,10 @@ public interface IKnowledgeService
|
|||
Task<bool> DeleteVectorCollectionData(string collectionName, string id);
|
||||
Task<bool> CreateVectorCollectionData(string collectionName, VectorCreateModel create);
|
||||
Task<bool> UpdateVectorCollectionData(string collectionName, VectorUpdateModel update);
|
||||
#endregion
|
||||
|
||||
#region Graph
|
||||
Task<GraphSearchResult> SearchGraphKnowledge(string query, GraphSearchOptions options);
|
||||
Task<KnowledgeSearchResult> SearchKnowledge(string query, string collectionName, VectorSearchOptions vectorOptions, GraphSearchOptions graphOptions);
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,8 @@ public interface IVectorDb
|
|||
Task<IEnumerable<string>> GetCollections();
|
||||
Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionData(string collectionName, VectorFilter filter);
|
||||
Task<IEnumerable<VectorCollectionData>> GetCollectionData(string collectionName, IEnumerable<Guid> ids, bool withPayload = false, bool withVector = false);
|
||||
Task CreateCollection(string collectionName, int dim);
|
||||
Task<bool> CreateCollection(string collectionName, int dimension);
|
||||
Task<bool> DeleteCollection(string collectionName);
|
||||
Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload = null);
|
||||
Task<IEnumerable<VectorCollectionData>> Search(string collectionName, float[] vector, IEnumerable<string>? fields, int limit = 5, float confidence = 0.5f, bool withVector = false);
|
||||
Task<bool> DeleteCollectionData(string collectionName, Guid id);
|
||||
|
|
|
|||
|
|
@ -18,12 +18,25 @@ public class KnowledgeBaseController : ControllerBase
|
|||
_services = services;
|
||||
}
|
||||
|
||||
#region Vector
|
||||
[HttpGet("knowledge/vector/collections")]
|
||||
public async Task<IEnumerable<string>> GetVectorCollections()
|
||||
{
|
||||
return await _knowledgeService.GetVectorCollections();
|
||||
}
|
||||
|
||||
[HttpPost("knowledge/vector/{collection}/create-collection/{dimension}")]
|
||||
public async Task<bool> CreateVectorCollection([FromRoute] string collection, [FromRoute] int dimension)
|
||||
{
|
||||
return await _knowledgeService.CreateVectorCollection(collection, dimension);
|
||||
}
|
||||
|
||||
[HttpDelete("knowledge/vector/{collection}/delete-collection")]
|
||||
public async Task<bool> GetVectorCollections([FromRoute] string collection)
|
||||
{
|
||||
return await _knowledgeService.DeleteVectorCollection(collection);
|
||||
}
|
||||
|
||||
[HttpPost("/knowledge/vector/{collection}/search")]
|
||||
public async Task<IEnumerable<VectorKnowledgeViewModel>> SearchVectorKnowledge([FromRoute] string collection, [FromBody] SearchVectorKnowledgeRequest request)
|
||||
{
|
||||
|
|
@ -109,7 +122,10 @@ public class KnowledgeBaseController : ControllerBase
|
|||
System.IO.File.Delete(filePath);
|
||||
return Ok(new { count = 1, file.Length });
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region Graph
|
||||
[HttpPost("/knowledge/graph/search")]
|
||||
public async Task<GraphKnowledgeViewModel> SearchGraphKnowledge([FromBody] SearchGraphKnowledgeRequest request)
|
||||
{
|
||||
|
|
@ -124,7 +140,9 @@ public class KnowledgeBaseController : ControllerBase
|
|||
Result = result.Result
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Knowledge
|
||||
[HttpPost("/knowledge/search")]
|
||||
public async Task<KnowledgeSearchViewModel> SearchKnowledge([FromBody] SearchKnowledgeRequest request)
|
||||
{
|
||||
|
|
@ -148,4 +166,5 @@ public class KnowledgeBaseController : ControllerBase
|
|||
GraphResult = result?.GraphResult != null ? new GraphKnowledgeViewModel { Result = result.GraphResult.Result } : null
|
||||
};
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,10 +10,16 @@ public class MemoryVectorDb : IVectorDb
|
|||
|
||||
public string Name => "MemoryVector";
|
||||
|
||||
public async Task CreateCollection(string collectionName, int dim)
|
||||
public async Task<bool> CreateCollection(string collectionName, int dimension)
|
||||
{
|
||||
_collections[collectionName] = dim;
|
||||
_collections[collectionName] = dimension;
|
||||
_vectors[collectionName] = new List<VecRecord>();
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteCollection(string collectionName)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> GetCollections()
|
||||
|
|
|
|||
|
|
@ -25,6 +25,25 @@ public partial class KnowledgeService
|
|||
}
|
||||
}
|
||||
|
||||
public async Task<bool> CreateVectorCollection(string collectionName, int dimension)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(collectionName))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var db = GetVectorDb();
|
||||
return await db.CreateCollection(collectionName, dimension);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Error when creating a vector collection ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> CreateVectorCollectionData(string collectionName, VectorCreateModel create)
|
||||
{
|
||||
try
|
||||
|
|
|
|||
|
|
@ -2,6 +2,25 @@ namespace BotSharp.Plugin.KnowledgeBase.Services;
|
|||
|
||||
public partial class KnowledgeService
|
||||
{
|
||||
public async Task<bool> DeleteVectorCollection(string collectionName)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(collectionName))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var db = GetVectorDb();
|
||||
return await db.DeleteCollection(collectionName);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning($"Error when deleting collection ({collectionName}). {ex.Message}\r\n{ex.InnerException}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteVectorCollectionData(string collectionName, string id)
|
||||
{
|
||||
try
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
|
||||
namespace BotSharp.Plugin.KnowledgeBase.Services;
|
||||
|
||||
public partial class KnowledgeService : IKnowledgeService
|
||||
|
|
|
|||
|
|
@ -11,7 +11,12 @@ public class FaissDb : IVectorDb
|
|||
{
|
||||
public string Name => "Faiss";
|
||||
|
||||
public Task CreateCollection(string collectionName, int dim)
|
||||
public Task<bool> CreateCollection(string collectionName, int dimension)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<bool> DeleteCollection(string collectionName)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -35,6 +35,34 @@ public class QdrantDb : IVectorDb
|
|||
return _client;
|
||||
}
|
||||
|
||||
public async Task<bool> CreateCollection(string collectionName, int dim)
|
||||
{
|
||||
var client = GetClient();
|
||||
var exist = await DoesCollectionExist(client, collectionName);
|
||||
|
||||
if (exist) return false;
|
||||
|
||||
// Create a new collection
|
||||
await client.CreateCollectionAsync(collectionName, new VectorParams()
|
||||
{
|
||||
Size = (ulong)dim,
|
||||
Distance = Distance.Cosine
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteCollection(string collectionName)
|
||||
{
|
||||
var client = GetClient();
|
||||
var exist = await DoesCollectionExist(client, collectionName);
|
||||
|
||||
if (!exist) return false;
|
||||
|
||||
await client.DeleteCollectionAsync(collectionName);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> GetCollections()
|
||||
{
|
||||
// List all the collections
|
||||
|
|
@ -117,27 +145,7 @@ public class QdrantDb : IVectorDb
|
|||
});
|
||||
}
|
||||
|
||||
public async Task CreateCollection(string collectionName, int dim)
|
||||
{
|
||||
var client = GetClient();
|
||||
var exist = await DoesCollectionExist(client, collectionName);
|
||||
if (!exist)
|
||||
{
|
||||
// Create a new collection
|
||||
await client.CreateCollectionAsync(collectionName, new VectorParams()
|
||||
{
|
||||
Size = (ulong)dim,
|
||||
Distance = Distance.Cosine
|
||||
});
|
||||
}
|
||||
|
||||
// Get collection info
|
||||
var collectionInfo = await client.GetCollectionInfoAsync(collectionName);
|
||||
if (collectionInfo == null)
|
||||
{
|
||||
throw new Exception($"Create {collectionName} failed.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, string>? payload = null)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -24,9 +24,16 @@ namespace BotSharp.Plugin.SemanticKernel
|
|||
|
||||
public string Name => "SemanticKernel";
|
||||
|
||||
public async Task CreateCollection(string collectionName, int dim)
|
||||
public async Task<bool> CreateCollection(string collectionName, int dimension)
|
||||
{
|
||||
await _memoryStore.CreateCollectionAsync(collectionName);
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DeleteCollection(string collectionName)
|
||||
{
|
||||
await _memoryStore.DeleteCollectionAsync(collectionName);
|
||||
return false;
|
||||
}
|
||||
|
||||
public Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionData(string collectionName, VectorFilter filter)
|
||||
|
|
|
|||
|
|
@ -155,28 +155,7 @@ namespace BotSharp.Plugin.TencentCos.Modules
|
|||
|
||||
public string? GetDirFile(string dir, string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
var request = new GetBucketRequest(_fullBucketName);
|
||||
request.SetPrefix($"{dir.TrimEnd('/')}/");
|
||||
request.SetDelimiter("/");
|
||||
|
||||
var result = _cosXml.GetBucket(request);
|
||||
|
||||
var info = result.listBucket;
|
||||
|
||||
var objects = info.contentsList;
|
||||
|
||||
return objects.Where(o => o.size > 0).FirstOrDefault(o => o.key == key)?.key;
|
||||
}
|
||||
catch (CosClientException clientEx)
|
||||
{
|
||||
throw new Exception(clientEx.Message);
|
||||
}
|
||||
catch (CosServerException serverEx)
|
||||
{
|
||||
throw new Exception(serverEx.Message);
|
||||
}
|
||||
return GetDirFiles(dir).FirstOrDefault(x => x == key);
|
||||
}
|
||||
|
||||
public List<string> GetDirectories(string dir)
|
||||
|
|
|
|||
Loading…
Reference in a new issue