Learn how to generate text embeddings using Voyage AI models with the Vercel AI SDK
Text embeddings convert text into dense vector representations that capture semantic meaning. You can use Voyage AI’s specialized text embedding models to power semantic search, recommendations, clustering, and other AI applications.
Generate embeddings for a single text input using the embed function:
import { createVoyage } from 'voyage-ai-provider';import { embed } from 'ai';const voyage = createVoyage({ apiKey: process.env.VOYAGE_API_KEY,});const { embedding } = await embed({ model: voyage.textEmbeddingModel('voyage-3-lite'), value: 'The quick brown fox jumps over the lazy dog',});console.log(embedding);
Generate embeddings for multiple texts efficiently using embedMany:
import { createVoyage } from 'voyage-ai-provider';import { embedMany } from 'ai';const voyage = createVoyage({ apiKey: process.env.VOYAGE_API_KEY,});const { embeddings } = await embedMany({ model: voyage.textEmbeddingModel('voyage-3-lite'), 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(`Text ${index + 1}: ${embedding.length} dimensions`);}
Voyage AI uses different prompts for queries and documents to optimize retrieval performance.
1
Query embeddings
2
Use inputType: 'query' when embedding search queries:
3
import { createVoyage } from 'voyage-ai-provider';import { embed } from 'ai';import type { VoyageEmbeddingOptions } from 'voyage-ai-provider';const voyage = createVoyage({ apiKey: process.env.VOYAGE_API_KEY,});const { embedding } = await embed({ model: voyage.textEmbeddingModel('voyage-3-lite'), value: 'How do I reset my password?', providerOptions: { voyage: { inputType: 'query', } satisfies VoyageEmbeddingOptions, },});
4
The model prepends “Represent the query for retrieving supporting documents: ” to query inputs.
5
Document embeddings
6
Use inputType: 'document' when embedding documents for retrieval:
7
import { createVoyage } from 'voyage-ai-provider';import { embedMany } from 'ai';import type { VoyageEmbeddingOptions } from 'voyage-ai-provider';const voyage = createVoyage({ apiKey: process.env.VOYAGE_API_KEY,});const { embeddings } = await embedMany({ model: voyage.textEmbeddingModel('voyage-3-lite'), values: [ 'To reset your password, click the forgot password link on the login page.', 'Our support team is available 24/7 to help with your questions.', 'Premium users get access to priority support and advanced features.', ], providerOptions: { voyage: { inputType: 'document', } satisfies VoyageEmbeddingOptions, },});
8
The model prepends “Represent the document for retrieval: ” to document inputs.
You can combine multiple related text segments into a single embedding. This is useful for representing complex documents with titles, descriptions, and metadata:
import { createVoyage } from 'voyage-ai-provider';import { embedMany } from 'ai';const voyage = createVoyage({ apiKey: process.env.VOYAGE_API_KEY,});// Use multimodal model for grouped textconst { embeddings } = await embedMany({ model: voyage.multimodalEmbeddingModel('voyage-multimodal-3'), values: [ // E-commerce product: title + description + features [ 'Premium Wireless Bluetooth Headphones', 'Experience superior sound quality with active noise cancellation', 'Battery life: 30 hours, Quick charge: 15 min = 3 hours playback', 'Compatible with iOS, Android, and all Bluetooth devices', ], // Blog post: title + summary + tags [ 'The Future of Artificial Intelligence in Healthcare', 'Exploring how AI is revolutionizing medical diagnosis and treatment', 'Tags: AI, healthcare, machine learning, medical technology, innovation', ], // Job listing: title + company + description [ 'Senior Software Engineer - Full Stack', 'TechCorp Inc. - Leading technology company', 'Build scalable web applications using React, Node.js, and cloud technologies', 'Requirements: 5+ years experience, strong problem-solving skills', ], ],});
Grouping related text segments creates richer semantic representations than embedding them separately.
import { createVoyage } from 'voyage-ai-provider';import { embedMany } from 'ai';const voyage = createVoyage({ apiKey: process.env.VOYAGE_API_KEY,});const { embeddings } = await embedMany({ model: voyage.textEmbeddingModel('voyage-finance-2'), values: [ 'Q3 earnings exceeded analyst expectations with revenue growth of 15%', 'The Federal Reserve announced a 25 basis point interest rate increase', 'Portfolio diversification reduces risk through asset allocation', ],});
Use voyage-law-2 for legal content:
import { createVoyage } from 'voyage-ai-provider';import { embedMany } from 'ai';const voyage = createVoyage({ apiKey: process.env.VOYAGE_API_KEY,});const { embeddings } = await embedMany({ model: voyage.textEmbeddingModel('voyage-law-2'), values: [ 'The plaintiff alleges breach of contract under Section 12 of the agreement', 'Pursuant to Article III, the parties agree to binding arbitration', 'The court granted summary judgment in favor of the defendant', ],});
Whether to truncate input texts to fit within the context length. Defaults to false.Set to true to automatically truncate long texts instead of raising an error.
The embedding response includes token usage information:
import { createVoyage } from 'voyage-ai-provider';import { embedMany } from 'ai';const voyage = createVoyage({ apiKey: process.env.VOYAGE_API_KEY,});const result = await embedMany({ model: voyage.textEmbeddingModel('voyage-3-lite'), values: [ 'First text to embed', 'Second text to embed', 'Third text to embed', ],});console.log(`Generated ${result.embeddings.length} embeddings`);console.log(`Tokens used: ${result.usage?.tokens}`);