🔧 Update ITextEmbedding interface and its implementations

- Update ITextEmbedding interface to use async methods for getting vectors
- Update KnowledgeService to use async methods for getting vectors
- Update TextEmbeddingProvider and fastTextEmbeddingProvider to use async methods for getting vectors
- Update IntentClassifier to use async methods for getting vectors
- Update SemanticKernelTextEmbeddingProvider to use async methods for getting vectors
This commit is contained in:
xbotter 2023-11-16 21:37:53 +08:00
parent f7ab61d544
commit 114fa98616
No known key found for this signature in database
GPG key ID: D299220A7FE5CF1E
7 changed files with 32 additions and 27 deletions

View file

@ -1,8 +1,10 @@
using System.Threading;
namespace BotSharp.Abstraction.MLTasks;
public interface ITextEmbedding
{
int Dimension { get; }
float[] GetVector(string text);
List<float[]> GetVectors(List<string> texts);
Task<float[]> GetVectorAsync(string text);
Task<List<float[]>> GetVectorsAsync(List<string> texts);
}

View file

@ -31,7 +31,7 @@ public class KnowledgeService : IKnowledgeService
await db.CreateCollection(knowledge.AgentId, textEmbedding.Dimension);
foreach (var line in lines)
{
var vec = textEmbedding.GetVector(line);
var vec = await textEmbedding.GetVectorAsync(line);
await db.Upsert(knowledge.AgentId, idStart, vec, line);
idStart++;
Console.WriteLine($"Saved vector {idStart}/{lines.Count}: {line}\n");
@ -41,7 +41,7 @@ public class KnowledgeService : IKnowledgeService
public async Task<string> GetKnowledges(KnowledgeRetrievalModel retrievalModel)
{
var textEmbedding = GetTextEmbedding();
var vector = textEmbedding.GetVector(retrievalModel.Question);
var vector = await textEmbedding.GetVectorAsync(retrievalModel.Question);
// Vector search
var result = await GetVectorDb().Search(retrievalModel.AgentId, vector, limit: 10);

View file

@ -15,7 +15,7 @@ public class TextEmbeddingProvider : ITextEmbedding
_settings = settings;
}
public float[] GetVector(string text)
public Task<float[]> GetVectorAsync(string text)
{
if (_embedder == null)
{
@ -23,10 +23,10 @@ public class TextEmbeddingProvider : ITextEmbedding
_embedder = new LLamaEmbedder(new ModelParams(path));
}
return _embedder.GetEmbeddings(text);
return Task.FromResult(_embedder.GetEmbeddings(text));
}
public List<float[]> GetVectors(List<string> texts)
public Task<List<float[]>> GetVectorsAsync(List<string> texts)
{
throw new NotImplementedException();
}

View file

@ -3,6 +3,7 @@ using BotSharp.Plugin.MetaAI.Settings;
using FastText.NetWrapper;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
namespace BotSharp.Plugin.MetaAI.Providers;
@ -26,19 +27,19 @@ public class fastTextEmbeddingProvider : ITextEmbedding
}
public float[] GetVector(string text)
public Task<float[]> GetVectorAsync(string text)
{
LoadModel();
return _fastText.GetSentenceVector(text);
return Task.FromResult(_fastText.GetSentenceVector(text));
}
public List<float[]> GetVectors(List<string> texts)
public async Task<List<float[]>> GetVectorsAsync(List<string> texts)
{
LoadModel();
var vectors = new List<float[]>();
for (int i = 0; i < texts.Count; i++)
{
vectors.Add(GetVector(texts[i]));
vectors.Add(await GetVectorAsync(texts[i]));
}
return vectors;
}

View file

@ -33,8 +33,8 @@ public class IntentClassifier
private string[] _labels;
public string[] Labels => _labels == null ? GetLabels() : _labels;
public IntentClassifier(IServiceProvider services,
ClassifierSetting settings,
public IntentClassifier(IServiceProvider services,
ClassifierSetting settings,
KnowledgeBaseSettings knowledgeBaseSettings,
ILogger logger)
{
@ -140,7 +140,7 @@ public class IntentClassifier
.FirstOrDefault(x => x.GetType().FullName.EndsWith(knowledgeSettings.TextEmbedding));
var x = np.zeros((1, embedding.Dimension), dtype: np.float32);
x[0] = embedding.GetVector(text);
x[0] = embedding.GetVectorAsync(text).GetAwaiter().GetResult();
return x;
}
@ -178,7 +178,7 @@ public class IntentClassifier
{
var texts = File.ReadAllLines(filePath, Encoding.UTF8).ToList();
vectorList.AddRange(vector.GetVectors(texts));
vectorList.AddRange(vector.GetVectorsAsync(texts).GetAwaiter().GetResult());
string fileName = Path.GetFileNameWithoutExtension(filePath);
labelList.AddRange(Enumerable.Repeat(fileName, texts.Count).ToList());
}

View file

@ -7,6 +7,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Plugin.SemanticKernel
{
@ -20,29 +21,27 @@ namespace BotSharp.Plugin.SemanticKernel
/// <summary>
/// Constructor of <see cref="SemanticKernelTextEmbeddingProvider"/>
/// </summary>
/// <param name="kernel"></param>
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGeneration embedding, int dimension)
{
this._embedding = embedding;
Dimension = dimension;
}
/// <inheritdoc/>
public int Dimension { get; }
public float[] GetVector(string text)
/// <inheritdoc/>
public async Task<float[]> GetVectorAsync(string text)
{
return this._embedding.GenerateEmbeddingAsync(text)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult()
.ToArray();
return (await this._embedding.GenerateEmbeddingAsync(text)).ToArray();
}
public List<float[]> GetVectors(List<string> texts)
/// <inheritdoc/>
public async Task<List<float[]>> GetVectorsAsync(List<string> texts)
{
return this._embedding.GenerateEmbeddingsAsync(texts).ConfigureAwait(false).GetAwaiter().GetResult()
.Select(_ => _.ToArray())
.ToList();
var embeddings = await this._embedding.GenerateEmbeddingsAsync(texts);
return embeddings.Select(_ => _.ToArray())
.ToList();
}
}
}

View file

@ -12,6 +12,7 @@ namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
{
internal class SemanticKernelHelper : IChatCompletion, ITextCompletion, IAIService
{
private Dictionary<string, string> _attributes = new();
private readonly string _excepted;
public SemanticKernelHelper(string excepted)
@ -19,6 +20,8 @@ namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
this._excepted = excepted;
}
public IReadOnlyDictionary<string, string> Attributes => _attributes;
public ChatHistory CreateNewChat(string? instructions = null)
{
return new ChatHistory();
@ -26,7 +29,7 @@ namespace BotSharp.Plugin.SemanticKernel.UnitTests.Helpers
public Task<IReadOnlyList<IChatResult>> GetChatCompletionsAsync(ChatHistory chat, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)
{
return Task.FromResult<IReadOnlyList<IChatResult>>( new List<IChatResult> { new ResultHelper(_excepted) });
return Task.FromResult<IReadOnlyList<IChatResult>>(new List<IChatResult> { new ResultHelper(_excepted) });
}
public Task<IReadOnlyList<ITextResult>> GetCompletionsAsync(string text, AIRequestSettings? requestSettings = null, CancellationToken cancellationToken = default)