Home > PHP Framework > Laravel > Teach you step by step how to use ChatGPT in Laravel10 project

Teach you step by step how to use ChatGPT in Laravel10 project

藏色散人
Release: 2023-03-16 15:41:11
forward
1986 people have browsed it

This article brings you relevant knowledge about Laravel, which mainly introduces how to use ChatGPT in the Laravel10 project? For those who are interested, take a look below, I hope it will be helpful to you.

Use ChatGPT in your Laravel 10 project!

What you get

Teach you step by step how to use ChatGPT in Laravel10 project

Teach you step by step how to use ChatGPT in Laravel10 project

##I assume you have used the official documentation Installed the Laravel 10 framework

Step 1: Create a controller

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class ChatGPTController extends Controller
{
    public function index()
    {
        return view(&#39;chatgpt.index&#39;);
    }
    public function ask(Request $request)
    {
        $prompt = $request->input(&#39;prompt&#39;);
        $response = $this->askToChatGPT($prompt);
        return view(&#39;chatgpt.response&#39;, [&#39;response&#39; => $response]);
    }
    private function askToChatGPT($prompt) 
    {
        $response = Http::withoutVerifying()
            ->withHeaders([
                &#39;Authorization&#39; => &#39;Bearer &#39; . env(&#39;CHATGPT_API_KEY&#39;),
                &#39;Content-Type&#39; => &#39;application/json&#39;,
            ])->post(&#39;https://api.openai.com/v1/engines/text-davinci-003/completions&#39;, [
                "prompt" => $prompt,
                "max_tokens" => 1000,
                "temperature" => 0.5
            ]);
        return $response->json()[&#39;choices&#39;][0][&#39;text&#39;];
    }
}
Copy after login

Step 2: Create a route

<?php
use App\Http\Controllers\ChatGPTController;
use Illuminate\Support\Facades\Route;
(...)
Route::get(&#39;/chatgpt&#39;, [ChatGPTController::class, &#39;index&#39;])
    ->name(&#39;chatgpt.index&#39;);
Route::post(&#39;/chatgpt/ask&#39;, [ChatG²PTController::class, &#39;ask&#39;])
    ->name(&#39;chatgpt.ask&#39;);
Copy after login

Step 3: Create a layout

// layouts/app.blade.php
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>My ChatGPT App</title>
        <!-- Styles -->
        <link href="{{ asset(&#39;css/app.css&#39;) }}" rel="stylesheet">
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
    </head>
    <body>
        <div class="container mt-5">
            @yield(&#39;content&#39;)
        </div>
    </body>
</html>
Copy after login

Step 4: Create index page

// chatgpt/index.blade.php
@extends(&#39;layouts.app&#39;)
@section(&#39;content&#39;)
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Ask something to ChatGPT</div>
                    <div class="card-body">
                        <form method="POST" action="{{ route(&#39;chatgpt.ask&#39;) }}">
                            @csrf
                            <div class="form-group">
                                <input type="text" class="form-control text-center" name="prompt" placeholder="Ask something...">
                            </div>
                            <button type="submit" class="btn btn-primary">Send</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
Copy after login

Step 5: Create response page

// chatgpt/response.blade.php
@extends(&#39;layouts.app&#39;)
@section(&#39;content&#39;)
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">ChatGPT answer</div>
                    <div class="card-body">
                        <p>{{ $response }}</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
Copy after login

Finally step 6: Create a .env variable

CHATGPT_API_KEY=YOUR_API_KEY
Copy after login

Get ChatGPT API Key

To obtain the API key you can go to the api-keys section in your openai platform account and generate your key

Teach you step by step how to use ChatGPT in Laravel10 project

If you want more examples, you can go to the official examples section: platform.openai.com/examples

Recommended learning: "

laravel video tutorial"

The above is the detailed content of Teach you step by step how to use ChatGPT in Laravel10 project. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template