using LLama.Native; using System; using LLama.Exceptions; using LLama.Abstractions; namespace LLama { /// /// The embedder for LLama, which supports getting embeddings from text. /// public sealed class LLamaEmbedder : IDisposable { private readonly LLamaContext _ctx; /// /// Dimension of embedding vectors /// public int EmbeddingSize => _ctx.EmbeddingSize; /// /// Create a new embedder (loading temporary weights) /// /// [Obsolete("Preload LLamaWeights and use the constructor which accepts them")] public LLamaEmbedder(ILLamaParams allParams) : this(allParams, allParams) { } /// /// Create a new embedder (loading temporary weights) /// /// /// [Obsolete("Preload LLamaWeights and use the constructor which accepts them")] public LLamaEmbedder(IModelParams modelParams, IContextParams contextParams) { using var weights = LLamaWeights.LoadFromFile(modelParams); contextParams.EmbeddingMode = true; _ctx = weights.CreateContext(contextParams); } /// /// Create a new embedder, using the given LLamaWeights /// /// /// public LLamaEmbedder(LLamaWeights weights, IContextParams @params) { @params.EmbeddingMode = true; _ctx = weights.CreateContext(@params); } /// /// Get the embeddings of the text. /// /// /// unused /// Add bos to the text. /// unused /// /// [Obsolete("'threads' and 'encoding' parameters are no longer used")] // ReSharper disable once MethodOverloadWithOptionalParameter public float[] GetEmbeddings(string text, int threads = -1, bool addBos = true, string encoding = "UTF-8") { return GetEmbeddings(text, addBos); } /// /// Get the embeddings of the text. /// /// /// /// public float[] GetEmbeddings(string text) { return GetEmbeddings(text, true); } /// /// Get the embeddings of the text. /// /// /// Add bos to the text. /// /// public float[] GetEmbeddings(string text, bool addBos) { var embed_inp_array = _ctx.Tokenize(text, addBos); // TODO(Rinne): deal with log of prompt if (embed_inp_array.Length > 0) _ctx.Eval(embed_inp_array, 0); unsafe { var embeddings = NativeApi.llama_get_embeddings(_ctx.NativeHandle); if (embeddings == null) return Array.Empty(); return new Span(embeddings, EmbeddingSize).ToArray(); } } /// /// /// public void Dispose() { _ctx.Dispose(); } } }