Start a project
Laravel 8 min read

How to Integrate ChatGPT API into Laravel ?

Introduction The rise of conversational AI has revolutionized the way users interact with applications. OpenAI's ChatGPT, a state-of-the-art language model, enables developers to build applications that can understand and generate human-like text. Integrating ChatGPT…

How to Integrate ChatGPT API into Laravel ?
// on this page
  1. Introduction
  2. Table of Contents
  3. Prerequisites
  4. Setting Up the Laravel Project
  5. 1. Install Laravel
  6. 2. Configure Environment
  7. 3. Serve the Application
  8. Installing the OpenAI PHP Client
  9. 1. Install OpenAI PHP Client
  10. Configuring Environment Variables
  11. 1. Update .env File
  12. 2. Update Configuration Files
  13. Creating Routes and Controllers
  14. 1. Create Routes
  15. 2. Generate Controller
  16. 3. Implement Controller Methods
  17. Integrating ChatGPT
  18. 1. Using OpenAI API Directly
  19. Creating the Frontend Interface
  20. 1. Create the Chat View
  21. Maintaining Conversation Context
  22. 1. Using Sessions
  23. 2. Update the generateChatGPTResponse Method
  24. 3. Handling Session Timeout
  25. Securing Your Application
  26. 1. Protect Your API Key
  27. 2. Use CSRF Protection
  28. 3. Input Validation
  29. 4. Use HTTPS in Production
  30. Deployment Considerations
  31. 1. Environment Variables
  32. 2. Use a Proper Web Server
  33. 3. Optimize Your Application
  34. 4. Handle Scaling
  35. Best Practices
  36. 1. Error Logging
  37. 2. Respect OpenAI’s Policies
  38. 3. Enhance User Interaction
  39. 4. Update Dependencies
  40. Conclusion
  41. Additional Resources
  42. Disclaimer

01Introduction

The rise of conversational AI has revolutionized the way users interact with applications. OpenAI’s ChatGPT, a state-of-the-art language model, enables developers to build applications that can understand and generate human-like text. Integrating ChatGPT into a Laravel web application can enhance user experience by providing intelligent and interactive features like chatbots, virtual assistants, and more.

In this comprehensive guide, we’ll walk you through the process of integrating ChatGPT into a Laravel web application. We’ll cover everything from setting up your Laravel project to handling user input and maintaining conversation context. By the end of this tutorial, you’ll have a fully functional web app where users can interact with ChatGPT in real-time.


02Table of Contents

  1. Prerequisites
  2. Setting Up the Laravel Project
  3. Installing the OpenAI PHP Client
  4. Configuring Environment Variables
  5. Creating Routes and Controllers
  6. Integrating ChatGPT
  7. Creating the Frontend Interface
  8. Maintaining Conversation Context
  9. Securing Your Application
  10. Deployment Considerations
  11. Best Practices
  12. Conclusion
  13. Additional Resources

03Prerequisites

Before we begin, ensure you have the following:

  • PHP 7.3+: Laravel requires PHP version 7.3 or higher.
  • Composer: Dependency management tool for PHP.
  • Laravel Installer: Optional but recommended.
  • Basic Knowledge of Laravel: Understanding of MVC architecture, routing, controllers, and views.
  • OpenAI API Key: Sign up on the OpenAI Platform to obtain your API key.
  • Development Environment: A code editor (like VSCode) and terminal access.
  • Node.js and NPM: Required for frontend assets (Laravel Mix).

04Setting Up the Laravel Project

1. Install Laravel

You can create a new Laravel project using Composer or the Laravel installer.

Using Composer

composer create-project --prefer-dist laravel/laravel chatgpt-laravel-app

Using Laravel Installer

laravel new chatgpt-laravel-app

Navigate into your project directory:

cd chatgpt-laravel-app

2. Configure Environment

Copy the .env.example file to .env:

cp .env.example .env

Generate an application key:

php artisan key:generate

