Back in October, Nuno Maduro released an OpenAI PHP Client and this week he announced a brand Laravel integration called OpenAPI Laravel.
On Twitter Nuno said, “OpenAI PHP for Laravel is a supercharged PHP API client that allows you to interact with the Open AI API, with this integration, developers can now easily use OpenAI’s powerful natural language processing capabilities in their Laravel applications.”
Getting started with the package is pretty straightforward and you can see the guide here:
First, install OpenAI:
composer require openai-php/laravel
Next, publish the configuration file:
php artisan vendor:publish --provider="OpenAI\Laravel\ServiceProvider"
This will create a config/openai.php
configuration file in your project, which you can modify to your needs
using environment variables:
OPENAI_API_KEY=sk-...
Finally, you may use the OpenAI
facade to access the OpenAI API:
use OpenAI\Laravel\Facades\OpenAI;
$result = OpenAI::completions()->create([
'model' => 'text-davinci-003',
'prompt' => 'PHP is',
]);
echo $result['choices'][0]['text']; // an open-source, widely-used, server-side scripting language.
Check out the repo for full instructions and for usage examples, take a look at the openai-php/client repository.