Home>Article>PHP Framework> Teach you step by step how to use ChatGPT in Laravel10 project
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
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']; } }Step 2: Create a route
name('chatgpt.index'); Route::post('/chatgpt/ask', [ChatG²PTController::class, 'ask']) ->name('chatgpt.ask');Step 3: Create a layout
// layouts/app.blade.phpStep 4: Create index pageMy ChatGPT App @yield('content')
// chatgpt/index.blade.php @extends('layouts.app') @section('content')Step 5: Create response page@endsectionAsk something to ChatGPT
// chatgpt/response.blade.php @extends('layouts.app') @section('content')Finally step 6: Create a .env variable@endsectionChatGPT answer{{ $response }}
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 keylaravel 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!