Skip to content

add collection exist #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace BotSharp.Abstraction.Knowledges;
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> DeleteVectorCollection(string collectionName);
Task<IEnumerable<string>> GetVectorCollections(string type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public interface IBotSharpRepository
bool AddKnowledgeCollectionConfigs(List<VectorCollectionConfig> configs, bool reset = false);
bool DeleteKnowledgeCollectionConfig(string collectionName);
IEnumerable<VectorCollectionConfig> GetKnowledgeCollectionConfigs(VectorCollectionConfigFilter filter);

bool SaveKnolwedgeBaseFileMeta(KnowledgeDocMetaData metaData);
/// <summary>
/// Delete file meta data in a knowledge collection, given the vector store provider. If "fileId" is null, delete all in the collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ namespace BotSharp.Abstraction.VectorStorage;
public interface IVectorDb
{
string Provider { get; }


Task<bool> DoesCollectionExist(string collectionName);
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public PagedItems<KnowledgeDocMetaData> GetKnowledgeBaseFileMeta(string collecti

return new PagedItems<KnowledgeDocMetaData>
{
Items = records.Skip(filter.Offset).Take(filter.Size),
Items = records.OrderByDescending(x => x.CreateDate).Skip(filter.Offset).Take(filter.Size),
Count = records.Count
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using BotSharp.Abstraction.Files.Utilities;
using BotSharp.Abstraction.Graph.Models;
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.VectorStorage.Models;
using BotSharp.OpenAPI.ViewModels.Knowledges;

Expand All @@ -20,6 +19,12 @@ public KnowledgeBaseController(IKnowledgeService knowledgeService, IServiceProvi
}

#region Vector
[HttpGet("knowledge/vector/{collection}/exist")]
public async Task<bool> ExistVectorCollection([FromRoute] string collection)
{
return await _knowledgeService.ExistVectorCollection(collection);
}

[HttpGet("knowledge/vector/collections")]
public async Task<IEnumerable<string>> GetVectorCollections([FromQuery] string type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public class MemoryVectorDb : IVectorDb

public string Provider => "MemoryVector";


public async Task<bool> DoesCollectionExist(string collectionName)
{
return false;
}

public async Task<bool> CreateCollection(string collectionName, int dimension)
{
_collections[collectionName] = dimension;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using BotSharp.Abstraction.Files.Utilities;
using BotSharp.Abstraction.Knowledges.Helpers;
using BotSharp.Abstraction.VectorStorage.Enums;
using System.Collections;
using System.Net.Http;
using System.Net.Mime;

Expand All @@ -13,13 +12,21 @@ public partial class KnowledgeService
{
public async Task<UploadKnowledgeResponse> UploadDocumentsToKnowledge(string collectionName, IEnumerable<ExternalFileModel> files)
{
var res = new UploadKnowledgeResponse
{
Success = [],
Failed = files?.Select(x => x.FileName) ?? new List<string>()
};

if (string.IsNullOrWhiteSpace(collectionName) || files.IsNullOrEmpty())
{
return new UploadKnowledgeResponse
{
Success = [],
Failed = files?.Select(x => x.FileName) ?? new List<string>()
};
return res;
}

var exist = await ExistVectorCollection(collectionName);
if (!exist)
{
return res;
}

var db = _services.GetRequiredService<IBotSharpRepository>();
Expand Down Expand Up @@ -103,6 +110,9 @@ public async Task<bool> ImportDocumentContentToKnowledge(string collectionName,

try
{
var exist = await ExistVectorCollection(collectionName);
if (!exist) return false;

var db = _services.GetRequiredService<IBotSharpRepository>();
var userId = await GetUserId();
var vectorStoreProvider = _settings.VectorDb.Provider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ namespace BotSharp.Plugin.KnowledgeBase.Services;
public partial class KnowledgeService
{
#region Collection
public async Task<bool> ExistVectorCollection(string collectionName)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var vectorDb = GetVectorDb();

var exist = await vectorDb.DoesCollectionExist(collectionName);
if (exist) return true;

var configs = db.GetKnowledgeCollectionConfigs(new VectorCollectionConfigFilter
{
CollectionNames = [collectionName],
VectorStroageProviders = [_settings.VectorDb.Provider]
});

return !configs.IsNullOrEmpty();
}

public async Task<bool> CreateVectorCollection(string collectionName, string collectionType, int dimension, string provider, string model)
{
try
Expand Down
49 changes: 26 additions & 23 deletions src/Plugins/BotSharp.Plugin.Qdrant/QdrantDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,22 @@ private QdrantClient GetClient()
return _client;
}

public async Task<bool> CreateCollection(string collectionName, int dimension)
public async Task<bool> DoesCollectionExist(string collectionName)
{
var client = GetClient();
var exist = await DoesCollectionExist(client, collectionName);
return await client.CollectionExistsAsync(collectionName);
}

public async Task<bool> CreateCollection(string collectionName, int dimension)
{
var exist = await DoesCollectionExist(collectionName);

if (exist) return false;

try
{
// Create a new collection
var client = GetClient();
await client.CreateCollectionAsync(collectionName, new VectorParams()
{
Size = (ulong)dimension,
Expand All @@ -65,11 +71,11 @@ public async Task<bool> CreateCollection(string collectionName, int dimension)

public async Task<bool> DeleteCollection(string collectionName)
{
var client = GetClient();
var exist = await DoesCollectionExist(client, collectionName);
var exist = await DoesCollectionExist(collectionName);

if (!exist) return false;

var client = GetClient();
await client.DeleteCollectionAsync(collectionName);
return true;
}
Expand All @@ -83,8 +89,7 @@ public async Task<IEnumerable<string>> GetCollections()

public async Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionData(string collectionName, VectorFilter filter)
{
var client = GetClient();
var exist = await DoesCollectionExist(client, collectionName);
var exist = await DoesCollectionExist(collectionName);
if (!exist)
{
return new StringIdPagedItems<VectorCollectionData>();
Expand Down Expand Up @@ -126,6 +131,7 @@ public async Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionDa
};
}

var client = GetClient();
var totalPointCount = await client.CountAsync(collectionName, filter: queryFilter);
var response = await client.ScrollAsync(collectionName, limit: (uint)filter.Size,
offset: !string.IsNullOrWhiteSpace(filter.StartId) ? new PointId { Uuid = filter.StartId } : null,
Expand All @@ -152,15 +158,18 @@ public async Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionDa
public async Task<IEnumerable<VectorCollectionData>> GetCollectionData(string collectionName, IEnumerable<Guid> ids,
bool withPayload = false, bool withVector = false)
{
if (ids.IsNullOrEmpty()) return Enumerable.Empty<VectorCollectionData>();

var client = GetClient();
var exist = await DoesCollectionExist(client, collectionName);
if (ids.IsNullOrEmpty())
{
return Enumerable.Empty<VectorCollectionData>();
}

var exist = await DoesCollectionExist(collectionName);
if (!exist)
{
return Enumerable.Empty<VectorCollectionData>();
}

var client = GetClient();
var pointIds = ids.Select(x => new PointId { Uuid = x.ToString() }).Distinct().ToList();
var points = await client.RetrieveAsync(collectionName, pointIds, withPayload, withVector);
return points.Select(x => new VectorCollectionData
Expand Down Expand Up @@ -209,8 +218,7 @@ public async Task<IEnumerable<VectorCollectionData>> Search(string collectionNam
{
var results = new List<VectorCollectionData>();

var client = GetClient();
var exist = await DoesCollectionExist(client, collectionName);
var exist = await DoesCollectionExist(collectionName);
if (!exist)
{
return results;
Expand All @@ -221,7 +229,8 @@ public async Task<IEnumerable<VectorCollectionData>> Search(string collectionNam
{
payloadSelector.Include = new PayloadIncludeSelector { Fields = { fields.ToArray() } };
}


var client = GetClient();
var points = await client.SearchAsync(collectionName,
vector,
limit: (ulong)limit,
Expand All @@ -244,33 +253,27 @@ public async Task<bool> DeleteCollectionData(string collectionName, List<Guid> i
{
if (ids.IsNullOrEmpty()) return false;

var client = GetClient();
var exist = await DoesCollectionExist(client, collectionName);
var exist = await DoesCollectionExist(collectionName);
if (!exist)
{
return false;
}

var client = GetClient();
var result = await client.DeleteAsync(collectionName, ids);
return result.Status == UpdateStatus.Completed;
}

public async Task<bool> DeleteCollectionAllData(string collectionName)
{
var client = GetClient();
var exist = await DoesCollectionExist(client, collectionName);
var exist = await DoesCollectionExist(collectionName);
if (!exist)
{
return false;
}

var client = GetClient();
var result = await client.DeleteAsync(collectionName, new Filter());
return result.Status == UpdateStatus.Completed;
}


private async Task<bool> DoesCollectionExist(QdrantClient client, string collectionName)
{
return await client.CollectionExistsAsync(collectionName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public SemanticKernelMemoryStoreProvider(IMemoryStore memoryStore)

public string Provider => "SemanticKernel";


public async Task<bool> DoesCollectionExist(string collectionName)
{
return false;
}

public async Task<bool> CreateCollection(string collectionName, int dimension)
{
await _memoryStore.CreateCollectionAsync(collectionName);
Expand Down