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 One to One Eloquent Relationship Tutorial

Laravel One To One Eloquent Relationships Tutorial Example, Laravel Eloquent ORM is the best possible database solution, ORM Mapping is very easy, lar...

Learn About API Testing Tool in Laravel 8 Tutorial

In this Laravel 8 tutorial, you will learn about the API Testing Tool. Laravel 8 composer package Web service testing API utility. How to use the Lara...

Laravel 9 Capture Image from Webcam and Store in Database

Learn how to capture images from webcam using Laravel 9 and store them in a database. Step-by-step guide with code examples and best practices.

Laravel Many to Many Polymorphic Relationship

Laravel many to many polymorphic relations, many to many polymorphic relations Laravel, polymorphic relations eloquent Laravel, Laravel morphToMany ex...

Laravel 8 Import Export Excel & CSV File Example

Using the Maatwebsite/Laravel-Excel package, you will learn how to easily import and export Excel and CSV files in the Laravel 8 application while com...