2024-08-07 15:17:28 +00:00
using BotSharp.Abstraction.Knowledges.Models ;
using BotSharp.Abstraction.Utilities ;
2023-11-16 13:12:33 +00:00
using BotSharp.Abstraction.VectorStorage ;
using Microsoft.SemanticKernel.Memory ;
2024-08-08 22:15:51 +00:00
using System ;
2023-11-16 13:12:33 +00:00
using System.Collections.Generic ;
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.
2023-11-16 13:12:33 +00:00
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.
2023-11-16 13:12:33 +00:00
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.
2023-11-16 13:12:33 +00:00
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.
2023-11-16 13:12:33 +00:00
{
this . _memoryStore = memoryStore ;
}
2024-08-09 21:03:29 +00:00
public string Name = > "SemanticKernel" ;
2023-11-16 13:12:33 +00:00
public async Task CreateCollection ( string collectionName , int dim )
{
await _memoryStore . CreateCollectionAsync ( collectionName ) ;
}
2024-08-07 16:50:23 +00:00
public Task < StringIdPagedItems < KnowledgeCollectionData > > GetCollectionData ( string collectionName , KnowledgeFilter filter )
2024-08-07 15:17:28 +00:00
{
throw new System . NotImplementedException ( ) ;
}
2024-08-08 22:15:51 +00:00
public async Task < IEnumerable < string > > GetCollections ( )
2023-11-16 13:12:33 +00:00
{
var result = new List < string > ( ) ;
await foreach ( var collection in _memoryStore . GetCollectionsAsync ( ) )
{
result . Add ( collection ) ;
}
return result ;
}
2024-08-09 21:03:29 +00:00
public async Task < IEnumerable < KnowledgeSearchResult > > Search ( string collectionName , float [ ] vector ,
IEnumerable < string > fields , int limit = 5 , float confidence = 0.5f , bool withVector = false )
2023-11-16 13:12:33 +00:00
{
var results = _memoryStore . GetNearestMatchesAsync ( collectionName , vector , limit ) ;
2024-08-09 21:03:29 +00:00
var resultTexts = new List < KnowledgeSearchResult > ( ) ;
await foreach ( var ( record , score ) in results )
2023-11-16 13:12:33 +00:00
{
2024-08-09 21:03:29 +00:00
resultTexts . Add ( new KnowledgeSearchResult
{
Data = new Dictionary < string , string > { { "text" , record . Metadata . Text } } ,
Score = score ,
Vector = withVector ? record . Embedding . ToArray ( ) : null
} ) ;
2023-11-16 13:12:33 +00:00
}
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 )
2023-11-16 13:12:33 +00:00
{
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.
2023-11-16 13:12:33 +00:00
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 ;
2023-11-16 13:12:33 +00:00
}
2024-08-08 22:15:51 +00:00
2024-08-09 21:03:29 +00:00
public async Task < bool > DeleteCollectionData ( string collectionName , string id )
2024-08-08 22:15:51 +00:00
{
2024-08-09 21:03:29 +00:00
var exist = await _memoryStore . DoesCollectionExistAsync ( collectionName ) ;
if ( exist )
{
await _memoryStore . RemoveAsync ( collectionName , id ) ;
return true ;
}
return false ;
2024-08-08 22:15:51 +00:00
}
2023-11-16 13:12:33 +00:00
}
}