how to integrate chatgpt API in laravel 10

Spread the love

Hello Guys,

In this post, we’ll guide you on integrating the ChatGPT API into Laravel 10. Discover a practical example of how to use the Laravel ChatGPT API in this article. You can also explore a sample of the Laravel ChatGPT API. Let’s dive into the specifics.

Chat GPT, short for Generative Pre-trained Transformer, is a powerful language model created by OpenAI. With billions of parameters, it stands as one of the most advanced and comprehensive language models to date.

Chat GPT was trained using an extensive corpus of text from various online sources, including books, research papers, and web content. Employing a self-supervised learning approach, the model was trained to predict the next word in a given text string. This enables the model to recognize linguistic patterns and structures, generating responses that are contextually relevant and human-like.

In this post, we’ll demonstrate how to leverage OpenAI within the Laravel framework. We’ll walk you through creating a simple GET route and utilizing the HTTP client facade to access OpenAI’s API. OpenAI offers several models, including GPT-3, GPT-3.5, GPT-4, and Davinci. We’ll showcase data retrieval using the gpt-3.5-turbo model.

So let’s take the following actions to integrate chatgpt API in laravel 10:

Step 1: Create OpenAI Account

To obtain your account key effortlessly, start by registering for an OpenAI account.

Also Read: How to use Drag and Drop Datatable Rows for Sorting using laravel 10

Create an Account from here:

image 21 how to integrate chatgpt API in laravel 10

Go to the following site after creating an account to generate a key:

Go Here:

image 22 how to integrate chatgpt API in laravel 10

Then, you can obtain your account key and add it to your .env file as seen below:


Step 2: Create a Route

Now that we have one route created, let’s add it to the web.php file so that our example can be called.

Also Read: How to Upload Images with Spatie Media Library in LARAVEL 10

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ChatGptController;
| 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!
Route::get('chat-gtp', [ChatGptController::class, 'index']);

Step 3: Create a Controller

In this phase, we will craft a ChatGptController and construct the index method. Subsequently, we will harness the OpenAI API to retrieve data. To achieve this, we will append a fresh route to the web.php file, as illustrated in the example below:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Http\JsonResponse;
class ChatGptController extends Controller
     * Write code on Method
     * @return response()
    public function index(): JsonResponse
        $search = "How to install laravel 10";
        $data = Http::withHeaders([
                    'Content-Type' => 'application/json',
                    'Authorization' => 'Bearer '.env('OPENAI_API_KEY'),
                  ->post("", [
                    "model" => "gpt-3.5-turbo",
                    'messages' => [
                           "role" => "user",
                           "content" => $search
                    'temperature' => 0.5,
                    "max_tokens" => 200,
                    "top_p" => 1.0,
                    "frequency_penalty" => 0.52,
                    "presence_penalty" => 0.5,
                    "stop" => ["11."],
        return response()->json($data['choices'][0]['message'], 200, array(), JSON_PRETTY_PRINT);

Now, Enter the provided URL into your web browser to view the app’s output.

Also Read: How to Create a Custom Error Page in Laravel 10?

http://localhost:8000/chat-gpt or

I hope it can be useful to you.

Leave a Comment