EACOMM Corporation Philippines

News and Articles from the Philippines' Premier Multimedia and Web Developer

Articles Current Projects Tutorials

Developing an OpenAI GPT-3 Chatbot with PHP Programming

We have been very impressed with the power and ease of use of OpenAI’s GPT-3 AI chat and how it can easily create a chatbot that provides relevant answers with very little training. For a live example of this click on the chat button on our corporate website: www.eacomm.com.

OpenAI powered GPT-3 Chatbot developed by EACOMM Corporation

To show you just how powerful OpenAI’s new chat is, we asked it how to create an OpenAI chatbot using the PHP programming language. Below is its answer:

To create a chatbot using OpenAI’s API in PHP, you will need to do the following:

  1. Sign up for an OpenAI API key at https://beta.openai.com/signup.
  2. Install the Guzzle HTTP library for PHP, which will be used to make HTTP requests to the OpenAI API. You can do this by running the following command:
composer require guzzlehttp/guzzle
  1. Use the Guzzle library to send a POST request to the OpenAI API with your text input and receive a response containing the chatbot’s output.

Here is some example code that demonstrates how to do this:

<?php

require 'vendor/autoload.php';

$apiKey = 'YOUR_API_KEY';
$prompt = 'What is the weather like today?';

$client = new GuzzleHttp\Client();
$response = $client->post('https://api.openai.com/v1/models/text-davinci-002/completions', [
    'headers' => [
        'Content-Type' => 'application/json',
        'Authorization' => "Bearer $apiKey"
    ],
    'json' => [
        'prompt' => $prompt,
        'max_tokens' => 2048,
        'temperature' => 0.5,
        'top_p' => 1,
        'frequency_penalty' => 0,
        'presence_penalty' => 0
    ]
]);

$responseJson = json_decode((string) $response->getBody());
$chatbotResponse = $responseJson->choices[0]->text;

echo $chatbotResponse;

This code will send a request to the OpenAI API with the text “What is the weather like today?” and print the chatbot’s response to the console.

For assistance and information in developing chatbots and other AI-powered text-based technologies, contact EACOMM Corporation at info@eacomm.com.

%d bloggers like this: