PHP code example of ruscoe / openai-php

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

    

ruscoe / openai-php example snippets




ee https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAICompletions($api_key);

$parameters = [
    'max_tokens' => 128,
];

$response = $api->create('gpt-3.5-turbo', 'Describe a keyboard', 1, $parameters);

var_dump($response);



ee https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIChat($api_key);

$messages = [
    (object) ['role' => 'user', 'content' => 'Hello, friend!'],
];

$response = $api->create('gpt-3.5-turbo', $messages);

var_dump($response);



ee https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIImages($api_key);

$response = $api->createAsURL('A red six-sided dice in water', 2, '256x256');



ee https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIImages($api_key);

$response = $api->createVariationAsURL('variation.png', 1, '256x256');



ee https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIImages($api_key);

$response = $api->createEditAsURL('edit.png', 'a duck driving a car', 'edit_mask.png', 1, '256x256');



ee https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIFiles($api_key);

$file = $api->uploadFile('training.jsonl');

$api = new OpenAI\OpenAIFiles($api_key);

$files = $api->getFiles();

var_dump($files);

$api = new OpenAI\OpenAIFineTunes($api_key);

$api->create('file-1CO...');

$api = new OpenAI\OpenAIModels($api_key);

$models = $api->getModels();

var_dump($models);

$api = new OpenAI\OpenAICompletions($api_key);

$response = $api->create('curie:ft...', 'dog =', 1, $parameters);

$api = new OpenAI\OpenAICompletions($api_key);

$response = $api->create('curie:ft...', 'marble =', 1, $parameters);