PHP code example of easygithdev / phpopenai

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

    

easygithdev / phpopenai example snippets



EasyGithDev\PHPOpenAI\Helpers\ModelEnum;
use EasyGithDev\PHPOpenAI\OpenAIClient;

$apiKey = getenv('OPENAI_API_KEY');

$response = (new OpenAIClient($apiKey))->Completion()->create(
    ModelEnum::TEXT_DAVINCI_003,
    "Say this is a test",
)->toObject();

// Response as stClass object
echo '<pre>', print_r($response, true), '</pre>';


$response = (new OpenAIApi(getenv('OPENAI_API_KEY')))->Completion()->create(
    ModelEnum::TEXT_DAVINCI_003,
    "Say this is a test",
);


$apiKey = getenv('OPENAI_API_KEY');
$org = getenv('OPENAI_API_ORG');

// Passing the organization to the client
$response = (new OpenAIClient($apiKey, $org))


$apiKey = getenv('OPENAI_API_KEY');

// Create a new router, with origine url and version
$route = new OpenAIRoute(
    'https://api.openai.com',
    'v1'
);

// Get a specific Url
echo $route->completionCreate() , '<br>';

// Passing the router to the client
$response = (new OpenAIClient($apiKey))
    ->setRoute($route);



$response = (new OpenAIClient($apiKey))->Completion()->create(
    ModelEnum::TEXT_DAVINCI_003,
    "Say this is a test",
)->toObject();

// Response as a stClass object
echo '<pre>', print_r($response, true), '</pre>';

$response = (new OpenAIClient($apiKey))->Completion()->create(
    ModelEnum::TEXT_DAVINCI_003,
    "Say this is a test",
)->toArray();

// Response as an associative array
echo '<pre>', print_r($response, true), '</pre>';

try {
    $response = (new OpenAIClient('BAD KEY'))
        ->Completion()
        ->create(
            ModelEnum::TEXT_DAVINCI_003,
            "Say this is a test",
        )
        ->toObject();
} catch (Throwable $t) {
    echo nl2br($t->getMessage());
    die;
}


$handler = (new OpenAIClient('BAD KEY'))->Completion()->create(
    ModelEnum::TEXT_DAVINCI_003,
    "Say this is a test",
);

$response = $handler->getResponse();
$contentTypeValidator = $handler->getContentTypeValidator();

if (!(new StatusValidator($response))->validate() or 
    !(new $contentTypeValidator($response))->validate()) {
    echo $response->getBody();
}

$response = (new OpenAIClient($apiKey))->Chat()->create(
    ModelEnum::GPT_3_5_TURBO,
    [
        new ChatMessage(ChatMessage::ROLE_SYSTEM, "You are a helpful assistant."),
        new ChatMessage(ChatMessage::ROLE_USER, "Who won the world series in 2020?"),
        new ChatMessage(ChatMessage::ROLE_ASSISTANT, "The Los Angeles Dodgers won the World Series in 2020."),
        new ChatMessage(ChatMessage::ROLE_USER, "Where was it played?"),
    ]
)->toObject();

$response = (new OpenAIClient($apiKey))->Completion()->create(
    ModelEnum::TEXT_DAVINCI_003,
    "Say this is a test",
)->toObject();


header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

...

(new OpenAIClient($apiKey))->Completion()->create(
    model: "text-davinci-003",
    prompt: "Translate this into 1. French, 2. Spanish and 3. Japanese:\n\nWhat rooms do you have available?\n\n1.",
    temperature: 0.3,
    max_tokens: 100,
    top_p: 1.0,
    frequency_penalty: 0.0,
    presence_penalty: 0.0,
    stream: true
)->getResponse();

$response = (new OpenAIClient($apiKey))->Edit()->create(
    "What day of the wek is it?",
    ModelEnum::TEXT_DAVINCI_EDIT_001,
    "Fix the spelling mistakes",
)->toObject();

function displayUrl($url)
{
    return '<img src="' . $url . '" />';
}

$response = (new OpenAIClient($apiKey))->Image()->create(
    "a rabbit inside a beautiful garden, 32 bit isometric",
    n: 2,
    size: ImageSizeEnum::is256,
)->toObject();

 $response = (new OpenAIClient($apiKey))->Image()->createVariation(
        __DIR__ . '/../../assets/image_variation_original.png',
        n: 2,
        size: ImageSizeEnum::is256
    )->toObject();

 $response = (new OpenAIClient($apiKey))->Image()->createEdit(
        image: __DIR__ . '/../../assets/image_edit_original.png',
        mask: __DIR__ . '/../../assets/image_edit_mask2.png',
        prompt: 'a sunlit indoor lounge area with a pool containing a flamingo',
        size: ImageSizeEnum::is512,
    )->toObject();

$response = (new OpenAIClient($apiKey))->Embedding()->create(
    ModelEnum::TEXT_EMBEDDING_ADA_002,
    "The food was delicious and the waiter...",
)->toObject();

$response = (new OpenAIClient($apiKey))->Audio()
    ->addCurlParam('timeout', 30)
    ->transcription(
        __DIR__ . '/../../assets/openai.mp3',
        ModelEnum::WHISPER_1,
        response_format: AudioResponseEnum::SRT
    )->toObject();

$response = (new OpenAIClient($apiKey))->Audio()
    ->addCurlParam('timeout', 30)
    ->translation(
        __DIR__ . '/../../assets/openai_fr.mp3',
        'whisper-1',
        response_format: AudioResponseEnum::TEXT
    )->toObject();

$response = (new OpenAIClient($apiKey))
    ->Model()
    ->list()
    ->toObject();

$response = (new OpenAIClient($apiKey))
    ->Model()
    ->retrieve('text-davinci-001')
    ->toObject();

$response = (new OpenAIClient($apiKey))
    ->Model()
    ->delete(
        $_POST['model']
    )->toObject();

$response = (new OpenAIApi($apiKey))
    ->File()
    ->list()
    ->toObject();

$response = (new OpenAIApi($apiKey))
    ->File()
    ->create(
        __DIR__ . '/../../assets/mydata.jsonl',
        'fine-tune',
    )
    ->toObject();

$response = (new OpenAIApi($apiKey))
    ->File()
    ->delete('file-xxxx')
    ->toObject();

$response = (new OpenAIApi($apiKey))
    ->File()
    ->retrieve('file-xxxx')
    ->toObject();

$response = (new OpenAIApi($apiKey))
    ->File()
    ->download('file-xxxx')
    ->toObject();

$response = (new OpenAIApi($apiKey))
    ->FineTune()
    ->list()
    ->toObject();

 $response = (new OpenAIApi($apiKey))
        ->FineTune()
        ->create(
            'file-xxxx'
        )
        ->toObject();

$response = (new OpenAIApi($apiKey))
    ->FineTune()
    ->retrieve('ft-xxx')
    ->toObject();

$response = (new OpenAIApi($apiKey))
    ->FineTune()
    ->listEvents('ft-xxx')
    ->toObject();

$response = (new OpenAIApi($apiKey))
    ->FineTune()
    ->Cancel('ft-xxx')
    ->toObject();

$response = (new OpenAIClient($apiKey))
    ->Moderation()
    ->create('I want to kill them.')
    ->toObject();
bash
composer 
html
 foreach ($response->data as $image) : 
bash
composer test tests/[NAME]Test.php