clock  Mon - Sun 8.00 AM - 8.00 PM
play store

OpenAI and it’s API integration in a Laravel application

writter  Sumit Dey Sarkar
Date  01 Apr 2023
Language  Laravel
OpenAI and it’s API integration in a Laravel application

OpenAI and it’s API integration in a Laravel application

In this tutiorial we will learn OpenAI and it’s API integration in a Laravel application.

OpenAI and it’s API integration in a Laravel application

OpenAI and it’s API integration in a Laravel application


What is open AI?

OpenAI is an artificial intelligence research laboratory.

It aims to create safe and beneficial AI for everyone.

OpenAI conducts research and develops AI tools and software.


How to integrate openAI API in a Laravel application

To integtate Chat GPT API in Laravel application, you need to follow below steps:


Step 1 - In first step you need to nstall the OpenAI SDK using following command in your Laravel project directory

composer require openai/openai


Step 2 - Create an OpenAI API key by signing up for an account on the OpenAI website.



Step 3 -  Add your API key to your Laravel project's .env file



Step 4 - Create a new controller in your Laravel project by running the following command:

php artisan make:controller OpenAIController


Step 5 - In your OpenAIController, import the OpenAI namespace and set up the API credentials


namespace App\Http\Controllers;

use OpenAI;

class OpenAIController extends Controller
    public function chatGPT()
        // Your code goes here


Step 6 - In the chatGPT method of your OpenAIController, call the appropriate OpenAI API method to perform the desired action. For example, to generate text using the Chat GPT model, you can use the following code

$response = OpenAI::completion([
    'engine' => 'davinci',
    'prompt' => 'Hello,',
    'max_tokens' => 5,
    'temperature' => 0.5,

$text = $response['choices'][0]['text'];

return response()->json(['text' => $text]);

In this example, the completion method is used to generate a short text completion given a prompt. The max_tokens and temperature parameters control the length and randomness of the generated text, respectively.


Step 7 - Finally, you can create a route in your routes/web.php file to call the chatGPT method of your OpenAIController. For example:

Route::get('/chat', [OpenAIController::class, 'chatGPT']);


Step 8 - You may want to create a form or user interface to allow users to input their desired prompts and see the generated text. You can do this by creating a view in your Laravel project, which can be loaded by the chatGPT method of your controller. For example, you can create a chat.blade.php file in the resources/views directory with the following code

<!DOCTYPE html>
    <title>Chat with GPT</title>
    <form method="POST" action="{{ route('chat') }}">
        <label for="prompt">Prompt:</label>
        <input type="text" name="prompt" id="prompt">
        <button type="submit">Generate</button>

    @if ($text)
        <p>{{ $text }}</p>


Step 9 - Modify your chatGPT method in your controller to handle the form submission and return the generated text to the view. For example

public function chatGPT(Request $request)

    $text = null;

    if ($request->isMethod('post')) {
        $response = OpenAI::completion([
            'engine' => 'davinci',
            'prompt' => $request->input('prompt'),
            'max_tokens' => 50,
            'temperature' => 0.5,

        $text = $response['choices'][0]['text'];

    return view('chat', ['text' => $text]);

In the above example, the chatGPT method checks if the HTTP method is POST, which indicates that the form has been submitted. If so, it generates text using the Chat GPT model and assigns the result to the $text variable. The method then returns the chat.blade.php view with the $text variable passed to it.


Step 10 - Finally, create a route in your routes/web.php file that maps to the chatGPT method of your controller. For example

Route::get('/chat', [OpenAIController::class, 'chatGPT'])->name('chat');
Route::post('/chat', [OpenAIController::class, 'chatGPT'])->name('');

In the above example, two routes are defined for the chatGPT method: one for the initial page load (GET method) and one for form submissions (POST method). The name method is used to name the routes, which can be used to generate URLs in your views using the route function.

Now you have a Laravel application that allows users to chat with the Chat GPT model. This is just a basic example but you can modify it according to your needs.

Comments 0

Leave a comment

Coursera, Codeacademy, Udacity, W3Schools, Udemy, Alison, TheNewBoston, edX, P.S.Codewars,Freecodecamp, Managing technical debt blog, Scrimba, Codepen, Codepen/challenges, The Odin Project, htmlreference.​io, cssreference.​io, Frontend Mentor, Dev Challenges, MDN, Code Mentor, Coding Dojo, CSS Battle, Codier, Ace Frontend, Can I Use, CSS Tricks, 30 Seconds of Code,tutorialspoint, Neumorphism, Shaddows Brumm, Fancy Border Radius, Glow Generator, Clothoid Corners, Glassmorphism, Clipy, CSS Filters, Base64 Image, Quantity Queries, Animations, Cubic-Bezier, Keyframes, Wait Animate, Transition.Style, graphic design, web design, website design, website builder, web developer, web designer, webdesign, ecommerce website, web design company, website creator, website designer, responsive web design, web development company, best website design, web design software, web page design, build a website, web developer salary, design website, web design courses, how to design a website, web design inspiration, website layout, web designer salary, web application development, ecommerce website design, web agency, software development company, web design tutorial, web programming, design company, website design templates, what is web designing, web developer jobs, website developer, web design agency, freelance web developer, web design services, freelance web designer, graphic design websites, web solutions, ecommerce website development, free website design, web development courses, webdev, web developers, web development tools, website design services, developpeur web, web design london, website design ideas, web designing and programming, design a website, web design and development, web dev, web development services, homepage design, best designed websites, cheap website design, learn web design, web design templates, web design tools, web design jobs, website design inspiration, web design india, flash website, website developers, designer websites, website services, website design cost, good website design, site design, simple website design, cool website designs, modern website design, graphic designer websites, webcode, best web design software, website making, free web design software, mobile website design, learn web development, front end web developer, how to become a web developer, web developer portfolio, web development company in india, python web development, web development tutorial, website company, website design and development, web company, webdesigning, professional website design, affordable web design, best web design company, creative web design, top website designs, website design pricing, web developer tools, how to develop a website