PHP code example of phpexperts / chatgpt-speaker

1. Go to this page and download the library: Download phpexperts/chatgpt-speaker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phpexperts / chatgpt-speaker example snippets


    $chatGPT = new ChatGPTSpeaker();

    $prompt = <<<PROMPT
    Please create a table of the PHP major version releases along with the date of release.
    PROMPT;
    $response = $chatGPT->prompt($prompt);

    $chatGPT = new ChatGPTSpeaker();
    $chatGPT->returnText();

    $prompt = <<<PROMPT
        Please create a table of the PHP major version releases along with the date of release.
    PROMPT;
    $response = $chatGPT->prompt($prompt);

 

$curlCLI = $chatGPT->api->http->testHandler->getRecords()[0];

Output: 
curl 'https://api.openai.com/v1/chat/completions' -A 'PHPExperts/RESTSpeaker-2.4 (PHP 8.3.3)' \
   -H 'Content-Type: application/json'  -H 'Authorization: Bearer [redacted]' \ 
   -X POST  -d '{"model":"gpt-3.5-turbo","messages":[{"role":"user","content":"Say 'Hello, World!'."}]}'

$rawReturn = (string)$chatGPT->api->getLastResponse()->getBody();
JSON
{
  "model": "gpt-3.5-turbo",
  "messages": {
    "0": {
      "role": "user",
      "content": "format responses in JSON"
    },
    "1": {
      "role": "user",
      "content": "    Please create a table of the PHP major version releases along with the date of release."
    },
    "role": "assistant",
    "content": {
      "php_major_version_releases": [
        {
          "version": "PHP 3",
          "release_date": "June 6, 1998"
        },
        {
          "version": "PHP 4",
          "release_date": "May 22, 2000"
        },
        {
          "version": "PHP 5",
          "release_date": "July 13, 2004"
        },
        {
          "version": "PHP 6",
          "release_date": "No official release"
        },
        {
          "version": "PHP 7",
          "release_date": "December 3, 2015"
        },
        {
          "version": "PHP 8",
          "release_date": "November 26, 2020"
        }
      ]
    }
  }
}