OpenAI is a powerful platform that enables developers to build intelligent applications with natural language processing capabilities. Laravel, on the other hand, is a popular PHP framework that provides an excellent foundation for web application development.

In this tutorial, we'll explore how to use OpenAI for Laravel, and how it can help us build smarter, more efficient web applications.


To follow along with this tutorial, you'll need the following:

  • A local development environment with Laravel installed.
  • Basic knowledge of Laravel and PHP.
  • A OpenAI API key.

Getting Started

First, we need to install the OpenAI API package. Open your Laravel project in a terminal and enter the following command:

composer require openai/api

Next, you need to obtain an API key from the OpenAI platform. Once you have your key, add the following to your .env file:


With the package installed and the API key configured, we're ready to start using OpenAI in our Laravel application.

Generating Text with OpenAI

The OpenAI API provides several capabilities, such as language processing, chatbots, and much more. In this example, we'll use it to generate text based on a given prompt.

To get started, create a new route in your routes/web.php file:

Route::get('/openai/gpt3', function () {
    $openai = new \OpenAI\Api(env('OPENAI_SECRET_KEY'));

    $prompt = "The quick brown fox";
    $completions = $openai->completions([
        'model' => 'text-davinci-002',
        'prompt' => $prompt,
        'max_tokens' => 5,
        'n' => 1,
        'stop' => '\n',

    return $completions->choices[0]->text;

This code creates a new route that generates text using the OpenAI GPT-3 model. We pass in a prompt, which is the initial text that the model uses to generate the output. We then specify the model to use, the maximum number of tokens to generate, and how many completions to return. Finally, we stop the generation when we reach a new line.

To test the route, visit http://localhost:8000/openai/gpt3 in your web browser. You should see some text generated by the GPT-3 model based on the prompt we provided.


In this tutorial, we learned how to use OpenAI in Laravel to generate text using the GPT-3 model. OpenAI is a powerful platform that can help us build intelligent applications with natural language processing capabilities. With the OpenAI API package installed in Laravel, we can easily integrate OpenAI into our applications and use it to make our web applications smarter and more efficient.

Recommended Posts

View All

Laravel 9 Upload image using Summernote editor

A basic Javascript package called Summernote Editor can be used to create online WYSIWYG web editors.

How to install Adminer in Laravel application

In this article we will install Adminer in Laravel 8 application

Laravel 8 Install Bootstrap Example Tutorial

laravel ui, laravel ui bootstrap 4, bootstrap 4, laravel 8 install bootstrap 4, install bootstrap 4 in laravel 8, how to use bootstrap in laravel 8

Laravel 8 Custom 404, 500 Error Page Example

How to create custom error page in Laravel 8 and we will also try to tell you why we required to create the custom error page.

Laravel 9 Socialite Login with LinkedIn Tutorial Example

How to use the Laravel socialite, Livewire, and Jetstream libraries to create a LinkedIn login system from scratch in Laravel