How can we use OpenAI in Laravel

How can we use OpenAI in Laravel

This article will help you use of OpenAI in Laravel 9. We will guide you step-by-step how can we use Open AI in Laravel. Open AI is very useful in every aspect. Simple mean the use of Artificial Machine your app. Which can help you in every aspect.

We will discus how to setup OpenAI account and discus about the dependencies which are required to build OpenAI System in our project.

After this article you will be able to build your own artificial system to done your work like writing an article, keywords finder and much more which are very help full for you.

How can we use OpenAi in laravel

There are some steps are required to make Open AI System.

Step 1: Laravel New Project

Step 2: Creating account on OpenAI

Step 3: OpenAI Client installing

Step 4: Views Files creating

Step 5: Routes

Step 6: Controller creating

Step 7: Testing Project


Laravel New project

First, Step is to making a new Laravel project to use of OpenAI System maker. Run the given command in your terminal to make a project.

Composer create-project Laravel/Laravel OpenAi

Or run the given command, if you installed Laravel in your system.

Laravel new OpenAi

Creating account on OpenAI

In this step we will see how create n account and get API keys. Open this link and create a new account. Or signup with google.

When you complete registration open this link and create a new Api Key.

After API Key creating open your .env file in your project and update with given code and replace your Api Key.

OPENAI_API_KEY=<Enter your API key>

OpenAI Client installing

After API KEY inserting open your terminal and run the given command install repository of OpenAI in your project.

Composer require openai-php/client

You can also download OpenAI package source code from github

Views Files creating

So, the next step is to make a balde file with the name of writer.blade.php and update with given code.

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>How can we use OpenAI in laravel - LayyahInfo</title>
        <script src=""></script>
        <script src="" defer></script>

    <body class="antialiased">
        <div class="relative flex items-top justify-center min-h-screen bg-gray-100 dark:bg-gray-900 sm:items-center py-4 sm:pt-0">
            <div class="max-w-6xl w-full mx-auto sm:px-6 lg:px-8 space-y-4 py-4">
                <div class="text-center text-gray-800 dark:text-gray-300 py-4">
                    <h1 class="text-7xl font-bold">OpenAI Bot</h1>

                <div class="w-full rounded-md bg-white border-2 border-gray-600 p-4 min-h-[60px] h-full text-gray-600">
                    <form action="/write/generate" method="post" class="inline-flex gap-2 w-full">
                        <input required name="title" class="w-full outline-none text-2xl font-bold" placeholder="Type your article title..." />
                        <button class="rounded-md bg-emerald-500 px-4 py-2 text-white font-semibold">Generate</button>
                <div class="w-full rounded-md bg-white border-2 border-gray-600 p-4 min-h-[720px] h-full text-gray-600">
                    <textarea class="min-h-[720px] h-full w-full outline-none" spellcheck="false">{{ $content }}</textarea>


Open routes/web.php file and update with given code to make routes for OpenAI.

use App\Http\Controllers\ ContentWriterController;
Route::get('/write', function () {
    $title = '';
    $content = '';
    return view('write', compact('title', 'content'));
Route::post('/write/generate', [ArticleGeneratorController::class, 'index']);


Controller Creating

After routes setup we need a controller which handle all the requests. For creating the controller run the given command.

Php artisan make:controller ContentWriterController

Then update created controller with given code.

public function index(Request $request)
    if ($request->title == null) {
    $title = $request->title;
    $client = OpenAI::client(env('OPENAI_API_KEY'));
    $result = $client->completions()->create([
        "model" => "text-davinci-003",
        "temperature" => 0.7,
        "top_p" => 1,
        "frequency_penalty" => 0,
        "presence_penalty" => 0,
        'max_tokens' => 600,
        'prompt' => sprintf('Write article about: %s', $title),
    $content = trim($result['choices'][0]['text']);
    return view('write', compact('title', 'content'));


After that open config/app.php and config

'openai_api_key' => env('OPENAI_API_KEY'),

So run the given command to optimize the cache.

Php artisan config:cache

So now use the config like this way

$client = OpenAI::client(config('app.openai_api_key'));

Testing Project

Now its time to test the project is it working properly or not. So run the given command to run the server.

Php artisan serve

So open this link to check your result.


No Comment posted Yet!

Leave a Reply

OK! You can skip this field.