There are a few reasons why you might want to use ChatGPT:
- 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.
- 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.
- 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.
- This introduction, and the majority of the snippets are written by ChatGPT, with minor corrections it's amazing!
Install Guzzle:
composer require guzzlehttp/guzzle
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.
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
$chatGPT = new OpenAiApi('<API_KEY_HERE>');
$response = $chatGPT->generateResponse('What is the first known example of working AI?');
echo $response;