PHP code example of webboy / open-ai-api-client

1. Go to this page and download the library: Download webboy/open-ai-api-client 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/ */

    

webboy / open-ai-api-client example snippets




use Dotenv\Dotenv;
use Webboy\OpenAiApiClient\Endpoints\OpenAICompletions;
use Webboy\OpenAiApiClient\Exceptions\OpenAIClientException;

$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->load();

$apiKey = $_ENV['OPENAI_API_KEY'];

$client = new OpenAICompletions($apiKey);

$options['model']   = 'text-davinci-003';
$options['prompt']  = 'What time is it?';

try {
    print_r($client->create($options));
} catch (OpenAIClientException $exception) {
    die('OpenAI error occured: '.$exception->getMessage());
}

./vendor/bin/phpunit