ChatGPT PHP Development

OpenAI is a leading artificial intelligence research organization that has made significant advancements in natural language processing and computer vision. They offer a variety of APIs that enable developers to create applications that generate text and images using state-of-the-art machine learning models. In this blog post, we’ll discuss how to interact with the OpenAI API using PHP and provide sample code for generating text and images using ChatGPT and DALL-E models.

Recently, I asked ChatGPT to write a PHP script that can interact with the OpenAI API, specifically ChatGPT and DALL-E, to generate text and images. The response was generated using the GPT-3.5 architecture and
included some initial basic code:

<?php

// Replace with your API key
$apiKey = 'YOUR_API_KEY_HERE';

// The API endpoint for ChatGPT
$chatgptEndpoint = 'https://api.openai.com/v1/engines/davinci-codex/completions';

// The API endpoint for DALL-E
$dalleEndpoint = 'https://api.openai.com/v1/images/generations';

// The text prompt for ChatGPT
$chatgptPrompt = 'Hello, how are you?';

// The image prompt for DALL-E
$dallePrompt = 'an armchair made out of pizza';

// The maximum number of tokens to generate for ChatGPT
$chatgptMaxTokens = 64;

// The maximum width and height of the generated image for DALL-E
$dalleSize = '512x512';

// Set up the HTTP headers for the API request
$headers = array(
  'Content-Type: application/json',
  'Authorization: Bearer ' . $apiKey
);

// Set up the JSON payload for the ChatGPT API request
$chatgptPayload = array(
  'prompt' => $chatgptPrompt,
  'max_tokens' => $chatgptMaxTokens
);

// Set up the JSON payload for the DALL-E API request
$dallePayload = array(
  'model' => 'image-alpha-001',
  'prompt' => $dallePrompt,
  'size' => $dalleSize
);

// Send the ChatGPT API request
$chatgptResponse = httpPost($chatgptEndpoint, $headers, json_encode($chatgptPayload));

// Send the DALL-E API request
$dalleResponse = httpPost($dalleEndpoint, $headers, json_encode($dallePayload));

// Output the results
echo 'ChatGPT response: ' . $chatgptResponse . "\n";
echo 'DALL-E response: ' . $dalleResponse . "\n";

// Helper function for making HTTP POST requests
function httpPost($url, $headers, $data) {
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

  $response = curl_exec($ch);

  curl_close($ch);

  return $response;
}
?>

I then asked ChatGPT to rewrite the code using classes, so I could further extend it for an application I intend to create to help write the posts for this site. Here are the final results that utilize the httpPost() method to make HTTP POST requests to the OpenAI API. The ChatGPT and Dalle classes extend the OpenAI class and have their own properties and methods for generating text and images.

<?php

class OpenAI {
  protected $apiKey;

  public function __construct($apiKey) {
    $this->apiKey = $apiKey;
  }

  protected function httpPost($url, $headers, $data) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

    $response = curl_exec($ch);

    curl_close($ch);

    return $response;
  }
}

class ChatGPT extends OpenAI {
  protected $endpoint = 'https://api.openai.com/v1/engines/davinci-codex/completions';
  protected $prompt;
  protected $maxTokens;

  public function __construct($apiKey, $prompt, $maxTokens) {
    parent::__construct($apiKey);
    $this->prompt = $prompt;
    $this->maxTokens = $maxTokens;
  }

  public function generateText() {
    $headers = array(
      'Content-Type: application/json',
      'Authorization: Bearer ' . $this->apiKey
    );

    $payload = array(
      'prompt' => $this->prompt,
      'max_tokens' => $this->maxTokens
    );

    $response = $this->httpPost($this->endpoint, $headers, json_encode($payload));

    return $response;
  }
}

class Dalle extends OpenAI {
  protected $endpoint = 'https://api.openai.com/v1/images/generations';
  protected $prompt;
  protected $size;

  public function __construct($apiKey, $prompt, $size) {
    parent::__construct($apiKey);
    $this->prompt = $prompt;
    $this->size = $size;
  }

  public function generateImage() {
    $headers = array(
      'Content-Type: application/json',
      'Authorization: Bearer ' . $this->apiKey
    );

    $payload = array(
      'model' => 'image-alpha-001',
      'prompt' => $this->prompt,
      'size' => $this->size
    );

    $response = $this->httpPost($this->endpoint, $headers, json_encode($payload));

    return $response;
  }
}

// Example usage
$apiKey = 'YOUR_API_KEY_HERE';

$chatgpt = new ChatGPT($apiKey, 'Hello, how are you?', 64);
$chatgptResponse = $chatgpt->generateText();
echo 'ChatGPT response: ' . $chatgptResponse . "\n";

$dalle = new Dalle($apiKey, 'an armchair made out of pizza', '512x512');
$dalleResponse = $dalle->generateImage();
echo 'DALL-E response: ' . $dalleResponse . "\n";
?>

To use the script, you will need to sign up for the OpenAI API and obtain an API key. Once you have the API key, you can instantiate the ChatGPT and Dalle classes with the API key, prompt, and other parameters. Then, you can call the generateText() and generateImage() methods to generate text and images using ChatGPT and DALL-E models, respectively.

 

Leave a comment