3. Serve the Application

Start the Laravel development server:

php artisan serve

Visit http://127.0.0.1:8000 in your browser to see the Laravel welcome page.

05Installing the OpenAI PHP Client

We need a PHP library to interact with the OpenAI API.

1. Install OpenAI PHP Client

Install the OpenAI PHP client library via Composer:

composer require openai-php/client

As of my knowledge cutoff in September 2021, there isn’t an official OpenAI PHP SDK, but there are community-maintained packages like openai-php/client. Please check the latest documentation or use Guzzle HTTP client if necessary.

06Configuring Environment Variables

To keep your API key secure, we’ll store it in the .env file.

1. Update .env File

Add the following lines to your .env file:

OPENAI_API_KEY=your-openai-api-key
OPENAI_ORGANIZATION=your-openai-organization-id (if applicable)

Replace your-openai-api-key with your actual API key.

2. Update Configuration Files

You can create a configuration file for OpenAI or set the API key in your code.

For simplicity, we’ll set it directly in the code, but remember to reference the environment variable.


07Creating Routes and Controllers

We’ll create routes and controllers to handle user interactions.

1. Create Routes

Edit the routes/web.php file:

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ChatController;

Route::get('/', [ChatController::class, 'index']);
Route::post('/send-message', [ChatController::class, 'sendMessage'])->name('send.message');

2. Generate Controller

Create a new controller for handling chat:

php artisan make:controller ChatController

3. Implement Controller Methods

Edit app/Http/Controllers/ChatController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use OpenAI;

class ChatController extends Controller
{
    public function index()
    {
        return view('chat');
    }

    public function sendMessage(Request $request)
    {
        $userInput = $request->input('message');

        // Generate response from ChatGPT
        $responseText = $this->generateChatGPTResponse($userInput);

        return response()->json(['response' => $responseText]);
    }

    private function generateChatGPTResponse($userInput)
    {
        try {
            $client = OpenAI::client(env('OPENAI_API_KEY'));

            $response = $client->chat()->create([
                'model' => 'gpt-3.5-turbo', // Or the latest model
                'messages' => [
                    ['role' => 'system', 'content' => 'You are a helpful assistant.'],
                    ['role' => 'user', 'content' => $userInput],
                ],
                'max_tokens' => 150,
                'temperature' => 0.7,
            ]);

            return $response->choices[0]->message->content;
        } catch (\Exception $e) {
            \Log::error('ChatGPT Error: ' . $e->getMessage());
            return "I'm sorry, but I couldn't process your request.";
        }
    }
}

Notes:

  • Ensure you have proper error handling.
  • Logging errors can help with debugging.

08Integrating ChatGPT

1. Using OpenAI API Directly

If there’s no available PHP SDK, you can use Guzzle HTTP client:

composer require guzzlehttp/guzzle

Update the ChatController:

use GuzzleHttp\Client;

private function generateChatGPTResponse($userInput)
{
    try {
        $client = new Client();

        $response = $client->post('https://api.openai.com/v1/chat/completions', [
            'headers' => [
                'Authorization' => 'Bearer ' . env('OPENAI_API_KEY'),
                'Content-Type' => 'application/json',
            ],
            'json' => [
                'model' => 'gpt-3.5-turbo',
                'messages' => [
                    ['role' => 'system', 'content' => 'You are a helpful assistant.'],
                    ['role' => 'user', 'content' => $userInput],
                ],
                'max_tokens' => 150,
                'temperature' => 0.7,
            ],
        ]);

        $responseBody = json_decode($response->getBody(), true);

        return $responseBody['choices'][0]['message']['content'];
    } catch (\Exception $e) {
        \Log::error('ChatGPT Error: ' . $e->getMessage());
        return "I'm sorry, but I couldn't process your request.";
    }
}
// skip the reading, get the result
I'll build this for your business and handle what's technical.One call, a written scope, and the work delivered in weekly milestones.
Start a project →

