Skip to main content

Documentation 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.

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 to Generate::embedding() that returns a float vector you can store or compare.
1
Install the package
2
Add aisdk/voyageai to your project with Composer. It pulls in aisdk/core and aisdk/openai-compatible automatically.
3
composer require aisdk/voyageai
4
Set your API key
5
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:
6
export VOYAGE_API_KEY="pa-..."
7
Alternatively, pass the key inline when creating the provider (useful for one-off scripts or tests — see Configuration for all options):
8
use AiSdk\VoyageAI;

VoyageAI::create(['apiKey' => 'pa-...']);
9
Generate an embedding
10
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.
11
<?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();
12
Access the results
13
The $result object exposes the embedding vector(s) and token usage through a consistent portable contract:
14
// 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 setting VOYAGE_API_KEY:
<?php

declare(strict_types=1);

use AiSdk\Generate;
use AiSdk\VoyageAI;

$result = Generate::embedding([
        'The PHP AI SDK makes embeddings easy.',
        'Voyage AI delivers state-of-the-art retrieval models.',
    ])
    ->model(VoyageAI::model('voyage-4-large'))
    ->dimensions(512)
    ->providerOptions('voyageai', [
        'input_type' => 'document',
        'truncation' => true,
        'output_dtype' => 'float',
    ])
    ->run();

echo 'First vector dimensions: ' . count($result->output->vector) . PHP_EOL;
echo 'Total embeddings: ' . count($result->embeddings) . PHP_EOL;
echo 'Tokens used: ' . $result->usage->totalTokens . PHP_EOL;
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.

Build docs developers (and LLMs) love