OpenAI PHP is a supercharged PHP API client that allows you to interact with the Open AI API.
This project is a work-in-progress. Code and documentation are currently under development and are subject to change.
Requires PHP 8.1+
First, install OpenAI via the Composer package manager:
composer require openai-php/client dev-main
Then, interact with OpenAI's API:
$client = OpenAI::client('YOUR_API_KEY');
$result = $client->completions()->create([
'model' => 'davinci',
'prompt' => 'PHP is',
]);
echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.
- Models
- Completions
- Edits
- Embeddings
- Files
- FineTunes
- Moderations
- Classifications
Lists the currently available models, and provides basic information about each one such as the owner and availability.
$client->models()->list(); // ['data' => [...], ...]
Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
$client->models()->retrieve($model); // ['id' => 'text-davinci-002', ...]
Creates a completion for the provided prompt and parameters.
$client->completions()->create($parameters); // ['choices' => [...], ...]
Creates a new edit for the provided input, instruction, and parameters.
$client->edits()->create(); // ['choices' => [...], ...]
Creates an embedding vector representing the input text.
$client->embeddings()->create(); // ['data' => [...], ...]
Returns a list of files that belong to the user's organization.
$client->files()->list(); // ['data' => [...], ...]
Delete a file.
$client->files()->delete($file); // ['id' => 'file-XjGxS3KTG0uNmNOK362iJua3', ...]
Returns information about a specific file.
$client->files()->retrieve($file); // ['id' => 'file-XjGxS3KTG0uNmNOK362iJua3', ...]
Returns the contents of the specified file.
$client->files()->download($file); // '{"prompt": "<prompt text>", ...'
OpenAI PHP is an open-sourced software licensed under the MIT license.