PHP code example of stuarttodd / openai-php-client

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

    

stuarttodd / openai-php-client example snippets




use Stuarttodd\OpenAiPhpClient\OpenAI;

$apiKey = "YOUR_API_KEY_HERE";

$response = OpenAI::client($apiKey)
    ->chatCompletions()
    ->setConfig([
        'model' => 'gpt-3.5-turbo', // if not set, defaults to 'gpt-3.5-turbo'
        'temperature' => 0.9, // if not set, defaults to 0.7
        'message' => 'Tell me you love me' // If not set, defaults to giving you a random Chuck Norris joke
    ])
    ->fetch(function($response) { // use of closure is optional
        return json_decode($response)->choices[0]->message->content;
    }); 

echo $response; // "As an AI, I do not have the capability to feel emotions or love. However, I am here to assist you with any questions or tasks you may have."
bash
composer