Skip to main content

Overview

Embeddings convert text into vector representations, enabling semantic search, similarity comparison, and other vector operations.

Embeddings Class

Import:
import { Embeddings } from "@langchain/core/embeddings";

Methods

embedDocuments
method
Embed multiple documents
async embedDocuments(texts: string[]): Promise<number[][]>
embedQuery
method
Embed a single query
async embedQuery(text: string): Promise<number[]>

Usage Examples

OpenAI Embeddings

import { OpenAIEmbeddings } from "@langchain/openai";

const embeddings = new OpenAIEmbeddings({
  model: "text-embedding-3-small",
});

// Embed a query
const queryVector = await embeddings.embedQuery(
  "What is the capital of France?"
);
console.log(queryVector.length); // 1536

// Embed multiple documents
const docVectors = await embeddings.embedDocuments([
  "Paris is the capital of France.",
  "London is the capital of England.",
  "Berlin is the capital of Germany."
]);
console.log(docVectors.length); // 3
import { cosineSimilarity } from "@langchain/core/utils/math";

const query = "French capital";
const docs = [
  "Paris is the capital of France.",
  "Tokyo is the capital of Japan."
];

const queryEmbedding = await embeddings.embedQuery(query);
const docEmbeddings = await embeddings.embedDocuments(docs);

const similarities = docEmbeddings.map(docEmbed => 
  cosineSimilarity(queryEmbedding, docEmbed)
);

console.log(similarities); // [0.85, 0.32]

Caching Embeddings

import { CacheBackedEmbeddings } from "@langchain/core/embeddings";
import { InMemoryStore } from "@langchain/core/stores";

const underlyingEmbeddings = new OpenAIEmbeddings();
const store = new InMemoryStore();

const cachedEmbeddings = CacheBackedEmbeddings.fromBytesStore(
  underlyingEmbeddings,
  store,
  {
    namespace: underlyingEmbeddings.model
  }
);

// First call makes API request
const result1 = await cachedEmbeddings.embedQuery("Hello");

// Second call uses cache
const result2 = await cachedEmbeddings.embedQuery("Hello");

Implementing Custom Embeddings

import { Embeddings } from "@langchain/core/embeddings";

class CustomEmbeddings extends Embeddings {
  async embedDocuments(texts: string[]): Promise<number[][]> {
    // Your implementation
    return texts.map(text => this.generateVector(text));
  }

  async embedQuery(text: string): Promise<number[]> {
    // Your implementation
    return this.generateVector(text);
  }

  private generateVector(text: string): number[] {
    // Generate embedding vector
    return Array(1536).fill(0).map(() => Math.random());
  }
}

OpenAI

text-embedding-3-small (1536d) text-embedding-3-large (3072d)

Cohere

embed-english-v3.0 embed-multilingual-v3.0

Google

text-embedding-004 Vertex AI embeddings

Local

Ollama embeddings Transformers.js

Embedding Integrations

Available embedding providers

Vector Stores

Using embeddings with vector stores

Build docs developers (and LLMs) love