Laravel ChatGPT – How to use ChatGPT in Laravel
In this post, we will give you information about Laravel ChatGPT – How to use ChatGPT in Laravel. Hear we will give you detail about Laravel ChatGPT – How to use ChatGPT in Laravel And how to use it also give you a demo for it if it is necessary.
The creation of AI-based applications is made easier for developers by the robust artificial intelligence platform known as ChatGPT.
Whether you are a seasoned Laravel developer or just getting started, this article will provide you with the knowledge you need to integrate ChatGPT machine-learning capabilities into your online apps.
We’ll cover everything, from setting up your ChatGPT account and installing the necessary dependencies to using the ChatGPT API and integrating it into your Laravel application.
You’ll have a clear knowledge of how to integrate ChatGPT into your Laravel projects by the end of this article, and you’ll be well on your way to creating web applications that are intelligent and machine learning-powered.
Laravel ChatGPT – How to use ChatGPT in Laravel
1. Install Laravel Project
First, open Terminal and run the following command to create a fresh laravel project:
composer create-project --prefer-dist laravel/laravel ChatGPT
or, if you have installed the Laravel Installer as a global composer dependency:
laravel new openai
2. Register to ChatGPT
First, we need to get access to API to setup OpenAI. Go to https://openai.com/api and click the signup button.
Once signed up, go to https://beta.openai.com/account/api-keys and click the button. Now create a new secret key, copy it, and put it in our .env
file inside our Laravel project.
OPENAI_API_KEY=<YOUR API KEY>
3. Install ChatGPT Client
Install ChatGPT Client repository, Run the below command:
composer require openai-php/client
You can experiment with the API using the playground and generate the API call with curl.
The openai-client
package source code is here: https://github.com/openai-php/client.
4. Create Blade Editor
We will create a resources/view/writer.blade.php
blade file.
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel ChatGPT - How to use ChatGPT in Laravel</title>
<!-- Fonts -->
<link href="https://fonts.bunny.net/css2?family=Space+Grotesk:[email protected];600;700&display=swap" rel="stylesheet">
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
font-family: 'Space Grotesk', sans-serif;
}
.title:empty:before {
content:attr(data-placeholder);
color:gray
}
</style>
<script src="https://unpkg.com/marked" defer></script>
</head>
<body >
<div >
<div >
<div >
<h1 >Writebot</h1>
</div>
<div >
<form action="/write/generate" method="post" >
@csrf
<input required name="title" placeholder="Type your article title..." />
<button >Generate</button>
</form>
</div>
<div >
<textarea spellcheck="false">{{ $content }}</textarea>
</div>
</div>
</div>
</body>
</html>
And then, register the UI to write the endpoint, and we will pass an empty variable for $title
and $content
. We will use this later to display the generated content.
// routes/web.php
Route::get('/write', function () {
$title = '';
$content = '';
return view('write', compact('title', 'content'));
});
5. Create a Controller
Create ArticleGeneratorController
controller, run the following command:
php artisan make:controller ArticleGeneratorController
Remember to register the endpoint to our routes.
use AppHttpControllersArticleGeneratorController;
Route::post('/write/generate', [ArticleGeneratorController::class, 'index']);
In the ArticleGeneratorController
, let’s add a method index
to get the user input and call the ChatGPT API to generate the content we want.
// app/Http/Controllers/ArticleGeneratorController.php
public function index(Request $request)
{
if ($request->title == null) {
return;
}
$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'));
}
Load the config from config/app.php
:
'openai_api_key' => env('OPENAI_API_KEY'),
Then run the config cache command to optimize the system call to get the env
value.
php artisan config:cache
Now you can use the config like this :
$client = OpenAI::client(config('app.openai_api_key'));
6. Run Laravel for Laravel ChatGPT – How to use ChatGPT in Laravel
Now, we will run the laravel application using the following command.
php artisan serve
Thank you for reading Laravel ChatGPT – How to use ChatGPT in Laravel article. Hope this code and post will helped you for implement How to use ChatGPT in Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs