softcreatr / php-openai-sdk
A powerful and easy-to-use PHP SDK for the OpenAI API, allowing seamless integration of advanced AI-powered features into your PHP projects.
Fund package maintenance!
softcreatr
ecologi.com/softcreatr?r=61212ab3fc69b8eb8a2014f4
Requires
- php: >=7.4
- ext-fileinfo: *
- ext-json: *
- ext-mbstring: *
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
README
This PHP library provides a simple wrapper for the OpenAI API, allowing you to easily integrate the OpenAI API into your PHP projects.
Features
- Easy integration with OpenAI API
- Supports all OpenAI API endpoints
- Utilizes PSR-17 and PSR-18 compliant HTTP clients, and factories for making API requests
Requirements
- PHP 7.4 or higher
- A PSR-17 HTTP Factory implementation (e.g., guzzle/psr7 or nyholm/psr7)
- A PSR-18 HTTP Client implementation (e.g., guzzlehttp/guzzle or symfony/http-client)
Installation
You can install the library via Composer:
composer require softcreatr/php-openai-sdk
Usage
First, 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 SoftCreatR\OpenAI\OpenAI; $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->listModels(); // Process the API response if ($response->getStatusCode() === 200) { $models = json_decode($response->getBody()->getContents(), true); print_r($models); } else { echo "Error: " . $response->getStatusCode(); }
For more details on how to use each endpoint, refer to the OpenAI API documentation, and the examples provided in the repository.
Supported Methods
Models
- List Models - Example
listModels()
- Retrieve Model - Example
retrieveModel(string $id)
- Delete Model - Example
deleteModel(string $id)
Chat Completions
- Create Chat Completion - Example
createChatCompletion(array $options = [])
Images
- Create Image - Example
createImage(array $options = [])
- Create Image Edit - Example
createImageEdit(array $options = [])
- Create Image Variation - Example
createImageVariation(array $options = [])
Embeddings
- Create Embedding - Example
createEmbedding(array $options = [])
Audio
- Create Transcription - Example
createTranscription(array $options = [])
- Create Translation - Example
createTranslation(array $options = [])
- Create Speech - Example
createSpeech(array $options = [])
Files
- List Files - Example
listFiles()
- Create File - Example
createFile(array $options = [])
- Delete File - Example
deleteFile(string $id)
- Retrieve File - Example
retrieveFile(string $id)
- Download File - Example
downloadFile(string $id)
Fine-tuning
- Create Fine-tuning Job - Example
createFineTuningJob(array $options = [])
- List Fine-tuning Jobs - Example
listFineTuningJobs()
- Retrieve Fine-tuning Job - Example
retrieveFineTuningJob(string $id)
- Cancel Fine-tuning - Example
cancelFineTuning(string $id)
- List Fine-tuning Events - Example
listFineTuningEvents(string $id, array $options = [])
Moderations
- Create moderation - Example
createModeration(array $options = [])
Changelog
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.
Known Problems and limitations
Streaming Support
Currently, streaming is not supported in the createCompletion
and createChatCompletion
methods. It's planned to address this limitation asap. For now, please be aware that these methods cannot be used for streaming purposes.
If you require streaming functionality, consider using an alternative implementation or keep an eye out for future updates to this library.
License
This library is licensed under the ISC License. See the LICENSE file for more information.