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.
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));
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);}
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 }));};// Usageconst 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:
Splits your text into chunks (by newline in this case)
Generates embeddings for all chunks in a single API call
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.
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.
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, },});