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

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

藏色散人
藏色散人 forward
2023-03-16 15:41:11 1792browse

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

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.php      My ChatGPT App    
@yield('content')

Step 4: Create index page

// chatgpt/index.blade.php @extends('layouts.app') @section('content') 
Ask something to ChatGPT
@csrf
@endsection

Step 5: Create response page

// chatgpt/response.blade.php @extends('layouts.app') @section('content') 
ChatGPT answer

{{ $response }}

@endsection

Finally step 6: Create a .env variable

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

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!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete