Larafast provides a simple way to integrate OpenAI API with Laravel.

Installation

  1. Create an account on OpenAI
  2. Create your API key from the dashboard from API Keys
  3. Add your API key to your .env file as OPENAI_KEY=your-api-key

That’s it, your project is ready to use OpenAI API.

Usage

To use OpenAI API completion, you can use the instance of OpenAIService.

use App\Services\OpenAIService;

$openai = app(OpenAIService::class);
$response = $openai->completion('Write recipe based on the given ingredients: 1 cup of flour, 2 eggs, 1 cup of sugar');

// You can also pass the model name
$response = $openai->completion('Write recipe based on the given ingredients: 1 cup of flour, 2 eggs, 1 cup of sugar', 'davinci');

// See the list of pre-defined model names in config/services.php -> openai.models

// To get the response
echo $response['choices'][0]['message']['content'];

To create images using OpenAI API, you can use:

use App\Services\OpenAIService;

$openai = app(OpenAIService::class);
$response = $openai->image('A beautiful sunset over the mountains');

You can adjust the number of generating images and the size in the OpenAIService@image method.

To create an audio from text using OpenAI API, you can use:

use App\Services\OpenAIService;

$openai = app(OpenAIService::class);
$response = $openai->textToSpeech('Hello, how are you?');

The text-to-speech will return the audio content instead of the URL as for the image. You can save the audio content to a file or return it as a response.

For more information, you can check the OpenAI API documentation to add new features to your service or adjust existing ones.