BotSharp/src/Plugins/BotSharp.Plugin.MetaAI/Providers/fastTextEmbeddingProvider.cs

36 lines
992 B
C#
Raw Normal View History

2023-06-17 13:32:39 +00:00
using BotSharp.Abstraction.MLTasks;
using BotSharp.Plugin.MetaAI.Settings;
using FastText.NetWrapper;
2023-06-18 02:56:22 +00:00
using System.IO;
2023-06-17 13:32:39 +00:00
namespace BotSharp.Plugin.MetaAI.Providers;
public class fastTextEmbeddingProvider : ITextEmbedding
{
private FastTextWrapper _fastText;
private readonly fastTextSetting _settings;
2023-06-18 13:02:59 +00:00
public int Dimension => _fastText.GetModelDimension();
2023-06-17 13:32:39 +00:00
public fastTextEmbeddingProvider(fastTextSetting settings)
{
_settings = settings;
_fastText = new FastTextWrapper();
2023-06-18 02:56:22 +00:00
if (!File.Exists(settings.ModelPath))
{
throw new FileNotFoundException($"Can't load pre-trained word vectors from {settings.ModelPath}.\n Try to download from https://fasttext.cc/docs/en/english-vectors.html.");
}
2023-06-17 13:32:39 +00:00
if (!_fastText.IsModelReady())
{
2023-06-18 13:02:59 +00:00
_fastText.LoadModel(_settings.ModelPath);
2023-06-17 13:32:39 +00:00
}
}
public float[] GetVector(string text)
{
return _fastText.GetSentenceVector(text);
}
}