Skip to content

Instantly share code, notes, and snippets.

@BenceBakos
Last active January 4, 2023 08:06
Show Gist options
  • Save BenceBakos/a5233de34a69cc58b9b7dd090d927195 to your computer and use it in GitHub Desktop.
Save BenceBakos/a5233de34a69cc58b9b7dd090d927195 to your computer and use it in GitHub Desktop.
How to use OpenAi's ChatGPT in PHP?

How to use OpenAi's ChatGPT in PHP?

Intro

There are a few reasons why you might want to use ChatGPT:

  1. It's easy to use: ChatGPT is a pre-trained language model that is easy to use through the OpenAI API. You can generate responses to prompts with just a few lines of code, without having to worry about training your own model.
  2. It can generate human-like responses: ChatGPT is trained on a large dataset of human conversations, so it can generate responses that sound similar to what a human might say. This can be useful if you're building a chatbot or other application that requires natural-sounding responses.
  3. It can be fine-tuned: While ChatGPT is a pre-trained model, you can also fine-tune it on your own dataset to make it even more accurate and relevant to your use case. Fine-tuning can be especially useful if you have a specific domain or topic that you want the model to be knowledgeable about.
  4. This introduction, and the majority of the snippets are written by ChatGPT, with minor corrections it's amazing!

Setup

Install Guzzle:

composer require guzzlehttp/guzzle

Sign up for API key

The ChatGPT model is provided by OpenAI, and it is available to use for free through the OpenAI API. In order to use the OpenAI API, you'll need to sign up for an API key, which is also free. However, the API does have rate limits and usage limits, so you may need to pay for additional usage if you exceed these limits. You can find more information about the pricing and usage limits for the OpenAI API on the OpenAI API website.

Create API key here.

Wrapper class

use GuzzleHttp\Client;

class OpenAiApi
{
    private $apiKey;
    private $client;

    public function __construct(string $apiKey)
    {
        $this->apiKey = $apiKey;
        $this->client = new Client([
            'base_uri' => 'https://api.openai.com/v1/',
        ]);
    }

    public function generateResponse(string $prompt, float $temperature = 0.5, int $maxTokens = 2048, string $model = "text-davinci-003") : string
    {
        $response = $this->client->post('completions', [
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => "Bearer {$this->apiKey}",
            ],
            'json' => [
                'model' => $model,
                'prompt' => $prompt,
                'temperature' => $temperature,
                'max_tokens' => $maxTokens,
            ],
        ]);

        return json_decode($response->getBody())->choices[0]->text;
    } 
}

ChatGPT provided me a response with the now deprecated engine parameter, I've replaced that with model and made it configurable as well. It also faild to use the correct completions API resource, instead it used text_completions

Usage

$chatGPT = new OpenAiApi('<API_KEY_HERE>');
$response = $chatGPT->generateResponse('What is the first known example of working AI?');
echo $response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment