Coder Social home page Coder Social logo

chatgpt-php's Introduction

ChatGPT PHP SDK | Package

Total Downloads Latest Stable Version License

Official and Reverse Engineered ChatGPT API for PHP.

Reconstruct from @acheong08's ChatGPT

Installation

composer require haozi-team/chatgpt-php

V1 Web ChatGPT

Uses chat.openai.com

  • Free
  • Rate limited
  • Needs Bypassing Cloudflare

Default api endpoint is https://ai.fakeopen.com/api/ by @pengzhile

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

Arkose Token: Recently, OpenAI began to require Arkose Token while bypassing Cloudflare request conversation API, usually the SDK can get it automatically through @pengzhile's API

Configuration

  1. Create account on OpenAI's ChatGPT
  2. Save your email and password

Authentication

- Access token

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.

Developer API

Basic example

<?php
use HaoZiTeam\ChatGPT\V1 as ChatGPTV1;

$chatGPT = new ChatGPTV1();
$chatGPT->addAccount('<your_access_token>');

$answers = $chatGPT->ask('Hello, how are you?');
foreach ($answers as $item) {
    print_r($item);
}
//Array(
//    'answer' => 'I am fine, thank you.',
//    'conversation_id' => '<uuid>',
//    'parent_id' => '<uuid>',
//    'model' => 'text-davinci-002-render-sha',
//    'account' => '0',
//)

Advanced example

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-api/');

More refer to the wiki for advanced developer usage.

V2 Official ChatGPT

Recently released by OpenAI

  • Costs money

Get API key from https://platform.openai.com/account/api-keys

Developer API

Basic example

<?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');

$answers = $chatGPT->ask('Hello, how are you?');
foreach ($answers as $item) {
    print_r($item);
}

Advanced example

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');

$answers = $chatGPT->ask('What did I ask you before?');
foreach ($answers as $item) {
    print_r($item);
}
//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');

$answers = $chatGPT->ask('Hello, how are you?', null, true);// A Generator
foreach ($answers as $item) {
    print_r($item);
}

Disclaimers

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.

Credits

chatgpt-php's People

Contributors

anthonymedinawork avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.