Method GetEmbeddings
- Namespace
- LMKit.Embeddings
- Assembly
- LM-Kit.NET.dll
GetEmbeddings(Attachment, CancellationToken)
Generates the embedding vector for a given file attachment. If the associated LM supports image embeddings, image attachments (for example, PNG, JPEG, TIFF) will be processed into embeddings; otherwise, text‑based attachments (for example, TXT, HTML) will be used.
public float[] GetEmbeddings(Attachment attachment, CancellationToken cancellationToken = default)
Parameters
attachmentAttachmentThe file attachment containing content, name, MIME type, and size. Supported formats include: PNG, BMP, GIF, PSD, PIC, JPEG, PNM, HDR, TGA, WEBP, TIFF, TXT, HTML.
cancellationTokenCancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- float[]
An array of floats representing the embedding vector of the attachment’s content. Each element contributes to the multidimensional representation of its semantic meaning.
Exceptions
- ArgumentNullException
Thrown when
attachmentisnull.
GetEmbeddings(ImageBuffer, CancellationToken)
Generates the embedding vector for a given image.
public float[] GetEmbeddings(ImageBuffer image, CancellationToken cancellationToken = default)
Parameters
imageImageBufferThe image buffer to be converted into an embedding vector.
cancellationTokenCancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- float[]
An array of floats representing the embedding vector of the image. Each element contributes to the multidimensional representation of its visual content.
Exceptions
- ArgumentNullException
Thrown when
imageisnull.- InvalidModelException
Thrown when the current model does not support image embeddings.
GetEmbeddings(IEnumerable<ImageBuffer>, CancellationToken)
Generates embedding vectors for a collection of images.
public float[][] GetEmbeddings(IEnumerable<ImageBuffer> images, CancellationToken cancellationToken = default)
Parameters
imagesIEnumerable<ImageBuffer>An enumerable of image buffers to be converted into embedding vectors.
cancellationTokenCancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- float[][]
An array of float arrays, each representing the embedding vector of the corresponding input image.
Exceptions
- ArgumentNullException
Thrown when
imagesisnullor empty.- InvalidModelException
Thrown when the current model does not support image embeddings.
GetEmbeddings(string, CancellationToken)
Generates the embedding vector for a given text string. This vector represents the text in a high-dimensional space, enabling various natural language processing tasks by capturing semantic meaning.
public float[] GetEmbeddings(string text, CancellationToken cancellationToken = default)
Parameters
textstringThe text string to be converted into an embedding vector. Cannot be null or empty.
cancellationTokenCancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- float[]
An array of floats representing the embedding vector of the input text. Each element contributes to the multidimensional representation of the text's semantic meaning.
Examples
using LMKit.Model;
using LMKit.Embeddings;
using System;
LM model = LM.LoadFromModelID("embeddinggemma-300m");
Embedder embedder = new Embedder(model);
// Generate embedding for text
float[] embedding = embedder.GetEmbeddings("Artificial intelligence is transforming industries.");
Console.WriteLine($"Embedding dimension: {embedding.Length}");
Console.WriteLine($"Embedding norm: {Math.Sqrt(embedding.Sum(x => x * x)):F4}");
Remarks
The dimension of the array is specified by the EmbeddingSize property.
Exceptions
- ArgumentNullException
Thrown when the input text is null or empty, as an embedding vector cannot be generated from an empty input.
GetEmbeddings(IEnumerable<string>, CancellationToken)
Generates embedding vectors for a collection of text strings. Each vector represents a text in a high-dimensional space, enabling various natural language processing tasks by capturing semantic meaning.
public float[][] GetEmbeddings(IEnumerable<string> text, CancellationToken cancellationToken = default)
Parameters
textIEnumerable<string>An enumerable of text strings to be converted into embedding vectors. Cannot be null or empty.
cancellationTokenCancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- float[][]
An array of float arrays, each representing the embedding vector of the corresponding input text.
Exceptions
- ArgumentNullException
Thrown when the input text is null or empty.
GetEmbeddings(IList<int>, CancellationToken)
Generates the embedding vector for a given tokenized text. This vector represents the text in a high-dimensional space, enabling various natural language processing tasks by capturing semantic meaning.
public float[] GetEmbeddings(IList<int> tokens, CancellationToken cancellationToken = default)
Parameters
tokensIList<int>The list of tokens to be converted into an embedding vector. Cannot be null or empty.
cancellationTokenCancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- float[]
An array of floats representing the embedding vector of the input tokens.
Remarks
The dimension of the array is specified by the EmbeddingSize property.
Exceptions
- ArgumentNullException
Thrown when the input tokens are null or empty.
GetEmbeddings(IEnumerable<IList<int>>, CancellationToken)
Generates embedding vectors for a collection of tokenized texts. Each vector represents a text in a high-dimensional space, enabling various natural language processing tasks by capturing semantic meaning.
public float[][] GetEmbeddings(IEnumerable<IList<int>> tokens, CancellationToken cancellationToken = default)
Parameters
tokensIEnumerable<IList<int>>An enumerable of token lists to be converted into embedding vectors. Cannot be null or empty.
cancellationTokenCancellationTokenOptional. A CancellationToken for handling cancellation requests.
Returns
- float[][]
An array of float arrays, each representing the embedding vector of the corresponding input tokens.
Exceptions
- ArgumentNullException
Thrown when the input tokens are null or empty.