Skip to main content

Quickstart

This guide shows you how to generate embeddings with Voyage AI Provider. You’ll learn the basics of text embeddings and see a complete working example.

Prerequisites

Before you begin, make sure you have:
  • Node.js 18 or later installed
  • A Voyage AI API key (get one here)
  • The voyage-ai-provider package installed
If you haven’t installed the provider yet, follow the installation guide.

Generate your first embedding

Create a new file and add this code to generate an embedding for a single text:
1

Import the provider

Import the voyage provider and the embed function from the AI SDK:
import { voyage } from 'voyage-ai-provider';
import { embed } from 'ai';
2

Create a model

Initialize a text embedding model. We’ll use voyage-3-lite for this example:
const model = voyage.textEmbeddingModel('voyage-3-lite');
3

Generate the embedding

Call embed with your model and text:
const { embedding } = await embed({
  model,
  value: 'The quick brown fox jumps over the lazy dog',
});

console.log('Embedding length:', embedding.length);
console.log('First 5 dimensions:', embedding.slice(0, 5));

Generate multiple embeddings

To generate embeddings for multiple texts at once, use the embedMany function:
import { voyage } from 'voyage-ai-provider';
import { embedMany } from 'ai';

const model = voyage.textEmbeddingModel('voyage-3-lite');

const { embeddings } = await embedMany({
  model,
  values: [
    'The quick brown fox jumps over the lazy dog',
    'Artificial intelligence is transforming the world',
    'Machine learning enables computers to learn without being explicitly programmed',
  ],
});

for (const [index, embedding] of embeddings.entries()) {
  console.log(`Embedding ${index + 1} length:`, embedding.length);
}

Complete example

Here’s a complete example that generates embeddings and stores them with their content:
import { voyage } from 'voyage-ai-provider';
import { embedMany } from 'ai';

const embeddingModel = voyage.textEmbeddingModel('voyage-3-lite');

export const generateEmbeddings = async (
  value: string,
): Promise<Array<{ embedding: number[]; content: string }>> => {
  // Generate chunks from the input value
  const chunks = value.split('\n');

  const { embeddings } = await embedMany({
    model: embeddingModel,
    values: chunks,
  });

  return embeddings.map((e, i) => ({ content: chunks[i], embedding: e }));
};

// Usage
const text = `Artificial intelligence is transforming industries worldwide.
Machine learning powers recommendation systems and search engines.
Deep learning enables breakthroughs in computer vision and natural language processing.`;

const results = await generateEmbeddings(text);
console.log(`Generated ${results.length} embeddings`);
This example:
  1. Splits your text into chunks (by newline in this case)
  2. Generates embeddings for all chunks in a single API call
  3. Returns an array pairing each chunk with its embedding
You can customize how you split text based on your use case. Consider using sentence boundaries, paragraphs, or a chunking library for better semantic coherence.

Configure model settings

You can pass additional settings to customize the embedding model:
import { createVoyage } from 'voyage-ai-provider';
import { embedMany } from 'ai';

const voyage = createVoyage({
  apiKey: process.env.VOYAGE_API_KEY,
});

const model = voyage.textEmbeddingModel('voyage-3-lite', {
  inputType: 'document', // or 'query'
  outputDimension: '1024', // 256, 512, 1024, or 2048 for supported models
  outputDtype: 'float',
});

const { embeddings } = await embedMany({
  model,
  values: ['Your text here'],
});
The inputType parameter helps the model optimize embeddings for your use case. Use 'document' for texts you want to search over, and 'query' for search queries.

Use provider options

You can also pass provider-specific options directly in the embed or embedMany call:
import { voyage } from 'voyage-ai-provider';
import { embed } from 'ai';
import type { VoyageEmbeddingOptions } from 'voyage-ai-provider';

const model = voyage.textEmbeddingModel('voyage-3-lite');

const { embedding } = await embed({
  model,
  value: 'The quick brown fox jumps over the lazy dog',
  providerOptions: {
    voyage: {
      inputType: 'query',
    } satisfies VoyageEmbeddingOptions,
  },
});

Next steps

Text embeddings

Learn about different text embedding models and use cases

Image embeddings

Generate embeddings from images

Multimodal embeddings

Combine text and images in embeddings

Reranking

Improve search results with reranking

Build docs developers (and LLMs) love