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

60 lines
2.7 KiB
C#
Raw Normal View History

using BotSharp.Abstraction.VectorStorage;
using Microsoft.SemanticKernel.Memory;
using System;
using System.Collections.Generic;
using System.Text;
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;
}
public async Task CreateCollection(string collectionName, int dim)
{
await _memoryStore.CreateCollectionAsync(collectionName);
}
public async Task<List<string>> GetCollections()
{
var result = new List<string>();
await foreach (var collection in _memoryStore.GetCollectionsAsync())
{
result.Add(collection);
}
return result;
}
2024-07-17 22:16:21 +00:00
public async Task<List<string>> Search(string collectionName, float[] vector, string returnFieldName, int limit = 5, float confidence = 0.5f)
{
var results = _memoryStore.GetNearestMatchesAsync(collectionName, vector, limit);
var resultTexts = new List<string>();
await foreach (var (record, _) in results)
{
resultTexts.Add(record.Metadata.Text);
}
return resultTexts;
}
2024-07-17 22:16:21 +00:00
public async Task<bool> Upsert(string collectionName, string id, float[] vector, string text, Dictionary<string, string>? 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;
}
}
}