using LLama;
using LLama.Abstractions;
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 LLamaWeights? _weights;
private readonly LLamaEmbedder _embedder;
private bool _ownsEmbedder = false;
private bool _ownsWeights = false;
private readonly Dictionary _attributes = new();
public IReadOnlyDictionary Attributes => this._attributes;
///
/// 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);
_ownsWeights = true;
_ownsEmbedder = true;
}
///
/// Initializes a new instance of the class from reused weights.
///
/// The configuration for LLamaSharp.
/// A LLamaWeights object.
public LLamaSharpTextEmbeddingGeneration(LLamaSharpConfig config, LLamaWeights weights)
{
this._config = config;
var @params = new ModelParams(_config.ModelPath);
_weights = weights;
_embedder = new LLamaEmbedder(_weights, @params);
_ownsEmbedder = true;
}
///
/// Initializes a new instance of the class from reused embedder.
///
/// A LLamaEmbedder object.
public LLamaSharpTextEmbeddingGeneration(LLamaEmbedder embedder)
{
this._config = null;
this._weights = null;
_embedder = embedder;
}
///
public void Dispose()
{
if (_ownsWeights)
{
_weights?.Dispose();
}
if (_ownsEmbedder)
{
_embedder.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);
}
}
}