Add TextEmbeddingProvider and MemoryStoreProvider

This commit is contained in:
xbotter 2023-11-16 21:12:33 +08:00
parent dbd1b8d0ce
commit f7ab61d544
No known key found for this signature in database
GPG key ID: D299220A7FE5CF1E
5 changed files with 120 additions and 2 deletions

View file

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
@ -10,7 +10,8 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-beta6" />
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-beta7" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Memory" Version="1.0.0-beta7" />
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.6.11" />
</ItemGroup>

View file

@ -0,0 +1,52 @@
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
{
private readonly IMemoryStore _memoryStore;
public SemanticKernelMemoryStoreProvider(IMemoryStore memoryStore)
{
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;
}
public async Task<List<string>> Search(string collectionName, float[] vector, int limit = 5)
{
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;
}
public async Task Upsert(string collectionName, int id, float[] vector, string text)
{
await _memoryStore.UpsertAsync(collectionName, MemoryRecord.LocalRecord(id.ToString(), text, null, vector));
}
}
}

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Plugins;
using BotSharp.Abstraction.VectorStorage;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
@ -12,8 +13,13 @@ namespace BotSharp.Plugin.SemanticKernel
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
var settings = new SemanticKernelSettings();
config.Bind("SemanticKernel", settings);
services.AddScoped<ITextCompletion, SemanticKernelTextCompletionProvider>();
services.AddScoped<IChatCompletion, SemanticKernelChatCompletionProvider>();
services.AddScoped<IVectorDb, SemanticKernelMemoryStoreProvider>();
services.AddScoped<ITextEmbedding, SemanticKernelTextEmbeddingProvider>();
}
}
}

View file

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Plugin.SemanticKernel
{
internal class SemanticKernelSettings
{
}
}

View file

@ -0,0 +1,48 @@
using BotSharp.Abstraction.MLTasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.Embeddings;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Plugins.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BotSharp.Plugin.SemanticKernel
{
/// <summary>
/// Use Semantic Kernel Memory as text embedding provider
/// </summary>
public class SemanticKernelTextEmbeddingProvider : ITextEmbedding
{
private readonly ITextEmbeddingGeneration _embedding;
/// <summary>
/// Constructor of <see cref="SemanticKernelTextEmbeddingProvider"/>
/// </summary>
/// <param name="kernel"></param>
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding, int dimension)
{
this._embedding = embedding;
Dimension = dimension;
}
public int Dimension { get; }
public float[] GetVector(string text)
{
return this._embedding.GenerateEmbeddingAsync(text)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult()
.ToArray();
}
public List<float[]> GetVectors(List<string> texts)
{
return this._embedding.GenerateEmbeddingsAsync(texts).ConfigureAwait(false).GetAwaiter().GetResult()
.Select(_ => _.ToArray())
.ToList();
}
}
}