09Creating the Frontend Interface

We’ll create a simple chat interface using Blade templates.

1. Create the Chat View

Create a resources/views/chat.blade.php file:

<!-- resources/views/chat.blade.php -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Chat with ChatGPT</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <style>
        /* Basic styles */
        body { font-family: Arial, sans-serif; margin: 20px; }
        #chatbox { width: 100%; max-width: 600px; height: 400px; border: 1px solid #ccc; overflow-y: scroll; padding: 10px; margin-bottom: 10px; }
        #messageInput { width: calc(100% - 100px); padding: 10px; }
        #sendButton { width: 80px; padding: 10px; }
        .message { margin: 5px 0; }
        .user { text-align: right; color: blue; }
        .bot { text-align: left; color: green; }
    </style>
</head>
<body>
    <h1>Chat with ChatGPT</h1>
    <div id="chatbox"></div>
    <input type="text" id="messageInput" placeholder="Type your message here" onkeydown="if(event.key === 'Enter') sendMessage()">
    <button id="sendButton" onclick="sendMessage()">Send</button>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        function sendMessage() {
            const userInputField = $('#messageInput');
            const userInput = userInputField.val().trim();
            if (!userInput) return;

            // Display user's message
            const chatbox = $('#chatbox');
            chatbox.append(`<div class="message user">You: ${userInput}</div>`);

            // Clear input field
            userInputField.val('');

            // Show typing indicator
            chatbox.append(`<div class="message bot" id="typingIndicator">ChatGPT is typing...</div>`);

            // Send the message to the server
            $.ajax({
                url: '{{ route('send.message') }}',
                type: 'POST',
                data: {
                    message: userInput,
                    _token: $('meta[name="csrf-token"]').attr('content'),
                },
                success: function(response) {
                    // Remove typing indicator
                    $('#typingIndicator').remove();

                    // Display bot's response
                    chatbox.append(`<div class="message bot">ChatGPT: ${response.response}</div>`);

                    // Auto-scroll to the bottom
                    chatbox.scrollTop(chatbox[0].scrollHeight);
                },
                error: function() {
                    // Remove typing indicator
                    $('#typingIndicator').remove();

                    // Display error message
                    chatbox.append(`<div class="message bot">ChatGPT: Sorry, there was an error processing your request.</div>`);
                    chatbox.scrollTop(chatbox[0].scrollHeight);
                }
            });
        }
    </script>
</body>
</html>

Notes:

  • We use jQuery for simplicity.
  • CSRF tokens are included for security.

10Maintaining Conversation Context

To make conversations more natural, we need to maintain the conversation history.

1. Using Sessions

We’ll store the conversation in the session.

Update the sendMessage method in ChatController:

use Illuminate\Support\Facades\Session;

public function sendMessage(Request $request)
{
    $userInput = $request->input('message');

    // Initialize messages in session
    $messages = Session::get('messages', []);
    if (empty($messages)) {
        $messages[] = ['role' => 'system', 'content' => 'You are a helpful assistant.'];
    }

    // Add user's message
    $messages[] = ['role' => 'user', 'content' => $userInput];

    // Generate response from ChatGPT
    $responseText = $this->generateChatGPTResponse($messages);

    // Add assistant's response
    $messages[] = ['role' => 'assistant', 'content' => $responseText];

    // Save messages back to session
    Session::put('messages', $messages);

    return response()->json(['response' => $responseText]);
}

2. Update the generateChatGPTResponse Method

Modify the method to accept conversation history:

private function generateChatGPTResponse($messages)
{
    try {
        $client = new Client();

        $response = $client->post('https://api.openai.com/v1/chat/completions', [
            'headers' => [
                'Authorization' => 'Bearer ' . env('OPENAI_API_KEY'),
                'Content-Type' => 'application/json',
            ],
            'json' => [
                'model' => 'gpt-3.5-turbo',
                'messages' => $messages,
                'max_tokens' => 150,
                'temperature' => 0.7,
            ],
        ]);

        $responseBody = json_decode($response->getBody(), true);

        return $responseBody['choices'][0]['message']['content'];
    } catch (\Exception $e) {
        \Log::error('ChatGPT Error: ' . $e->getMessage());
        return "I'm sorry, but I couldn't process your request.";
    }
}

