PHP code example of teariot / php-yandex-gpt

1. Go to this page and download the library: Download teariot/php-yandex-gpt 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/ */

    

teariot / php-yandex-gpt example snippets




const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function complete(string $message): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $completion = new Completion();
    
    $completion->setModelUri(self::folder_id, 'yandexgpt-lite/latest')
            ->addText([
                [
                    'role' => $completion::USER,
                    'text' => $message,
                ]
            ]);

    $result = $cloud->request($completion);
    return json_decode($result, true);
}



const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function complete(string $message): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $completion = new Completion();
    
    $completion->setModelUri(self::folder_id, 'yandexgpt-lite/latest')
            ->addText([
                [
                    'role' => $completion::USER,
                    'text' => $message,
                ]
            ])
            ->isAsync();
            
    $taskData = $cloud->request($completion);
    $taskData = json_decode($taskData, true);
    
    $operation = new Operation();
    if (!empty($taskData) && isset($taskData['id'])) {
        $operation = $operation->waitAndGet($result['id'])
            ->setTimeOut(240);  // Optional: Sets the timeout for the operation. Default timeout is 120 seconds.
            
        $result = $cloud->request($operation);
        $result = json_decode($result, true);
        return json_decode($result, true);
    }
    return [];
}



const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function complete(string $systemMessage, string $userMessage): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $completion = new Completion();
    
    $completion->setModelUri(self::folder_id, 'yandexgpt-lite/latest')
            ->addText([
                [
                    'role' => $completion::SYSTEM,
                    'text' => $systemMessage,
                ],
                [
                    'role' => $completion::USER,
                    'text' => $message,
                ],
            ]);

    $result = $cloud->request($completion);
    return json_decode($result, true);
}



const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function tokenize(string $message): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $tokenize = new Tokenize($message);
    $tokenize->setModelUri(self::folder_id, 'yandexgpt/latest');
    
    $result = $cloud->request($tokenize);
    return json_decode($result, true);
}



const OAuthToken = 'YOUR_OAUTH_TOKEN';
const folder_id = 'YOUR_FOLDER_ID';

public static function embedding(string $message): array
{
    $cloud = new Cloud(self::OAuthToken, self::folder_id);
    $embedding = new Embedding($message);
    $embedding->setModelUri(self::folder_id, 'text-search-query/latest');
    
    $result = $cloud->request($embedding);
    return json_decode($result, true);
}
bash
composer