PHP code example of naiuz / sdk

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

    

naiuz / sdk example snippets


use Naiuz\NeuronAI;

$client = new NeuronAI();

$speech = $client->tts->synthesize(['text' => 'Assalomu alaykum!', 'voice_id' => 'kamron', 'language' => 'uz']);
$speech->save('salom.wav');
echo "{$speech->character_count} characters, {$speech->cost} credits\n";

$answer = $client->chat->completions->create([
    'model' => 'gemma-4-26b-a4b',
    'messages' => [['role' => 'user', 'content' => 'Salom! Bugun ob-havo qanday?']],
]);
echo $answer->choices[0]->message->content;

use Naiuz\NeuronAI;

$client = new NeuronAI([
    'api_key' => 'nai_...', // else NEURONAI_API_KEY
    'base_url' => 'https://my.neuronai.uz/api/v1', // else NEURONAI_BASE_URL, else this address
    'timeout' => 300, // seconds each attempt may take: 5 minutes by default
    'max_retries' => 2, // retries of a failed attempt: 2 by default
    'default_headers' => ['x-app' => 'shop'], // sent with every call
]);

$speech = $client->tts->synthesize(['text' => 'Xush kelibsiz!', 'voice_id' => 'kamron', 'quality' => 'high']);
$speech->save('welcome.wav');

$dialogue = $client->tts->dialogue([
    'turns' => [
        ['voice_id' => 'voice-a', 'text' => 'Assalomu alaykum!'],
        ['voice_id' => 'voice-b', 'text' => 'Va alaykum assalom!'],
    ],
    'gap_ms' => 300,
]);
foreach ($dialogue->turns as $turn) {
    echo "{$turn->index} {$turn->voice_id} {$turn->start_s} {$turn->end_s}\n";
}
echo $dialogue->turn_count;

use Naiuz\Exceptions\WaitTimeoutException;

$longText = str_repeat("Bir bor ekan, bir yo'q ekan. ", 100);
try {
    $job = $client->tts->jobs->createAndWait(
        ['text' => $longText, 'voice_id' => 'kamron'],
        ['idempotency_key' => 'chapter-7', 'poll_interval' => 2, 'timeout' => 600],
    );
    if ($job->status === 'succeeded') {
        $client->tts->jobs->audio($job->id)->save('chapter.wav');
    } elseif ($job->error !== null) {
        echo "{$job->error->code}: {$job->error->message}\n";
    }
} catch (WaitTimeoutException $error) {
    // The wait ran out first: the job may still finish, so fetch its audio later.
    echo "Still {$error->job->status}: {$error->job->id}\n";
}

$voice = $client->voices->create([
    'name' => 'Office voice',
    'language' => 'uz',
    'ref_audio' => 'sample.wav', // a path
    'ref_text' => 'Salom, men sizga yordam beraman.',
    'tags' => ['support', 'calm'],
]);

$stream = fopen('call.mp3', 'rb');
if ($stream === false) {
    exit("call.mp3 can't be opened.\n");
}
$transcription = $client->stt->transcribe([
    'file' => ['stream' => $stream, 'filename' => 'call.mp3'], // a stream, with the filename to send it under
    'language' => 'uz',
]);
echo "{$transcription->text} ({$transcription->duration_seconds} s)\n";

$stream = $client->chat->completions->create([
    'model' => 'gemma-4-26b-a4b',
    'messages' => [['role' => 'user', 'content' => 'Toshkent haqida qisqacha gapirib ber.']],
    'stream' => true,
]);
foreach ($stream as $chunk) {
    echo $chunk->choices[0]->delta->content ?? '';
    if ($chunk->usage !== null) {
        echo "\n{$chunk->usage->total_tokens} tokens\n";
    }
}

$embeddings = $client->embeddings->create(['model' => 'bge-m3', 'input' => ['Salom', 'Rahmat']]);
echo count($embeddings->data[0]->embedding), " dimensions, {$embeddings->cost} credits\n";

$ranked = $client->rerank->create(['model' => 'bge-reranker-v2-m3', 'query' => 'ob-havo', 'documents' => ['Bugun quyoshli.', 'Narxlar oshdi.'], 'top_n' => 1]);
echo "{$ranked->results[0]->index} {$ranked->results[0]->relevance_score}\n";

foreach ($client->voices->list(['type' => 'custom', 'limit' => 50]) as $voice) {
    echo "{$voice->id} {$voice->name}\n";
}

$page = $client->apiKeys->list(['limit' => 10]);
if ($page->hasNextPage()) {
    echo count($page->nextPage()->data), "\n";
}

$raw = $client->withRawResponse()->tts->jobs->create(['text' => 'Salom']);
echo $raw->status, ' ', $raw->headers['x-request-id'] ?? '', ' ', $raw->data->id, "\n";

use Naiuz\ErrorCode;
use Naiuz\Exceptions\InsufficientQuotaException;
use Naiuz\Exceptions\RateLimitException;

try {
    $client->tts->synthesize(['text' => 'Salom', 'voice_id' => 'kamron']);
} catch (InsufficientQuotaException $error) {
    if ($error->error_code === ErrorCode::InsufficientBalance->value) {
        echo "Top up your balance.\n";
    }
} catch (RateLimitException $error) {
    echo 'Try again in ', $error->retry_after ?? 1, " s.\n";
}