This PHP library provides a simple wrapper for the OpenAI API, allowing you to easily integrate the OpenAI API into your PHP projects.
You can install the library via Composer:
composer require softcreatr/php-openai-sdkFirst, include the library in your project:
<?php
require_once 'vendor/autoload.php';Then, create an instance of the OpenAI class with your API key, organization (optional), an HTTP client, an HTTP request factory, and an HTTP stream factory:
use SoftCreatROpenAIOpenAI;
$apiKey = 'your_api_key';
$organization = 'your_organization_id'; // optional
// Replace these lines with your chosen PSR-17 and PSR-18 compatible HTTP client and factories
$httpClient = new YourChosenHttpClient();
$requestFactory = new YourChosenRequestFactory();
$streamFactory = new YourChosenStreamFactory();
$uriFactory = new YourChosenUriFactory();
$openAI = new OpenAI($requestFactory, $streamFactory, $uriFactory, $httpClient, $apiKey, $organization);Now you can call any supported OpenAI API endpoint using the magic method __call:
$response = $openAI->createChatCompletion([
'model' => 'gpt-4',
'messages' => [
['role' => 'system', 'content' => 'You are a helpful assistant.'],
['role' => 'user', 'content' => 'Hello!'],
],
]);
// Process the API response
if ($response->getStatusCode() === 200) {
$responseObj = json_decode($response->getBody()->getContents(), true);
print_r($responseObj);
} else {
echo "Error: " . $response->getStatusCode();
}You can enable real-time streaming for chat completions:
$streamCallback = static function ($data) {
if (isset($data['choices'][0]['delta']['content'])) {
echo $data['choices'][0]['delta']['content'];
}
};
$openAI->createChatCompletion(
[
'model' => 'gpt-4',
'messages' => [
[
'role' => 'user',
'content' => 'Tell me a story about a brave knight.',
],
],
'stream' => true,
],
$streamCallback
);For more details on how to use each endpoint, refer to the OpenAI API documentation, and the examples provided in the repository.
Below is a list of supported methods organized by category. Each method links to its corresponding OpenAI API documentation and includes a link to an example in this repository.
createTranscription(array $options = [])createTranslation(array $options = [])createSpeech(array $options = [])createChatCompletion(array $options = [])createEmbedding(array $options = [])createFineTuningJob(array $options = [])listFineTuningJobs()retrieveFineTuningJob(array $parameters)cancelFineTuning(array $parameters)listFineTuningEvents(array $parameters, array $options = [])createBatch(array $options = [])retrieveBatch(array $parameters)cancelBatch(array $parameters)listBatches()listFiles()uploadFile(array $options = [])deleteFile(array $parameters)retrieveFile(array $parameters)retrieveFileContent(array $parameters)createUpload(array $options = [])addUploadPart(array $parameters, array $options = [])completeUpload(array $parameters)cancelUpload(array $parameters)createImage(array $options = [])createImageEdit(array $options = [])createImageVariation(array $options = [])listModels()retrieveModel(array $parameters)deleteModel(array $parameters)createModeration(array $options = [])createAssistant(array $options = [])listAssistants()retrieveAssistant(array $parameters)modifyAssistant(array $parameters, array $options = [])deleteAssistant(array $parameters)createThread(array $options = [])retrieveThread(array $parameters)modifyThread(array $parameters, array $options = [])deleteThread(array $parameters)createMessage(array $parameters, array $options = [])listMessages(array $parameters)retrieveMessage(array $parameters)modifyMessage(array $parameters, array $options = [])deleteMessage(array $parameters)createRun(array $parameters, array $options = [])createThreadAndRun(array $options = [])listRuns(array $parameters)retrieveRun(array $parameters)modifyRun(array $parameters, array $options = [])submitToolOutputsToRun(array $parameters, array $options = [])cancelRun(array $parameters)listRunSteps(array $parameters)retrieveRunStep(array $parameters)createVectorStore(array $options = [])listVectorStores()retrieveVectorStore(array $parameters)modifyVectorStore(array $parameters, array $options = [])deleteVectorStore(array $parameters)createVectorStoreFile(array $parameters, array $options = [])listVectorStoreFiles(array $parameters)retrieveVectorStoreFile(array $parameters)deleteVectorStoreFile(array $parameters)createVectorStoreFileBatch(array $parameters, array $options = [])retrieveVectorStoreFileBatch(array $parameters)cancelVectorStoreFileBatch(array $parameters)listVectorStoreFilesInBatch(array $parameters)listInvites()createInvite(array $options = [])retrieveInvite(array $parameters)deleteInvite(array $parameters)listUsers()modifyUser(array $parameters, array $options = [])retrieveUser(array $parameters)deleteUser(array $parameters)listProjects()createProject(array $options = [])retrieveProject(array $parameters)modifyProject(array $parameters, array $options = [])archiveProject(array $parameters)listProjectUsers(array $parameters)createProjectUser(array $parameters, array $options = [])retrieveProjectUser(array $parameters)modifyProjectUser(array $parameters, array $options = [])deleteProjectUser(array $parameters)listProjectServiceAccounts(array $parameters)createProjectServiceAccount(array $parameters, array $options = [])retrieveProjectServiceAccount(array $parameters)deleteProjectServiceAccount(array $parameters)listProjectApiKeys(array $parameters)retrieveProjectApiKey(array $parameters)deleteProjectApiKey(array $parameters)listAuditLogs(array $options = [])For a detailed list of changes and updates, please refer to the CHANGELOG.md file. We adhere to Semantic Versioning and document notable changes for each release.
Streaming is now supported for real-time token generation in chat completions. Please make sure you are handling streams correctly using a callback, as demonstrated in the examples.
This library is licensed under the ISC License. See the LICENSE file for more information.
|
Sascha Greuel |