PHP code example of sgraaf / openai-php

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

    

sgraaf / openai-php example snippets


// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// lists the currently available models
var_dump($client->listModels());

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// retrieves a model instance
var_dump($client->retrieveModel(model: 'text-davinci-003'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a completion for the provided prompt and parameters
var_dump($client->createCompletion(model: 'text-davinci-003', prompt: 'Say this is a test', max_tokens: 7, temperature: 0));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a completion for the chat message
var_dump($client->createChatCompletion(model: 'gpt-3.5-turbo', messages: [['role' => 'user', 'content' => 'Hello!']]));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a new edit for the provided input, instruction, and parameters
var_dump($client->createEdit(model: 'text-davinci-003', input: 'What day of the wek is it?', instruction: 'Fix the spelling mistakes'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates an image given a prompt
var_dump($client->createImage(prompt: 'A cute baby sea otter', n: 2, size: '1024x1024'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates an edited or extended image given an original image and a prompt
var_dump($client->createImageEdit(image: 'otter.png', mask: 'mask.png', prompt: 'A cute baby sea otter wearing a beret', n: 2, size: '1024x1024'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a variation of a given image
var_dump($client->createImageVariation(image: 'otter.png', n: 2, size: '1024x1024'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates an embedding vector representing the input text
var_dump($client->createEmbedding(model: 'text-embedding-ada-002', input: 'The food was delicious and the waiter...'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// transcribes audio into the input language
var_dump($client->createTranscription(file: 'audio.mp3', model: 'whisper-1'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// translates audio into english
var_dump($client->createTranslation(file: 'german.m4a', model: 'whisper-1'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// returns a list of files that belong to the user's organization
var_dump($client->listFiles());

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// upload a file that contains document(s) to be used across various endpoints/features
var_dump($client->createFile(file: 'mydata.jsonl', purpose: 'fine-tune'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// delete a file
var_dump($client->deleteFile(file_id: 'file-XjGxS3KTG0uNmNOK362iJua3'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// returns information about a specific file
var_dump($client->retrieveFile(file_id: 'file-XjGxS3KTG0uNmNOK362iJua3'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// returns the contents of the specified file
var_dump($client->downloadFile(file_id: 'file-XjGxS3KTG0uNmNOK362iJua3'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// creates a job that fine-tunes a specified model from a given dataset
var_dump($client->createFineTune(training_file: 'file-XGinujblHPwGLSztz8cPS8XY'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// list your organization's fine-tuning jobs

var_dump($client->listFineTunes());

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// gets info about the fine-tune job
var_dump($client->retrieveFineTune(fine_tune_id: 'ft-AF1WoRqd3aJAHsqc9NY7iL8F'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// immediately cancel a fine-tune job
var_dump($client->cancelFineTune(fine_tune_id: 'ft-AF1WoRqd3aJAHsqc9NY7iL8F'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// get fine-grained status updates for a fine-tune job.

var_dump($client->listFineTuneEvents(fine_tune_id: 'ft-AF1WoRqd3aJAHsqc9NY7iL8F'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// delete a fine-tuned model
var_dump($client->deleteModel(model: 'curie:ft-acmeco-2021-03-03-21-44-20'));

// initialize the client
$client = new OpenAI\Client('YOUR_OPENAI_API_KEY');

// classifies if text violates openai's content policy
var_dump($client->createModeration(input: 'I want to kill them.'));
bash
composer