replace with options

This commit is contained in:
Jicheng Lu 2025-08-14 22:33:22 -05:00
parent 26aab1f3a8
commit 15978470bf
8 changed files with 35 additions and 18 deletions

View file

@ -1,5 +1,4 @@
using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.Models;
using BotSharp.Abstraction.VectorStorage.Models;
namespace BotSharp.Abstraction.Knowledges;
@ -8,7 +7,7 @@ public interface IKnowledgeService
{
#region Vector
Task<bool> ExistVectorCollection(string collectionName);
Task<bool> CreateVectorCollection(string collectionName, string collectionType, int dimension, string provider, string model);
Task<bool> CreateVectorCollection(string collectionName, string collectionType, VectorCollectionCreateOptions options);
Task<bool> DeleteVectorCollection(string collectionName);
Task<IEnumerable<VectorCollectionConfig>> GetVectorCollections(string? type = null);
Task<VectorCollectionDetails?> GetVectorCollectionDetails(string collectionName);

View file

@ -16,7 +16,7 @@ public interface IVectorDb
=> throw new NotImplementedException();
Task<IEnumerable<VectorCollectionData>> GetCollectionData(string collectionName, IEnumerable<Guid> ids, VectorQueryOptions? options = null)
=> throw new NotImplementedException();
Task<bool> CreateCollection(string collectionName, int dimension)
Task<bool> CreateCollection(string collectionName, VectorCollectionCreateOptions options)
=> throw new NotImplementedException();
Task<bool> DeleteCollection(string collectionName)
=> throw new NotImplementedException();

View file

@ -0,0 +1,8 @@
namespace BotSharp.Abstraction.VectorStorage.Models;
public class VectorCollectionCreateOptions
{
public int Dimension { get; set; }
public string Provider { get; set; } = null!;
public string Model { get; set; } = null!;
}

View file

@ -45,7 +45,13 @@ public class KnowledgeBaseController : ControllerBase
[HttpPost("knowledge/vector/create-collection")]
public async Task<bool> CreateVectorCollection([FromBody] CreateVectorCollectionRequest request)
{
return await _knowledgeService.CreateVectorCollection(request.CollectionName, request.CollectionType, request.Dimension, request.Provider, request.Model);
var options = new VectorCollectionCreateOptions
{
Provider = request.Provider,
Model = request.Model,
Dimension = request.Dimension
};
return await _knowledgeService.CreateVectorCollection(request.CollectionName, request.CollectionType, options);
}
[HttpDelete("knowledge/vector/{collection}/delete-collection")]

View file

@ -17,9 +17,9 @@ public class MemoryVectorDb : IVectorDb
return false;
}
public async Task<bool> CreateCollection(string collectionName, int dimension)
public async Task<bool> CreateCollection(string collectionName, VectorCollectionCreateOptions options)
{
_collections[collectionName] = dimension;
_collections[collectionName] = options.Dimension;
_vectors[collectionName] = new List<VecRecord>();
return true;
}

View file

@ -24,7 +24,7 @@ public partial class KnowledgeService
return !configs.IsNullOrEmpty();
}
public async Task<bool> CreateVectorCollection(string collectionName, string collectionType, int dimension, string provider, string model)
public async Task<bool> CreateVectorCollection(string collectionName, string collectionType, VectorCollectionCreateOptions options)
{
try
{
@ -46,9 +46,9 @@ public partial class KnowledgeService
},
TextEmbedding = new KnowledgeEmbeddingConfig
{
Provider = provider,
Model = model,
Dimension = dimension
Provider = options.Provider,
Model = options.Model,
Dimension = options.Dimension
}
}
});
@ -56,7 +56,7 @@ public partial class KnowledgeService
if (created)
{
var vectorDb = GetVectorDb();
created = await vectorDb.CreateCollection(collectionName, dimension);
created = await vectorDb.CreateCollection(collectionName, options);
}
return created;
@ -182,7 +182,6 @@ public partial class KnowledgeService
}
}
public async Task<bool> UpdateVectorCollectionData(string collectionName, VectorUpdateModel update)
{
try
@ -245,7 +244,12 @@ public partial class KnowledgeService
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;
if (!payload.TryGetValue(KnowledgePayloadName.DataSource, out _))
{
payload[KnowledgePayloadName.DataSource] = !string.IsNullOrWhiteSpace(update.DataSource) ?
update.DataSource : VectorDataSource.Api;
}
return await db.Upsert(collectionName, guid, vector, update.Text, payload);
}
@ -266,7 +270,7 @@ public partial class KnowledgeService
}
var db = GetVectorDb();
return await db.DeleteCollectionData(collectionName, new List<Guid> { guid });
return await db.DeleteCollectionData(collectionName, [guid]);
}
catch (Exception ex)
{

View file

@ -57,7 +57,7 @@ public class QdrantDb : IVectorDb
return await client.CollectionExistsAsync(collectionName);
}
public async Task<bool> CreateCollection(string collectionName, int dimension)
public async Task<bool> CreateCollection(string collectionName, VectorCollectionCreateOptions options)
{
var exist = await DoesCollectionExist(collectionName);
@ -69,14 +69,14 @@ public class QdrantDb : IVectorDb
var client = GetClient();
await client.CreateCollectionAsync(collectionName, new VectorParams()
{
Size = (ulong)dimension,
Size = (ulong)options.Dimension,
Distance = Distance.Cosine
});
return true;
}
catch (Exception ex)
{
_logger.LogWarning($"Error when create collection (Name: {collectionName}, Dimension: {dimension}).");
_logger.LogWarning($"Error when create collection (Name: {collectionName}, Dimension: {options.Dimension}).");
return false;
}
}

View file

@ -31,7 +31,7 @@ namespace BotSharp.Plugin.SemanticKernel
return false;
}
public async Task<bool> CreateCollection(string collectionName, int dimension)
public async Task<bool> CreateCollection(string collectionName, VectorCollectionCreateOptions options)
{
await _memoryStore.CreateCollectionAsync(collectionName);
return true;