How to Integrate ChatGPT With Laravel 10?
What is ChatGPT?
Recently, OpenAI has developed an Artificial intelligence Chatbot called ChatGPT. It is based on the design of the “Generative Pre-trained Transformer 3.5” (GPT-3.5). Based on input, GPT-3.5 can comprehend and produce text that resembles a person’s. It uses deep learning techniques to evaluate and comprehend data patterns, allowing it to produce logical and pertinent responses to the situation. Here, we are going to discuss ChatGPT in Laravel and its use cases. Read on!
ChatGPT can answer your queries, give explanations, engage in conversation, and do several other natural language processing functions. It also assists with tasks that require language understanding and generation. To enable developers to incorporate ChatGPT into various programs and services and offer interactive and conversational experiences, OpenAI has made it available through an API. People are using it for content creation, virtual assistants, chatbots for customer service, and more. In this ChatGPT tutorial for Laravel, you will learn how to integrate it into your laravel project.
Applications of ChatGPT: A Quick Look
It would help if you kept in mind that the GPT model does not include a fully working product that covers all domain areas. Consequently, more models are a must for the GPT model to operate. You’ll also need the application for it as well as backend administration tools. Users can use the output of a GPT model to examine using NLP methods to extract pertinent information. The model replies with text that appears to be long sentences. You can use one or more words as filters or keywords in later pipeline stages as this output.
You may already be aware that OpenAI released ChatGPT after 2022. Everyone is aware of how effective ChatGPT is. Thankfully, as developers, you can use the official API to benefit from this capability. In today’s session, we’ll discover the process of ChatGPT integration with Laravel. Therefore, we won’t be creating a chat-based app this time. As you are aware, Chatgpt is currently available for everyone and you can integrate this amazing tool with your Laravel project to make your applications intelligent. Let’s learn How to integrate ChatGPT in Laravel 10.
Integrate ChatGPT api in Laravel 10
To use ChatGPT API in Laravel 10, you can follow these steps:
- Install GuzzleHTTP package:
Guzzle is a PHP HTTP client that will help you make API requests. You can install it using Composer by running the following command in your Laravel project directory:
composer require guzzlehttp/guzzle
- Create a Controller:
Create a new laravel controller that will handle the API requests to ChatGPT. For example, let’s create a ChatGPTController.php file:
- Open the ChatGPTController.php and implement the following code:
php artisan make:controller ChatGPTController
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Support\Facades\Http; use GuzzleHttp\Client; class ChatGPTController extends Controller { protected $httpClient; public function __construct() { $this->httpClient = new Client([ 'base_uri' => 'https://api.openai.com/v1/', 'headers' => [ 'Authorization' => 'Bearer ' . env('CHATGPT_API_KEY'), 'Content-Type' => 'application/json', ], ]); } public function askToChatGpt() { $message = "what is laravel"; $response = $this->httpClient->post('chat/completions', [ 'json' => [ 'model' => 'gpt-3.5-turbo', 'messages' => [ ['role' => 'system', 'content' => 'You are'], ['role' => 'user', 'content' => $message], ], ], ]); return json_decode($response->getBody(), true)['choices'][0]['message']['content']; } }
- Register route:
Open the routes/web.php file and add a route to test the ChatGPT API:
Route::get('/chat', [App\Http\Controllers\ChatGPTController::class, 'askToChatGpt']);
- Visit this link to generate the ChatGPT API key: https://platform.openai.com/account/api-keys
- Copy the key and paste it into the .env file CHATGPT_API_KEY=’YOUR_GENERATED_KEY’
Visit http://localhost:8000/chat in your web browser or use tools like Postman to make a GET request to the /chat endpoint. You should see the response from the ChatGPT API.
That’s it! You have now integrated ChatGPT API into your Laravel 10 application. We hope this ChatGPT guide for Laravel will help you with the integration. If you are looking for expert Laravel development services, we are here for you. Let’s connect to discuss more.
Tags: ChatGPT guide for Laravel, ChatGPT in Laravel 10, ChatGPT integration with Laravel, ChatGPT tutorial for Laravel
Originally published at https://www.avyatech.com on June 5, 2023.