This guide walks you through everything you need to generate your first text embedding with Voyage AI in a PHP project. By the end you will have the package installed, your API key configured, and a working call toDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/voyageai/llms.txt
Use this file to discover all available pages before exploring further.
Generate::embedding() that returns a float vector you can store or compare.
Add
aisdk/voyageai to your project with Composer. It pulls in aisdk/core and aisdk/openai-compatible automatically.The provider reads your key from the
VOYAGE_API_KEY environment variable by default. Export it in your shell or add it to your .env file:Alternatively, pass the key inline when creating the provider (useful for one-off scripts or tests — see Configuration for all options):
Use
Generate::embedding() to embed one or more text inputs. The model() call accepts any Voyage model ID; model IDs pass through unchanged to Voyage’s API. Use providerOptions() to set Voyage-specific fields such as input_type.<?php
declare(strict_types=1);
use AiSdk\Generate;
use AiSdk\VoyageAI;
// Relies on VOYAGE_API_KEY in the environment
$result = Generate::embedding([
'First document',
'Second document',
])
->model(VoyageAI::model('voyage-4-large'))
->dimensions(512)
->providerOptions('voyageai', [
'input_type' => 'document',
'truncation' => true,
])
->run();
The
$result object exposes the embedding vector(s) and token usage through a consistent portable contract:// Float vector for the first input
$vector = $result->output->vector;
// Float vectors for all inputs (indexed by position)
$allVectors = $result->embeddings;
$secondVector = $result->embeddings[1]->vector;
// Token usage
$totalTokens = $result->usage->totalTokens;
$inputTokens = $result->usage->inputTokens;
// Provider metadata (e.g. the model name echoed back by Voyage)
$model = $result->providerMetadata['voyageai']['model'];
Full Working Example
The snippet below combines every step into a self-contained script you can run immediately after settingVOYAGE_API_KEY:
Float vectors only. The PHP AI SDK result contract is a float vector, so this adapter accepts
output_dtype: float exclusively. Passing output_dtype: binary, output_dtype: int8, or encoding_format: base64 will throw an InvalidArgumentException before any HTTP request is made. If you need quantized output, it requires a separate result contract.