using LLama;
using LLama.Common;
using Microsoft.KernelMemory;
using Microsoft.KernelMemory.AI;
namespace LLamaSharp.KernelMemory
{
///
/// Provides text embedding generation for LLamaSharp.
///
public class LLamaSharpTextEmbeddingGenerator
: ITextEmbeddingGenerator, IDisposable
{
private readonly LLamaSharpConfig? _config;
private readonly LLamaWeights? _weights;
private readonly LLamaEmbedder _embedder;
private bool _ownsEmbedder = false;
private bool _ownsWeights = false;
///
public int MaxTokens => (int?)_config?.ContextSize ?? 2048;
///
/// Initializes a new instance of the class.
///
/// The configuration for LLamaSharp.
public LLamaSharpTextEmbeddingGenerator(LLamaSharpConfig config)
{
this._config = config;
var @params = new ModelParams(_config.ModelPath)
{
EmbeddingMode = true,
MainGpu = _config.MainGpu,
SplitMode = _config.SplitMode
};
_weights = LLamaWeights.LoadFromFile(@params);
_embedder = new LLamaEmbedder(_weights, @params);
_ownsWeights = true;
_ownsEmbedder = true;
}
///
/// Initializes a new instance of the class from reused weights.
///
/// The configuration for LLamaSharp.
/// A LLamaWeights object.
public LLamaSharpTextEmbeddingGenerator(LLamaSharpConfig config, LLamaWeights weights)
{
this._config = config;
var @params = new ModelParams(_config.ModelPath)
{
EmbeddingMode = true,
MainGpu = _config.MainGpu,
SplitMode = _config.SplitMode
};
_weights = weights;
_embedder = new LLamaEmbedder(_weights, @params);
_ownsEmbedder = true;
}
///
/// Initializes a new instance of the class from reused embedder.
///
/// A LLamaEmbedder object.
public LLamaSharpTextEmbeddingGenerator(LLamaEmbedder embedder)
{
this._config = null;
this._weights = null;
_embedder = embedder;
}
///
public void Dispose()
{
if (_ownsWeights)
{
_weights?.Dispose();
}
if (_ownsEmbedder)
{
_embedder.Dispose();
}
}
///
public async Task>> GenerateEmbeddingsAsync(IList data, CancellationToken cancellationToken = default)
{
IList> results = new List>();
foreach (var d in data)
{
var embeddings = await _embedder.GetEmbeddings(d, cancellationToken);
results.Add(new ReadOnlyMemory(embeddings));
}
return results;
}
///
public async Task GenerateEmbeddingAsync(string text, CancellationToken cancellationToken = default)
{
var embeddings = await _embedder.GetEmbeddings(text, cancellationToken);
return new Embedding(embeddings);
}
///
public int CountTokens(string text) => _embedder.Context.Tokenize(text).Length;
}
}