Embedding Similarity

Vector Similarity

Overview

Calculate similarity between embedding vectors using various metrics. Support for cosine similarity, euclidean distance, dot product, and more.

Embedding Similarity Diagram

Supported Metrics

  • Cosine Similarity
  • Euclidean Distance
  • Dot Product
  • Manhattan Distance

Key Features

  • Multiple metrics
  • Batch comparison
  • Normalized scores
  • Dimension validation

Configuration

Required Parameters

  • vectorsArray of embedding vectors
  • metricSimilarity metric to use

Example Usage

// Calculate cosine similarity
const cosineSim = new EmbeddingSimilarity({
  metric: "cosine"
});

// Compare two vectors
const result = await cosineSim.compare({
  vector1: [0.1, 0.2, 0.3],
  vector2: [0.2, 0.3, 0.4]
});

// Batch comparison
const batchResult = await cosineSim.compareBatch({
  sourceVector: [0.1, 0.2, 0.3],
  targetVectors: [
    [0.2, 0.3, 0.4],
    [0.3, 0.4, 0.5],
    [0.4, 0.5, 0.6]
  ]
});

// Using different metrics
const euclideanSim = new EmbeddingSimilarity({
  metric: "euclidean"
});

const dotProductSim = new EmbeddingSimilarity({
  metric: "dot-product"
});

Best Practices

  • Normalize vectors when needed
  • Choose appropriate metric
  • Validate vector dimensions
  • Handle edge cases

Metric Selection Tips

  • Cosine for direction similarity
  • Euclidean for absolute distance
  • Dot product for quick comparison

Response Format

{
  "similarity": {
    "score": number,
    "normalized_score": number,
    "metric": string
  },
  "metadata": {
    "vector_dimensions": number,
    "comparison_time": number
  },
  "status": {
    "success": boolean,
    "error": string | null
  }
}