Laravel OpenAI Package is a Laravel wrapper for the OpenAI API, providing easy integration and usage of OpenAI's capabilities such as GPT-3, DALL-E, and more within Laravel applications. This package simplifies the process of connecting to OpenAI's API and performing various AI-driven tasks.
- Easy integration with Laravel projects.
- Supports various OpenAI API functionalities like Chat, Image Generation, etc.
- Customizable to fit specific use cases.
- PHP >= 7.4
- Laravel >= 8.0
- Guzzle HTTP Client
Install the package via Composer:
composer require dragnsurvey/openaiAfter installing, publish the configuration file (if needed):
php artisan vendor:publish
--provider="Dragnsurvey\OpenAi\OpenAiServiceProvider"To configure the package, add your OpenAI API key to your .env file:
OPENAI_API_KEY=your_api_key_hereSet other configuration options in config/openai.php (if the
configuration file was published).
To use the Chat API:
use Dragnsurvey\OpenAi\Chat\ChatRequest;
$chatRequest = new ChatRequest();
$chatRequest->setModel("gpt-4");
$chatRequest->addSystemMessage("You answer questions about football using
the tone of a sportscaster ");
$chatRequest->addUserMessage("Hello, who won the world cup in 1998?");
$response = $chatRequest->send();
print_r($response);To use the Image Generation API:
use Dragnsurvey\OpenAi\Image\ImageRequest;
$imageRequest = new ImageRequest();
$imageRequest->setPrompt("A two-headed dragon");
$response = $imageRequest->send();
//OR pass the prompt in the constructor
$imageRequest = new ImageRequest("A two-headed dragon");
$response = $imageRequest->send();
//change model
$imageRequest->setModel('dall-e-3')
print_r($response);For advanced usage, refer to OpenAI's official documentation.
Run the tests with:
vendor/bin/phpunitContributions are welcome, and any help is greatly appreciated.
This package is open-sourced software licensed under the MIT license.
- Roman STEC (https://www.dragnsurvey.com)