Using OpenAI GPT-3 in Laravel

GPT-3 is a neural network machine learning model that is pre-trained using internet data to generate any type of text. It requires a small amount of input text to generate large volumes of relevant and sophisticated machine-generated text. It is developed by OpenAI.

GPT-3 is not open-sourced yet, but the good thing is you can use it through OpenAI GPT-3 API. The pricing of the API is relatively low as compared to the performance of GPT-3.

You can use GPT-3 for generating blog posts, Facebook ads, paraphrasing, writing bio, etc. In this article, we will see how we can use OpenAI GPT-3 in Laravel.

To get started first go to https://beta.openai.com/signup and create a new account, after the signup process you will get 18$ of free GPT-3 API requests in your OpenAI account.

Now goto https://beta.openai.com/account/api-keys and generate a new api key.

Our next step is to create a new Laravel project. Make sure your system has COMPOSER.

composer create-project laravel/laravel gpt3-app
 
cd gpt3-app

Now create a new controller :

php artisan:make controller GPT3Controller

Go to app/Http/Controllers/GPT3Controller

Create a new method “index”. Add use Illuminate\Support\Facades\Http; line before controller class.

See the below code, I have explained the lines with comments!

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

//add this line, we will use Laravel HTTP to call OpenAI API !
use Illuminate\Support\Facades\Http;

class GPT3Controller extends Controller
{

    //OpenAI GPT3 Engine names :
    private $engines = [
       "babbage" => "text-babbage-001",
       "curies" => "text-curie-001",
       "ada" => "text-ada-001",
       "davinci" => "text-davinci-001"
    ];

    //Put your OpenAI API Token !
    private $token = "token-example";

    public function index(){

      //prompt or you can say user input
      $prompt = "What is Laravel";

      //choose model !
      //Davinci is the most powerful engine
      $engine = $this->engines['davinci'];

      //max tokens you want as an output
      //1 token is almost 0.75 word
      $maxTokens = 100;
        

       //Using Laravel HTTP
      $response = Http::withHeaders([
            'Content-Type' => 'application/json',
            'Authorization' => "Bearer $this->token"
        ])->post("https://api.openai.com/v1/engines/$engine/completions", [
            'prompt' => $prompt,
            "temperature" => 0.7,
            "max_tokens" => $maxTokens,
            "top_p" => 1,
            "frequency_penalty" => 0,
            "presence_penalty" => 0,
        ]);

     //Now check if the request was successful or not !
     //After checking print result !

      if($response->failed()){
            return "Request Failed !";
      }
      else{

          //OpenAI API result
          return $response['choices'][0]['text'];
      }
    
   }
}

?>

Go to routes/web.php and create a new route for GPT3Controller.

<?php

use Illuminate\Support\Facades\Route;

//ADD THIS LINE
use App\Http\Controllers\GPT3Controller;


/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/


//AND ADD THIS LINE !
Route::get("/gpt3", [GPT3Controller::class, "index"])->name("gpt3Route");


?>

Start the development server by running :

php artisan server

Now go to http://localhost:YOUR_APP_PORT/gpt3 and see the output. You can also change the prompt & engine from GPT3 Controller.

Congrats! You have successfully set up your own GPT3 App, now you can use your creativity to create awesome AI Apps using the power of the OpenAI GPT3 model.

READ: How to send emails in Laravel using PHPMailer.

Related Posts