EmbeddingVector

Models & Types · AiDotNet.Agentic.Embeddings

An immutable, finite-valued embedding vector with a cached magnitude.

For Beginners

An embedding is a list of numbers that stands in for a piece of text, arranged so that texts meaning similar things get similar lists. Cosine similarity measures the angle between two such lists: 1.0 means they point the same way (very similar text), 0.0 means unrelated. This class holds one such list, checks the numbers are sane, and does the similarity arithmetic for you.

How It Works

The vector is copied on construction and never handed out as a mutable array, so a vector held in a cache cannot be altered by whoever received it. Every component is validated as finite: a provider that returns a NaN or an infinity is a broken response, and admitting one would silently poison every later cosine comparison with a NaN that compares false against every threshold. Magnitude is computed once, which is what makes a repeated similarity scan against a fixed set of neighbours cheap.

CosineSimilarity returns zero for a mismatched length or a zero-magnitude operand rather than throwing, matching the reference implementation’s guard so that a degenerate vector reads as “not similar to anything” instead of aborting a search. It never returns NaN.

Constructors

Constructor Summary
EmbeddingVector(IEnumerable<double> components) Initializes an embedding vector from a sequence of finite components.

Properties

Property Summary
IReadOnlyList<double> Components { get; } Gets the vector components in provider order.
double Magnitude { get; } Gets the Euclidean length of the vector, computed once at construction.
int Dimensions { get; } Gets the number of components.

Methods

Method Summary
double CosineSimilarity(EmbeddingVector first, EmbeddingVector second) Computes the cosine similarity of two vectors.
string ToString() Returns the dimension count only, never the components, so a log stays bounded.