BotSharp/src/Plugins/BotSharp.Plugin.SemanticKernel/SemanticKernelMemoryStoreProvider.cs

114 lines
4.7 KiB
C#
Raw Normal View History

2024-08-07 15:17:28 +00:00
using BotSharp.Abstraction.Utilities;
using BotSharp.Abstraction.VectorStorage;
2025-08-15 04:40:15 +00:00
using BotSharp.Abstraction.VectorStorage.Enums;
2024-08-15 23:19:10 +00:00
using BotSharp.Abstraction.VectorStorage.Models;
using Microsoft.SemanticKernel.Memory;
using System;
using System.Collections.Generic;
2024-09-10 19:02:25 +00:00
using System.Linq;
using System.Threading.Tasks;
namespace BotSharp.Plugin.SemanticKernel
{
internal class SemanticKernelMemoryStoreProvider : IVectorDb
{
2024-05-28 15:45:13 +00:00
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
private readonly IMemoryStore _memoryStore;
2024-05-28 15:45:13 +00:00
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
2024-05-28 15:45:13 +00:00
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
public SemanticKernelMemoryStoreProvider(IMemoryStore memoryStore)
2024-05-28 15:45:13 +00:00
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
{
this._memoryStore = memoryStore;
}
2024-08-31 00:31:18 +00:00
public string Provider => "SemanticKernel";
2024-09-24 20:34:47 +00:00
public async Task<bool> DoesCollectionExist(string collectionName)
{
return false;
}
2025-08-15 03:33:22 +00:00
public async Task<bool> CreateCollection(string collectionName, VectorCollectionCreateOptions options)
{
await _memoryStore.CreateCollectionAsync(collectionName);
2024-08-29 19:30:47 +00:00
return true;
}
public async Task<bool> DeleteCollection(string collectionName)
{
await _memoryStore.DeleteCollectionAsync(collectionName);
return false;
}
2024-08-23 16:25:44 +00:00
public Task<StringIdPagedItems<VectorCollectionData>> GetPagedCollectionData(string collectionName, VectorFilter filter)
2024-08-07 15:17:28 +00:00
{
2024-08-28 22:45:33 +00:00
throw new NotImplementedException();
2024-08-07 15:17:28 +00:00
}
2025-08-06 22:40:47 +00:00
public Task<IEnumerable<VectorCollectionData>> GetCollectionData(string collectionName, IEnumerable<Guid> ids, VectorQueryOptions? options = null)
2024-08-23 16:25:44 +00:00
{
throw new NotImplementedException();
}
2024-08-08 22:15:51 +00:00
public async Task<IEnumerable<string>> GetCollections()
{
var result = new List<string>();
await foreach (var collection in _memoryStore.GetCollectionsAsync())
{
result.Add(collection);
}
return result;
}
2025-08-05 20:46:07 +00:00
public async Task<IEnumerable<VectorCollectionData>> Search(string collectionName, float[] vector, VectorSearchOptions? options = null)
{
2025-08-05 20:46:07 +00:00
options ??= VectorSearchOptions.Default();
var results = _memoryStore.GetNearestMatchesAsync(collectionName, vector, options.Limit.GetValueOrDefault());
2024-08-15 23:19:10 +00:00
var resultTexts = new List<VectorCollectionData>();
await foreach (var (record, score) in results)
{
2024-08-15 23:19:10 +00:00
resultTexts.Add(new VectorCollectionData
{
2025-08-22 15:49:28 +00:00
Payload = new Dictionary<string, VectorPayloadValue>
2025-08-15 04:40:15 +00:00
{
{ "text", new(record.Metadata.Text, VectorPayloadDataType.String) }
},
Score = score,
2025-08-05 20:46:07 +00:00
Vector = options.WithVector ? record.Embedding.ToArray() : null
});
}
return resultTexts;
}
2025-08-15 04:40:15 +00:00
public async Task<bool> Upsert(string collectionName, Guid id, float[] vector, string text, Dictionary<string, VectorPayloadValue>? payload)
{
2024-05-28 15:45:13 +00:00
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
await _memoryStore.UpsertAsync(collectionName, MemoryRecord.LocalRecord(id.ToString(), text, null, vector));
2024-05-28 15:45:13 +00:00
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
2024-07-17 22:16:21 +00:00
return true;
}
2024-08-08 22:15:51 +00:00
2024-09-10 19:02:25 +00:00
public async Task<bool> DeleteCollectionData(string collectionName, List<Guid> ids)
2024-08-08 22:15:51 +00:00
{
2024-09-10 19:02:25 +00:00
if (ids.IsNullOrEmpty()) return false;
var exist = await _memoryStore.DoesCollectionExistAsync(collectionName);
2024-09-10 19:02:25 +00:00
if (!exist) return false;
2024-09-10 19:02:25 +00:00
await _memoryStore.RemoveBatchAsync(collectionName, ids.Select(x => x.ToString()));
return true;
2024-08-08 22:15:51 +00:00
}
2024-09-18 21:17:54 +00:00
public async Task<bool> DeleteCollectionAllData(string collectionName)
{
return await Task.FromResult(false);
}
}
}