sgraaf/openai-php

A thin PHP wrapper for the OpenAI API built upon libcurl.

0.1.6 2023-03-07 10:11 UTC

This package is auto-updated.

Last update: 2024-06-03 22:50:55 UTC


README

Packagist Version Packagist PHP Version pre-commit.ci status Packagist License

A thin PHP wrapper for the OpenAI API built upon libcurl.

Installation

You can install OpenAI PHP via Composer:

composer require sgraaf/openai-php

Usage

Models

List models

Lists the currently available models, and provides basic information about each one such as the owner and availability. See the OpenAI docs for more information.

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

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

Retrieve model

Retrieves a model instance, providing basic information about the model such as the owner and permissioning. See the OpenAI docs for more information.

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

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

Completions

Create completion

Creates a completion for the provided prompt and parameters. See the OpenAI docs for more information.

// 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));

Chat

Create chat completion

Creates a completion for the chat message. See the OpenAI docs for more information.

// 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!']]));

Edits

Create edit

Creates a new edit for the provided input, instruction, and parameters. See the OpenAI docs for more information.

// 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'));

Images

Create image

Creates an image given a prompt. See the OpenAI docs for more information.

// 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'));

Create image edit

Creates an edited or extended image given an original image and a prompt. See the OpenAI docs for more information.

// 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'));

Create image variation

Creates a variation of a given image. See the OpenAI docs for more information.

// 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'));

Embeddings

Create embeddings

Creates an embedding vector representing the input text. See the OpenAI docs for more information.

// 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...'));

Audio

Create transcription

Transcribes audio into the input language. See the OpenAI docs for more information.

// 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'));

Create translation

Translates audio into English. See the OpenAI docs for more information.

// 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'));

Files

List files

Returns a list of files that belong to the user's organization. See the OpenAI docs for more information.

// 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());

Upload file

Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact OpenAI if you need to increase the storage limit. See the OpenAI docs for more information.

// 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'));

Delete file

Delete a file. See the OpenAI docs for more information.

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

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

Retrieve file

Returns information about a specific file. See the OpenAI docs for more information.

// 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'));

Retrieve file content

Returns the contents of the specified file. See the OpenAI docs for more information.

// 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'));

Fine-Tunes

Create fine-tune

Creates a job that fine-tunes a specified model from a given dataset.

Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.

See the OpenAI docs for more information.

// 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'));

List fine-tunes

List your organization's fine-tuning jobs. See the OpenAI docs for more information.

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

// list your organization's fine-tuning jobs

var_dump($client->listFineTunes());

Retrieve fine-tune

Gets info about the fine-tune job. See the OpenAI docs for more information.

// 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'));

Cancel fine-tune

Immediately cancel a fine-tune job. See the OpenAI docs for more information.

// 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'));

List fine-tune events

Get fine-grained status updates for a fine-tune job. See the OpenAI docs for more information.

// 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'));

Delete fine-tune model

Delete a fine-tuned model. You must have the Owner role in your organization. See the OpenAI docs for more information.

// 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'));

Moderations

Create moderation

Classifies if text violates OpenAI's Content Policy. See the OpenAI docs for more information.

// 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.'));