PHP code example of capevace / laravel-gpt

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

    

capevace / laravel-gpt example snippets


use Capevace\GPT\Facades\GPT;

$response = GPT::generate(
    'Name a thing that is blue.',
    model: 'text-davinci-003',
    maxTokens: 400,
    frequencyPenalty: 1.0,
);

echo $response->first(); // "The sky"

# Access via injection

use Capevace\GPT\GPTService;

class MyController extends Controller {
    protected GPTService $gpt;

    public function __construct(GPTService $gpt) {
        $this->gpt = $gpt;
    }

    public function index() {
        $this->gpt->generate(
            // ..
        );
    }

}

# Access via Facade

use Capevace\GPT\Facades\GPT;

GPT::generate(/* .. */);

use Capevace\GPT\Facades\GPT;

$response = GPT::generate(
    'Generate a list of things that are blue.',
    model: 'text-davinci-003',
    maxTokens: 400,
    frequencyPenalty: 1.0,
);

use Capevace\GPT\Facades\GPT;

$response = GPT::generate(
    'Name a thing that is blue.',
    model: 'text-davinci-003',
    maxTokens: 400,
    frequencyPenalty: 1.0,
);

$firstChoice = $response->first(); // "the sky"

$allChoices = $response->all(); // ["the sky", "the ocean" ...]

use Capevace\GPT\Facades\GPT;
use Capevace\GPT\Support\GPTException;
use Exception;

try {
    $response = GPT::generate('Do nothing.');
} catch (GPTException $e) {
    // Exception will be thrown, as the response text is ""
} catch (Exception $e) {
    // Catch connectivity errors etc.
}
bash
php artisan vendor:publish --tag="laravel-gpt-config"