using LLama;
using LLama.Common;
using Microsoft.SemanticKernel.AI.Embeddings;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LLamaSharp.KernelMemory
{
///
/// Provides text embedding generation for LLamaSharp.
///
public class LLamaSharpTextEmbeddingGeneration : ITextEmbeddingGeneration, IDisposable
{
private readonly LLamaSharpConfig _config;
private readonly LLamaEmbedder _embedder;
private readonly LLamaWeights _weights;
///
/// Initializes a new instance of the class.
///
/// The configuration for LLamaSharp.
public LLamaSharpTextEmbeddingGeneration(LLamaSharpConfig config)
{
this._config = config;
var @params = new ModelParams(_config.ModelPath);
_weights = LLamaWeights.LoadFromFile(@params);
_embedder = new LLamaEmbedder(_weights, @params);
}
///
public void Dispose()
{
_embedder.Dispose();
_weights.Dispose();
}
///
public Task>> GenerateEmbeddingsAsync(IList data, CancellationToken cancellationToken = default)
{
IList> results = new List>();
foreach (var d in data)
{
var embeddings = _embedder.GetEmbeddings(d);
results.Add(new ReadOnlyMemory(embeddings));
}
return Task.FromResult(results);
}
}
}