Using GuzzleHTTP to integrate the ChatGPT API into PHP provides a streamlined and fast solution to harness advanced natural language processing capabilities in your applications. We’ll walk you through a step-by-step procedure for using the ChatGPT API in PHP, utilising the GuzzleHTTP framework for seamless connection with the API endpoint.
Before we get into the details, it’s important to grasp the importance of including ChatGPT into your PHP projects.
ChatGPT, created by OpenAI, allows your apps to interpret and generate human-like prose, making it an effective tool for developing intelligent chatbots, language translation services, and more.
Let’s get started.
What is ChatGPT?
ChatGPT is a language model developed by OpenAI. It is part of the GPT (Generative Pre-trained Transformer) series, specifically GPT-3.5-turbo. ChatGPT is designed to generate human-like responses based on the input it receives.
Key features of ChatGPT include:
- Natural Language Understanding
- Contextual Understanding
- Large-scale Pre-training
Create an Account and Generate OpenAI API Key
To make use of ChatGPT, create an account on OpenAI platform to generate API keys.
You have to pass an email address and password. It will send you a verification email and later on asks for basic information like Name, Phone number, DOB etc.
Once your account will be set successfully
Click on Create New Secret Key
Copy generated secret key.
Composer Package Installation “guzzlehttp”
Open project terminal and run this command,
composer require guzzlehttp/guzzle
It will install package and setup vendor folder into project.
Use of ChatGPT API in PHP Using “guzzlehttp”
Here, you can see we have the complete source code of ChatGPT API Integration in PHP.
<?php ini_set("display_errors", 1); // Print errors if any require "vendor/autoload.php"; // Include the Composer autoloader use GuzzleHttp\Client; $openAISecretKey = "sk-UKlics6Ct2So5noVWWGzT3B..."; $apiUrl = "https://api.openai.com/v1/chat/completions"; $search = "How artificial intelligence is transforming the world?"; // Set headers $headers = [ "headers" => [ "Content-Type" => "application/json", "Authorization" => "Bearer " . $openAISecretKey, ], ]; // Set data $data = [ "json" => [ "model" => "gpt-3.5-turbo", "messages" => [ [ "role" => "user", "content" => $search, ], ], "temperature" => 0.5, "max_tokens" => 200, "top_p" => 1.0, "frequency_penalty" => 0.52, "presence_penalty" => 0.5, "stop" => ["11."], ], ]; $client = new Client(); // Merge headers with the data $options = array_merge($data, $headers); // Make the API request using Guzzle $response = $client->post($apiUrl, $options); // Decode and print the API response $result = json_decode($response->getBody(), true); echo "<pre>"; print_r($result);
You will get all keys in response.
To get only proper message output of your query,
We hope this article helped you to learn about How To Use ChatGPT API in PHP Using guzzlehttp Tutorial Example in a very detailed way.
Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.