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
##I assume you have used the official documentation Installed the Laravel 10 frameworkStep 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('chatgpt.index'); } public function ask(Request $request) { $prompt = $request->input('prompt'); $response = $this->askToChatGPT($prompt); return view('chatgpt.response', ['response' => $response]); } private function askToChatGPT($prompt) { $response = Http::withoutVerifying() ->withHeaders([ 'Authorization' => 'Bearer ' . env('CHATGPT_API_KEY'), 'Content-Type' => 'application/json', ])->post('https://api.openai.com/v1/engines/text-davinci-003/completions', [ "prompt" => $prompt, "max_tokens" => 1000, "temperature" => 0.5 ]); return $response->json()['choices'][0]['text']; } }
<?php use App\Http\Controllers\ChatGPTController; use Illuminate\Support\Facades\Route; (...) Route::get('/chatgpt', [ChatGPTController::class, 'index']) ->name('chatgpt.index'); Route::post('/chatgpt/ask', [ChatG²PTController::class, 'ask']) ->name('chatgpt.ask');
// 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('css/app.css') }}" 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('content') </div> </body> </html>
// chatgpt/index.blade.php @extends('layouts.app') @section('content') <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('chatgpt.ask') }}"> @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
// chatgpt/response.blade.php @extends('layouts.app') @section('content') <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
CHATGPT_API_KEY=YOUR_API_KEY
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 If you want more examples, you can go to the official examples section: platform.openai.com/examplesRecommended 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!