To use OpenAI API completion, you can use the instance of OpenAIService.
Copy
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 responseecho $response['choices'][0]['message']['content'];
To create images using OpenAI API, you can use:
Copy
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:
Copy
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.