ChatGPT PHP SDK | Package
Official and Reverse Engineered ChatGPT API for PHP.
Reconstruct from @acheong08's ChatGPT
composer require haozi-team/chatgpt-php
Uses
chat.openai.com
- Free
- Rate limited
- Needs Bypassing Cloudflare
Default bypass server is
chatgpt.duti.tech
by @acheong08Rate limit: 25 requests per 10 seconds (per IP)
OpenAI rate limit: 50 requests per hour on free accounts. You can get around it with multi-account cycling
Plus accounts has around 150 requests per hour rate limit
- Create account on OpenAI's ChatGPT
- Save your email and password
Login OpenAI account and go to https://chat.openai.com/api/auth/session to get your access_token.
{
"access_token": "<access_token>"
}
The access_token is valid for 30 days.
<?php
use HaoziTeam\ChatGPT\V1 as ChatGPTV1;
$chatGPT = new ChatGPTV1();
$chatGPT->addAccount('<your_access_token>');
$answer = $chatGPT->ask('Hello, how are you?');
print_r($answer);
//Array(
// 'answer' => 'I am fine, thank you.',
// 'conversation_id' => '<uuid>',
// 'parent_id' => '<uuid>',
// 'model' => 'text-davinci-002-render-sha',
// 'account' => '0',
//)
You can pass "baseUrl" to the first parameter to set a custom API endpoint.
<?php
use HaoziTeam\ChatGPT\V1 as ChatGPTV1;
$chatGPT = new ChatGPTV1('https://chat.openai.com/backend-');
More refer to the wiki for advanced developer usage.
Recently released by OpenAI
- Costs money
Get API key from https://platform.openai.com/account/api-keys
<?php
use HaoziTeam\ChatGPT\V2 as ChatGPTV2;
$chatGPT = new ChatGPTV2('sk-<your_api_key>');
$chatGPT->addMessage('You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.', 'system');
$answer = $chatGPT->ask('Hello, how are you?');
print_r($answer);
You can pass "baseUrl" to the second parameter to set a custom API endpoint.
<?php
use HaoziTeam\ChatGPT\V2 as ChatGPTV2;
$chatGPT = new ChatGPTV2('sk-<your_api_key>', 'https://api.openai.com/');
You can use addMessage
to add messages to the conversation.
<?php
use HaoziTeam\ChatGPT\V2 as ChatGPTV2;
$chatGPT = new ChatGPTV2('sk-<your_api_key>');
$chatGPT->addMessage('You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.', 'system');
$chatGPT->addMessage('Hello, how are you?', 'user');
$chatGPT->addMessage('I am fine, thank you.', 'assistant');
$answer = $chatGPT->ask('What did I ask you before?');
print_r($answer);
//Array(
// 'answer' => 'Hello, how are you?',
// 'id' => 'cmpl-xxxxx',
// 'model' => 'gpt-3.5-turbo',
// 'usage' => [
// "prompt_tokens": 9,
// "completion_tokens": 12,
// "total_tokens": 21,
// ],
//)
You can set the stream
parameter to true
to get a stream for output answers as they are generated.
<?php
use HaoziTeam\ChatGPT\V2 as ChatGPTV2;
$chatGPT = new ChatGPTV2('sk-<your_api_key>');
$chatGPT->addMessage('You are ChatGPT, a large language model trained by OpenAI. Answer as concisely as possible.', 'system');
$answer = $chatGPT->ask('Hello, how are you?', null, true);
// GuzzleHttp StreamInterface
This is not an official OpenAI product. This is a personal project and is not affiliated with OpenAI in any way. Don't sue me.
- acheong08 - Python ChatGPT API