3. Handling Session Timeout

You might want to handle session timeout or provide a way to reset the conversation.

Add a “Reset Conversation” button in your view:

<button id="resetButton" onclick="resetConversation()">Reset Conversation</button>

Implement the resetConversation function:

<script>
    function resetConversation() {
        $.ajax({
            url: '{{ route('reset.conversation') }}',
            type: 'POST',
            data: {
                _token: $('meta[name="csrf-token"]').attr('content'),
            },
            success: function() {
                $('#chatbox').empty();
            }
        });
    }
</script>

Update your routes in web.php:

Route::post('/reset-conversation', [ChatController::class, 'resetConversation'])->name('reset.conversation');

Add the method in ChatController:

public function resetConversation()
{
    Session::forget('messages');
    return response()->json(['status' => 'Conversation reset.']);
}

11Securing Your Application

Security is crucial when dealing with API keys and user data.

1. Protect Your API Key

  • Never expose your API key in client-side code.
  • Ensure your .env file is not committed to version control by adding it to .gitignore.

2. Use CSRF Protection

Laravel provides CSRF protection by default. Ensure your forms and AJAX requests include the CSRF token.

3. Input Validation

Validate and sanitize user inputs to prevent injection attacks.

public function sendMessage(Request $request)
{
    $request->validate([
        'message' => 'required|string|max:500',
    ]);

    // ... rest of the code ...
}

4. Use HTTPS in Production

Ensure your application uses HTTPS to encrypt data in transit.

12Deployment Considerations

When deploying your application, consider the following:

1. Environment Variables

Set your environment variables securely on your production server, not in the .env file.

2. Use a Proper Web Server

Use a web server like Nginx or Apache with PHP-FPM to serve your Laravel application.

3. Optimize Your Application

Run optimization commands before deployment:

php artisan config:cache
php artisan route:cache
php artisan view:cache

4. Handle Scaling

For applications expecting high traffic:

  • Use a load balancer.
  • Consider storing session data in a database or cache like Redis.
  • Be mindful of OpenAI’s API rate limits.

13Best Practices

1. Error Logging

Implement proper logging to monitor errors and system performance.

\Log::error('Error message');

2. Respect OpenAI’s Policies

Ensure your application complies with OpenAI’s usage policies.

3. Enhance User Interaction

  • Persistent Storage: Use databases to store conversation history for authenticated users.
  • User Authentication: Implement authentication if needed.
  • Frontend Frameworks: Consider using Vue.js (which comes with Laravel) or React for a more dynamic frontend.

4. Update Dependencies

Keep your packages and dependencies up to date to benefit from security patches and new features.


14Conclusion

Congratulations! You’ve successfully integrated ChatGPT into a Laravel web application. This powerful combination allows you to create interactive and intelligent applications capable of understanding and responding to user inputs in a conversational manner.

This guide covered the essential steps to set up the application, create a user interface, integrate with the OpenAI API, maintain conversation context, and deploy the application securely.

Feel free to expand upon this foundation by adding new features, improving the user interface, or integrating with other services. The possibilities are endless when combining the robustness of Laravel with the intelligence of ChatGPT.


15Additional Resources

16Disclaimer

Ensure that you comply with OpenAI’s policies and guidelines when using their API. Always handle user data responsibly and secure your application against potential vulnerabilities.

// comments

Leave a comment

// your email is never published

Reading about it is good.
Having it built is better.

Booking platforms, business systems, online stores and AI assistants — scoped on one call, built in weekly milestones.

// you pay as each milestone is delivered
share