How to use OpenAI in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hello Folks,

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:

.env

OPENAI_API_KEY=sk-FQolFbZAM6OHS7ddhlS...

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:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\OpenAIController;

/*

|--------------------------------------------------------------------------

| 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:

app/Http/Controllers/OpenAIController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Illuminate\Support\Facades\Http;

use Illuminate\Http\JsonResponse;

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'),

])

->post("https://api.openai.com/v1/chat/completions", [

"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."],

])

->json();

return response()->json($data['choices'][0]['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:

http://localhost:8000/open-ai

Output:

I hope it can help you...

Tags :
Shares