PHP code example of aisdk / groq

1. Go to this page and download the library: Download aisdk/groq 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/ */

    

aisdk / groq example snippets


use AiSdk\Generate;
use AiSdk\Groq;

$result = Generate::text()
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->instructions('Write short, clear answers.')
    ->prompt('Explain closures in PHP.')
    ->run();

echo $result->text;

Generate::model(Groq::model('llama-3.3-70b-versatile'));

$result = Generate::text('Explain closures in PHP.')->run();

$provider = Groq::create([
    'apiKey' => 'gsk-...',
    'baseUrl' => 'https://api.groq.com/openai/v1',
    'headers' => ['X-Custom-Header' => 'value'],
]);

use AiSdk\Generate;
use AiSdk\Groq;

$stream = Generate::text('Tell me a story.')
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->stream();

foreach ($stream->chunks() as $chunk) {
    echo $chunk;
}

$result = $stream->run();

use AiSdk\Generate;
use AiSdk\Groq;

$result = Generate::embedding(['Search query', 'Document text'])
    ->model(Groq::model('nomic-embed-text-v1_5'))
    ->providerOptions('groq', ['user' => 'user-123'])
    ->run();

$queryVector = $result->embeddings[0]->vector;
$documentVector = $result->embeddings[1]->vector;

use AiSdk\Generate;
use AiSdk\Groq;

$result = Generate::speech()
    ->model(Groq::model('canopylabs/orpheus-v1-english'))
    ->input('Welcome to Orpheus text-to-speech. [cheerful] This is expressive Groq audio generation.')
    ->voice('austin')
    ->format('wav')
    ->run();

$result->output->save(__DIR__.'/orpheus.wav');

use AiSdk\Content;
use AiSdk\Generate;
use AiSdk\Groq;

$result = Generate::transcription(Content::audio(__DIR__.'/meeting.mp3'))
    ->model(Groq::model('whisper-large-v3-turbo'))
    ->run();

echo $result->output->text;

use AiSdk\Generate;
use AiSdk\Groq;
use AiSdk\Schema;

$result = Generate::text()
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->prompt('Extract the city and country from: Lahore, Pakistan.')
    ->output(Schema::object(
        name: 'address',
        properties: [
            Schema::string(name: 'city')->

use AiSdk\Generate;
use AiSdk\Groq;
use AiSdk\Schema;
use AiSdk\Tool;

$weather = Tool::make('weather', 'Get current weather')
    ->input(Schema::string(name: 'city')->ather in Lahore?')
    ->tool($weather)
    ->run();

$result = Generate::text('Hello')
    ->model(Groq::model('llama-3.3-70b-versatile'))
    ->providerOptions('groq', [
        'raw' => ['top_k' => 40],
    ])
    ->run();