Voyage AI Provider
The Voyage AI provider contains embedding and reranking model support for the Voyage AI API.
Setup
The Voyage AI provider is available in the @ai-sdk/voyage module. You can install it with
pnpm add @ai-sdk/voyage
Provider Instance
You can import the default provider instance voyage from @ai-sdk/voyage:
import { voyage } from '@ai-sdk/voyage';If you need a customized setup, you can import createVoyage from @ai-sdk/voyage
and create a provider instance with your settings:
import { createVoyage } from '@ai-sdk/voyage';
const voyage = createVoyage({ // custom settings});You can use the following optional settings to customize the Voyage AI provider instance:
-
baseURL string
Use a different URL prefix for API calls, e.g. to use proxy servers. The default prefix is
https://api.voyageai.com/v1. -
apiKey string
API key that is being sent using the
Authorizationheader. It defaults to theVOYAGE_API_KEYenvironment variable. -
headers Record<string,string>
Custom headers to include in the requests.
-
fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>
Custom fetch implementation. Defaults to the global
fetchfunction. You can use it as a middleware to intercept requests, or to provide a custom fetch implementation for e.g. testing.
Embedding Models
You can create models that call the Voyage AI embeddings API
using the .embedding() factory method.
const model = voyage.embedding('voyage-3.5');You can use Voyage AI embedding models to generate embeddings with the embed function:
import { voyage } from '@ai-sdk/voyage';import { embed } from 'ai';
const { embedding } = await embed({ model: voyage.embedding('voyage-3.5'), value: 'sunny day at the beach',});Voyage AI embedding models support additional provider options that can be passed via providerOptions.voyage:
import { voyage, type VoyageEmbeddingModelOptions } from '@ai-sdk/voyage';import { embed } from 'ai';
const { embedding } = await embed({ model: voyage.embedding('voyage-4-large'), value: 'sunny day at the beach', providerOptions: { voyage: { inputType: 'document', outputDimension: 512, } satisfies VoyageEmbeddingModelOptions, },});The following provider options are available:
-
inputType 'query' | 'document' | null
Specifies the type of input passed to the model. Defaults to
null.null: The embedding model directly converts the inputs into numerical vectors.'query': Used for embeddings of search queries run against a vector DB to find relevant documents.'document': Used for embeddings stored in a vector database for search use-cases.
-
truncation boolean
Whether to truncate the input texts to fit within the model's context length. Defaults to
true. -
outputDimension number
The number of dimensions for the resulting output embeddings. Supported by models with flexible dimensions (e.g.
voyage-4-large,voyage-4,voyage-4-lite,voyage-4-nano,voyage-code-3.5,voyage-code-3,voyage-3-large,voyage-3.5,voyage-3.5-lite). Supported values: 256, 512, 1024, 2048. -
outputDtype 'float' | 'int8' | 'uint8' | 'binary' | 'ubinary'
The data type for the output embeddings. Defaults to
'float'.'float': 32-bit floating-point numbers.'int8','uint8': 8-bit integer types.'binary','ubinary': Bit-packed, quantized single-bit embedding values.
Model Capabilities
| Model | Default Dimensions | Context Length |
|---|---|---|
voyage-4-large | 1024 | 32,000 |
voyage-4 | 1024 | 32,000 |
voyage-4-lite | 1024 | 32,000 |
voyage-4-nano | 1024 | 32,000 |
voyage-code-3.5 | 1024 | 32,000 |
voyage-code-3 | 1024 | 32,000 |
voyage-3-large | 1024 | 32,000 |
voyage-3.5 | 1024 | 32,000 |
voyage-3.5-lite | 1024 | 32,000 |
voyage-3 | 1024 | 32,000 |
voyage-3-lite | 512 | 32,000 |
voyage-finance-2 | 1024 | 32,000 |
voyage-multilingual-2 | 1024 | 32,000 |
voyage-law-2 | 1024 | 16,000 |
voyage-code-2 | 1536 | 16,000 |
The table above lists popular models. Please see the Voyage AI docs for a full list of available models. You can also pass any available provider model ID as a string if needed.
Reranking Models
You can create models that call the Voyage AI rerank API
using the .reranking() factory method.
const model = voyage.reranking('rerank-2.5');You can use Voyage AI reranking models to rerank documents with the rerank function:
import { voyage } from '@ai-sdk/voyage';import { rerank } from 'ai';
const documents = [ 'sunny day at the beach', 'rainy afternoon in the city', 'snowy night in the mountains',];
const { ranking } = await rerank({ model: voyage.reranking('rerank-2.5'), documents, query: 'talk about rain', topN: 2,});
console.log(ranking);Voyage AI reranking models support additional provider options that can be passed via providerOptions.voyage:
import { voyage, type VoyageRerankingModelOptions } from '@ai-sdk/voyage';import { rerank } from 'ai';
const { ranking } = await rerank({ model: voyage.reranking('rerank-2.5'), documents: ['sunny day at the beach', 'rainy afternoon in the city'], query: 'talk about rain', providerOptions: { voyage: { returnDocuments: true, truncation: true, } satisfies VoyageRerankingModelOptions, },});The following provider options are available:
-
returnDocuments boolean
Whether to return the documents in the response. Defaults to
false. -
truncation boolean
Whether to truncate the input texts to fit within the model's context length. Defaults to
true.
Model Capabilities
| Model | Context Length |
|---|---|
rerank-2.5 | 32,000 |
rerank-2.5-lite | 32,000 |
rerank-2 | 16,000 |
rerank-2-lite | 8,000 |
rerank-1 | 8,000 |
rerank-lite-1 | 4,000 |