In our previous tutorial, we have explained how to Encrypt and Decrypt Password in PHP. In this tutorial, we will explain How To Use ChatGPT API with PHP.
ChatGPT is a language model developed by OpenAI. It’s amazing with it’s some outstanding functionalities such as write essays, stories, solve coding related problems, debug your code and all sorts of scripts etc.
The ChatGPT API can be used with any programming language. So here we will explain how to use ChatGPT API using PHP. We will make ChatGPT API request using PHP and make a Question and Answer Application to make question and get answer.
So let’s proceed to make a Question and Answer Application using ChatGPT API with PHP.
Make Question and Answer Interface
We will create a PHP file index.php and design form using Bootstrap4 to ask question. So we include Bootstrap4 library files.
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css"> <script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
Then we will design our page with Form to ask question.
<form method="post" action=""> <div class="form-group"> <label for="email">Your Question Here:</label> <textarea class="form-control" id="question" name="question" rows="5"></textarea> </div> <button type="submit" class="btn btn-primary">Get Answer</button> </form>
We will check for form submit and create object of class OpenAi.php and call method getAnswer() to get question answer from ChatGPT.
<?php if(!empty($_POST["question"]) && $_POST['question']) { include('class/OpenAi.php'); $openai = new OpenAi(); $openai->question = $_POST['question']; ?> <span class="text-danger font-weight-bold">Question : </span> <span class="font-weight-normal" style="margin-left:10px;"><?php echo ucfirst($_POST['question']); ?><span><br> <span class="text-success font-weight-bold">Answer : </span> <span class="font-weight-normal" style="margin-left:10px;"> <?php echo $openai->getAnswer(); ?> <span> <?php } ?>
Make ChatGPT API Call with PHP
We will create a class OpenAi.php and implement method getAnswer() to make ChatGPT API call using PHP Curl and get response data to display it. We need to get API KEY from OpenAi to make API request.
<?php class OpenAi { private $dTemperature = 0.9; private $iMaxTokens = 100; private $top_p = 1; private $frequency_penalty = 0.0; private $presence_penalty = 0.0; private $sModel = "text-davinci-003"; private $ApiKey = 'YOUR-API-KEY'; public function getAnswer(){ if($this->question) { $ch = curl_init(); $headers = [ 'Accept: application/json', 'Content-Type: application/json', 'Authorization: Bearer ' . $this->ApiKey . '' ]; $postData = [ 'model' => $this->sModel, 'prompt' => str_replace('"', '', $this->question), 'temperature' => $this->dTemperature, 'max_tokens' => $this->iMaxTokens, 'top_p' => $this->top_p, 'frequency_penalty' => $this->frequency_penalty, 'presence_penalty' => $this->presence_penalty, 'stop' => '[" Human:", " AI:"]', ]; curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/completions'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData)); $result = curl_exec($ch); $decoded_json = json_decode($result, true); return ($decoded_json['choices'][0]['text']); } } } ?>