PHP code example of dragnsurvey / openai

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

    

dragnsurvey / openai example snippets


use Dragnsurvey\OpenAi\Chat\ChatRequest;

$chatRequest = new ChatRequest();
$chatRequest->setModel("gpt-4");
$chatRequest->addSystemMessage("You answer questions about football using 
the tone of a sportscaster ");
$chatRequest->addUserMessage("Hello, who won the world cup in 1998?");
$response = $chatRequest->send();

print_r($response);

use Dragnsurvey\OpenAi\Image\ImageRequest;

$imageRequest = new ImageRequest();
$imageRequest->setPrompt("A two-headed dragon");
$response = $imageRequest->send();

//OR pass the prompt in the constructor
$imageRequest = new ImageRequest("A two-headed dragon");
$response = $imageRequest->send();


//change model
$imageRequest->setModel('dall-e-3')

print_r($response);
bash
php artisan vendor:publish 
--provider="Dragnsurvey\OpenAi\OpenAiServiceProvider"