How to use OpenAI in Laravel?
I am going to explain to you example of how to use openai in laravel. In this article, we will implement a laravel open ai package. We will use laravel use open api example. you will learn how to connect openai in laravel. So, let's follow a few steps to create an example of how to use openai in laravel.
OpenAI is a leading research organization that is dedicated to developing and advancing artificial intelligence in a safe and beneficial manner. It was founded in 2015 by a group of prominent figures in the tech industry, including Elon Musk, Sam Altman, Greg Brockman, and others.
The organization has made significant contributions to the field of artificial intelligence, including developing some of the most advanced language models in the world, such as GPT-3, which is a language model capable of generating human-like text. OpenAI has also developed cutting-edge algorithms for tasks like image recognition and reinforcement learning.
In this post, i will show you how to use open ai in laravel. we will create simple get route and call openai apt to getting data using HTTP client facade. Open AI has many models like GPT-3, GPT-3.5, GPT-4 and davinci etc. we will use gpt-3.5-turbo and get data.
So, let's follow the below steps:
Step 1: Install Laravel
This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Create OpenAI Account
First you need to create account on OpenAI. then you can easily get account key.
Create Account from here: https://openai.com/product.
After, creating account go to following link and generate key:
Go Here: https://beta.openai.com/account/api-keys.
Next you can get account key and add on .env file as like bellow:
Step 3: Create Route
now we will create one route for calling our example, so let's add new route to web.php file as bellow:
| 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('open-ai', [OpenAIController::class, 'index']);
Step 4: Create Controller
in this step, we will create OpenAIController and write index method. Then we will call OpenAI API and getting data, so let's add new route to web.php file as bellow:
class OpenAIController extends Controller
* Write code on Method
* @return response()
public function index(): JsonResponse
$search = "laravel get ip address";
$data = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'Bearer '.env('OPENAI_API_KEY'),
"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']['message'], 200, array(), JSON_PRETTY_PRINT);
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
I hope it can help you...
We are Recommending you
- How to Convert Image to Base64 in Laravel?
- How to Get Environment Variable in Laravel React JS?
- Laravel Country State City Dropdown using Ajax Example
- Laravel 9 Custom Validation Error Message Example
- Laravel Contact Form Send Email Tutorial
- Laravel Google Pie Chart Example Tutorial
- How to Setup File Permissions Correctly in Laravel?
- Laravel Firebase Push Notification to Android and IOS App Example
- How to Get Last 6 Months Data in Laravel?
- How to Remove Composer Package in Laravel?
- How to Run All Seeders in Laravel?
- Laravel Blade If Multiple Conditions Example
- Laravel Move File from One Folder to Another Example