PHP code example of rmh / vertex

1. Go to this page and download the library: Download rmh/vertex library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rmh / vertex example snippets


'vertex' => [
    'project_id'  => env('VERTEX_PROJECT_ID'),
    'location'    => env('VERTEX_LOCATION', 'us-central1'),
    // Auth - choose one (or omit both to use Application Default Credentials):
    'credentials' => env('VERTEX_CREDENTIALS'), // path to service-account.json
    // 'api_key' => env('VERTEX_API_KEY'),
],

'vertex' => [
    'api_key' => env('VERTEX_API_KEY'),
    // project_id and location omitted — triggers Express mode
],

use Prism\Prism\Prism;
use Prism\Vertex\Enums\Vertex;

// Google Gemini
$response = Prism::text()
    ->using(Vertex::Gemini, 'gemini-2.5-flash')
    ->withPrompt('Explain quantum computing in simple terms')
    ->asText();

echo $response->text;

// Anthropic Claude on Vertex AI
$response = Prism::text()
    ->using(Vertex::Anthropic, 'claude-3-5-sonnet@20241022')
    ->withPrompt('Explain quantum computing in simple terms')
    ->asText();

// Meta Llama on Vertex AI
$response = Prism::text()
    ->using(Vertex::Meta, 'llama-4-scout-17b-16e-instruct-maas')
    ->withPrompt('Explain quantum computing in simple terms')
    ->asText();

use Prism\Prism\Prism;
use Prism\Vertex\Enums\Vertex;
use Prism\Prism\Schema\ObjectSchema;
use Prism\Prism\Schema\StringSchema;
use Prism\Prism\Schema\ArraySchema;

$schema = new ObjectSchema(
    name: 'languages',
    description: 'Top programming languages',
    properties: [
        new ArraySchema(
            'languages',
            'List of programming languages',
            items: new ObjectSchema(
                name: 'language',
                description: 'Programming language details',
                properties: [
                    new StringSchema('name', 'The language name'),
                    new StringSchema('popularity', 'Popularity description'),
                ]
            )
        )
    ]
);

$response = Prism::structured()
    ->using(Vertex::Gemini, 'gemini-2.5-flash')
    ->withSchema($schema)
    ->withPrompt('List the top 3 programming languages')
    ->asStructured();

$data = $response->structured;

use Prism\Prism\Prism;
use Prism\Vertex\Enums\Vertex;

$response = Prism::embeddings()
    ->using(Vertex::Gemini, 'text-embedding-005')
    ->fromInput('The sky is blue')
    ->asEmbeddings();

$embeddings = $response->embeddings;

use Prism\Vertex\Enums\VertexSchema;

$response = Prism::text()
    ->using(Vertex::Gemini, 'some-model')
    ->withProviderOptions(['apiSchema' => VertexSchema::Anthropic])
    ->withPrompt('Hello')
    ->asText();

Prism::structured()
    ->using(Vertex::Anthropic, 'claude-3-5-sonnet@20241022')
    ->withSchema($schema)
    ->withProviderOptions([
        'jsonModeMessage' => 'My custom JSON instruction message',
    ])
    ->withPrompt('My prompt')
    ->asStructured();

'vertex' => [
    'project_id' => env('VERTEX_PROJECT_ID'),
    'location'   => env('VERTEX_LOCATION', 'us-central1'),
    // no api_key or credentials - ADC handles auth automatically
],

'vertex' => [
    'project_id'  => env('VERTEX_PROJECT_ID'),
    'location'    => env('VERTEX_LOCATION', 'us-central1'),
    'credentials' => env('VERTEX_CREDENTIALS'),
],

// Override for Anthropic only (e.g. different region)
'vertex-anthropic' => [
    'location' => 'europe-west1',
],