2023-02-11
With the help of @PawanOsman, we've figured out a way to continue using the ChatGPT raw models directly. In an attempt to prevent the models from being disabled again, we've decided to provide a reverse proxy (i.e. a private API server) with a completions endpoint that's compatible with the OpenAI API. I've updated ChatGPTClient
to support using a reverse proxy URL endpoint instead of the default OpenAI API endpoint. See Using a Reverse Proxy for more information.
Please note that if you choose to go this route, you are exposing your API key or session token to a third-party server. If you are concerned about this, you may choose to use the official OpenAI API instead, with the text-davinci-003
model.
2023-02-10
I've found a new working model for text-chat-davinci-002
, text-chat-davinci-002-sh-alpha-aoruigiofdj83
. This is the raw model that the ChatGPT Plus "Turbo" version uses. Responses are blazing fast. I've updated the library to use this model.
Bad timing; text-chat-davinci-002-sh-alpha-aoruigiofdj83
was removed shortly after, possibly due to a new model somewhere out there?
2023-02-09
Experience the power of Bing's GPT-4 version of ChatGPT with BingAIClient
(experimental).
The API server and CLI still need to be updated to support this, but you can use the client directly right now.
Please note that if your account is still wait-listed, you will not be able to use this client.
2023-02-08
Even though text-chat-davinci-002-20221122
is back up again, it seems like it's constantly overloaded and returns a 429 error. It's likely that OpenAI only dedicated a small amount of resources to this model to prevent it being widely used by the public. Additionally, I've heard that newer versions are now access-locked to OpenAI employees and partners, so it's unlikely that we'll be able to find any workarounds until the model is officially released.
You may use the text-davinci-003
model instead as a drop-in replacement. Keep in mind that text-davinci-003
is not as good as text-chat-davinci-002
(which is trained via RHLF and fine-tuned to be a conversational AI), though results are still pretty good in most cases. Please note that using text-davinci-003
will cost you credits ($).
I will be re-adding support for the browser-based ChatGPT for the API server and CLI. Please star and watch this repository for updates.
2023-02-07
The roller coaster has reached the next stop. text-chat-davinci-002-20221122
is back up again.
Trying to use text-chat-davinci-002-20221122
with the OpenAI API now returns a 404 error.
You may use the text-davinci-003
model instead as a drop-in replacement. Keep in mind that text-davinci-003
is not as good as text-chat-davinci-002
(which is trained via RHLF and fine-tuned to be a conversational AI), though results are still very good. Please note that using text-davinci-003
will cost you credits ($).
Please hold for further updates as we investigate further workarounds.
2023-02-02
Trying to use text-chat-davinci-002-20230126
with the OpenAI API now returns a 404 error. Someone has already found the new model name, but they are unwilling to share at this time. I will update this repository once I find the new model. If you have any leads, please open an issue or a pull request.
In the meantime, I've added support for models like text-davinci-003
, which you can use as a drop-in replacement. Keep in mind that text-davinci-003
is not as good as text-chat-davinci-002
(which is trained via RHLF and fine-tuned to be a conversational AI), though results are still very good. Please note that using text-davinci-003
will cost you credits ($).
Discord user @pig#8932 has found a working text-chat-davinci-002
model, text-chat-davinci-002-20221122
. I've updated the library to use this model.
A ChatGPT implementation with support for Bing's GPT-4 version of ChatGPT, plus the official ChatGPT model via OpenAI's API. Available as a Node.js module, REST API server, and CLI app.
This is an implementation of ChatGPT, with support for Bing's GPT-4 version of ChatGPT, plus the official ChatGPT raw model, text-chat-davinci-002
.
An experimental client for Bing's GPT-4 version of ChatGPT is available in BingAIClient
. It works much like ChatGPT, but it's powered by GPT-4 instead of GPT-3. For more information on its capabilities and limitations, see this Reddit comment.
The model name text-chat-davinci-002-20230126
was briefly leaked while I was inspecting the network requests made by the official ChatGPT website, and I discovered that it works with the OpenAI API. Since then, that model and others have been disabled, but I'm keeping this repo updated with the newer versions of text-chat-davinci-002
as we find them. Usage of this model currently does not cost any credits.
As far as I'm aware, I was the first one who discovered this, and usage of the model has since been implemented in libraries like acheong08/ChatGPT and transitive-bullshit/chatgpt-api as we collaborated and shared knowledge.
By itself, the model does not have any conversational support, so ChatGPTClient
uses a cache to store conversations and pass them to the model as context. This allows you to have persistent conversations with ChatGPT in a nearly identical way to the official website.
- Experimental support for Bing's version of ChatGPT, powered by GPT-4.
- Support for the official ChatGPT raw model,
text-chat-davinci-002
, via OpenAI's API. - Includes an API server (with Docker support) you can run to use ChatGPT in non-Node.js applications.
- Includes a
ChatGPTClient
andBingAIClient
class that you can use in your own Node.js applications. - Includes a CLI interface where you can chat with ChatGPT.
- (
ChatGPTClient
) Replicates chat threads from the official ChatGPT website (with conversation IDs and message IDs), with persistent conversations using Keyv.- Conversations are stored in memory by default, but you can optionally install a storage adapter to persist conversations to a database.
- The
keyv-file
adapter is also included in this package, and can be used to store conversations in a JSON file if you're using the API server or CLI (seesettings.example.js
).
- (
ChatGPTClient
) Supports configurable prompt prefixes, and custom names for the user and ChatGPT.- In essence, this allows you to turn ChatGPT into a different character.
- This is currently only configurable on a global level, but I plan to add support for per-conversation customization.
- Node.js >= 16.0.0
- npm
- Docker (optional, for API server)
- OpenAI API key
npm i @waylaidwanderer/chatgpt-api
BingAIClient
import { BingAIClient } from '@waylaidwanderer/chatgpt-api';
const bingAIClient = new BingAIClient({
userToken: '', // "_U" cookie from bing.com
debug: false,
});
let response = await bingAIClient.sendMessage('Write a short poem about cats', {
onProgress: (token) => {
process.stdout.write(token);
},
});
console.log(response);
response = await bingAIClient.sendMessage('Now write it in French', {
conversationSignature: response.conversationSignature,
conversationId: response.conversationId,
clientId: response.clientId,
invocationId: response.invocationId,
onProgress: (token) => {
process.stdout.write(token);
},
});
console.log(response);
ChatGPTClient
import { ChatGPTClient } from '@waylaidwanderer/chatgpt-api';
const clientOptions = {
// (Optional) Support for a reverse proxy for the completions endpoint (private API server).
// Warning: This will expose your `openaiApiKey` to a third-party. Consider the risks before using this.
// reverseProxyUrl: 'https://chatgpt.pawan.krd/api/completions',
// (Optional) Parameters as described in https://platform.openai.com/docs/api-reference/completions
modelOptions: {
// You can override the model name and any other parameters here.
// model: 'text-chat-davinci-002-20221122',
},
// (Optional) Set custom instructions instead of "You are ChatGPT...".
// promptPrefix: 'You are Bob, a cowboy in Western times...',
// (Optional) Set a custom name for the user
// userLabel: 'User',
// (Optional) Set a custom name for ChatGPT
// chatGptLabel: 'ChatGPT',
// (Optional) Set to true to enable `console.debug()` logging
debug: false,
};
const cacheOptions = {
// Options for the Keyv cache, see https://www.npmjs.com/package/keyv
// This is used for storing conversations, and supports additional drivers (conversations are stored in memory by default)
// For example, to use a JSON file (`npm i keyv-file`) as a database:
// store: new KeyvFile({ filename: 'cache.json' }),
};
const chatGptClient = new ChatGPTClient('OPENAI_API_KEY', clientOptions, cacheOptions);
const response = await chatGptClient.sendMessage('Hello!');
console.log(response); // { response: 'Hi! How can I help you today?', conversationId: '...', messageId: '...' }
const response2 = await chatGptClient.sendMessage('Write a poem about cats.', { conversationId: response.conversationId, parentMessageId: response.messageId });
console.log(response2.response); // Cats are the best pets in the world.
const response3 = await chatGptClient.sendMessage('Now write it in French.', {
conversationId: response2.conversationId,
parentMessageId: response2.messageId,
// If you want streamed responses, you can set the `onProgress` callback to receive the response as it's generated.
// You will receive one token at a time, so you will need to concatenate them yourself.
onProgress: (token) => console.log(token),
});
console.log(response3.response); // Les chats sont les meilleurs animaux de compagnie du monde.
You can install the package using
npm i -g @waylaidwanderer/chatgpt-api
then run it using
chatgpt-api
.
This takes an optional --settings=<path_to_settings.js>
parameter, or looks for settings.js
in the current directory if not set, with the following contents:
module.exports = {
// Your OpenAI API key (for `ChatGPTClient`)
openaiApiKey: process.env.OPENAI_API_KEY || '',
chatGptClient: {
// (Optional) Support for a reverse proxy for the completions endpoint (private API server).
// Warning: This will expose your `openaiApiKey` to a third-party. Consider the risks before using this.
// reverseProxyUrl: 'https://chatgpt.pawan.krd/api/completions',
// (Optional) Parameters as described in https://platform.openai.com/docs/api-reference/completions
modelOptions: {
// You can override the model name and any other parameters here.
// model: 'text-chat-davinci-002-20221122',
},
// (Optional) Set custom instructions instead of "You are ChatGPT...".
// promptPrefix: 'You are Bob, a cowboy in Western times...',
// (Optional) Set a custom name for the user
// userLabel: 'User',
// (Optional) Set a custom name for ChatGPT
// chatGptLabel: 'ChatGPT',
// (Optional) Set to true to enable `console.debug()` logging
debug: false,
},
// Options for the Keyv cache, see https://www.npmjs.com/package/keyv.
// This is used for storing conversations, and supports additional drivers (conversations are stored in memory by default).
// Does not apply when using `BingAIClient`.
cacheOptions: {},
// Options for the Bing client
bingAiClient: {
// The "_U" cookie value from bing.com
userToken: '',
// (Optional) Set to true to enable `console.debug()` logging
debug: false,
},
// Options for the API server
apiOptions: {
port: process.env.API_PORT || 3000,
host: process.env.API_HOST || 'localhost',
// (Optional) Set to true to enable `console.debug()` logging
debug: false,
// (Optional) Set to "bing" to use `BingAIClient` instead of `ChatGPTClient`.
// clientToUse: 'bing',
},
// Options for the CLI app
cliOptions: {
// (Optional) Set to "bing" to use `BingAIClient` instead of `ChatGPTClient`.
// clientToUse: 'bing',
},
// If set, `ChatGPTClient` will use `keyv-file` to store conversations to this JSON file instead of in memory.
// However, `cacheOptions.store` will override this if set
storageFilePath: process.env.STORAGE_FILE_PATH || './cache.json',
};
Alternatively, you can install and run the package directly.
- Clone this repository:
git clone https://github.com/waylaidwanderer/node-chatgpt-api
- Install dependencies with
npm install
(if not using Docker) - Rename
settings.example.js
tosettings.js
in the root directory and change the settings where required. - Start the server:
- using
npm start
ornpm run server
(if not using Docker) - using
docker-compose up
(requires Docker)
- using
To start a conversation with ChatGPT, send a POST request to the server's /conversation
endpoint with a JSON body in the following format.
Optional parameters are only necessary for conversations that span multiple requests:
{
"message": "Hello, how are you today?",
"conversationId": "your-conversation-id (optional)",
"parentMessageId": "your-parent-message-id (optional, for `ChatGPTClient` only)",
"conversationSignature": "your-conversation-signature (optional, for `BingAIClient` only)",
"clientId": "your-client-id (optional, for `BingAIClient` only)",
"invocationId": "your-invocation-id (optional, for `BingAIClient` only)",
}
The server will return a JSON object containing ChatGPT's response:
// HTTP/1.1 200 OK
{
"response": "I'm doing well, thank you! How are you?",
"conversationId": "your-conversation-id",
"messageId": "response-message-id (for `ChatGPTClient` only)",
"conversationSignature": "your-conversation-signature (for `BingAIClient` only)",
"clientId": "your-client-id (for `BingAIClient` only)",
"invocationId": "your-invocation-id (for `BingAIClient` only - pass this new value back into subsequent requests as-is)",
"details": "additional details about the AI's response (for `BingAIClient` only)"
}
If the request is unsuccessful, the server will return a JSON object with an error message.
If the request object is missing a required property (e.g. message
):
// HTTP/1.1 400 Bad Request
{
"error": "The message parameter is required."
}
If there was an error sending the message to ChatGPT:
// HTTP/1.1 503 Service Unavailable
{
"error": "There was an error communicating with ChatGPT."
}
You can set "stream": true
in the request body to receive a stream of tokens as they are generated.
{
"message": "Write a poem about cats.",
"conversationId": "your-conversation-id (optional)",
"parentMessageId": "your-parent-message-id (optional)",
"stream": true
}
See demos/use-api-server-streaming.js for an example of how to receive the response as it's generated. You will receive one token at a time, so you will need to concatenate them yourself.
Successful output:
{ data: '', event: '', id: '', retry: 3000 }
{ data: 'Hello', event: '', id: '', retry: undefined }
{ data: '!', event: '', id: '', retry: undefined }
{ data: ' How', event: '', id: '', retry: undefined }
{ data: ' can', event: '', id: '', retry: undefined }
{ data: ' I', event: '', id: '', retry: undefined }
{ data: ' help', event: '', id: '', retry: undefined }
{ data: ' you', event: '', id: '', retry: undefined }
{ data: ' today', event: '', id: '', retry: undefined }
{ data: '?', event: '', id: '', retry: undefined }
{ data: '[DONE]', event: '', id: '', retry: undefined }
// Hello! How can I help you today?
Error output:
const message = {
data: '{"code":503,"error":"There was an error communicating with ChatGPT."}',
event: 'error',
id: '',
retry: undefined
};
if (message.event === 'error') {
console.error(JSON.parse(message.data).error); // There was an error communicating with ChatGPT.
}
Follow the same setup instructions for the API server, creating settings.js
.
If installed globally:
chatgpt-cli
If installed locally:
npm run cli
ChatGPT's responses are automatically copied to your clipboard, so you can paste them into other applications.
As shown in the examples above, you can set reverseProxyUrl
in ChatGPTClient
's options to use a private API instead of the official ChatGPT API.
For now, this is the only way to use the ChatGPT raw models directly.
Depending on whose private API you use, there are some things you have to do differently to make it work with ChatGPTClient
, and some things may not work as expected. Instructions are provided below.
https://chatgpt.pawan.krd/api/completions (@PawanOsmon)
- Set
reverseProxyUrl
tohttps://chatgpt.pawan.krd/api/completions
insettings.js
orChatGPTClient
's options. - Set the OpenAI API key to your ChatGPT session's access token instead of your actual OpenAI API key.
- You can find your ChatGPT session's access token by logging in to ChatGPT and then going to https://chat.openai.com/api/auth/session (look for the
accessToken
property). - Fetching or refreshing your ChatGPT session's access token is not currently supported by this library.
- You can find your ChatGPT session's access token by logging in to ChatGPT and then going to https://chat.openai.com/api/auth/session (look for the
- Set the
model
totext-davinci-002-render
,text-davinci-002-render-paid
, or any other ChatGPT models that your account has access to. Models must be a ChatGPT model name, not the raw model name, and you cannot use a model that your account does not have access to.- You can check which ones you have access to by opening DevTools and going to the Network tab. Refresh the page and look at the response body for https://chat.openai.com/backend-api/models.
Since text-chat-davinci-002
is ChatGPT's raw model, I had to do my best to replicate the way the official ChatGPT website uses it. After extensive testing and comparing responses, I believe that the model used by ChatGPT has some additional fine-tuning.
This means my implementation or the raw model may not behave exactly the same in some ways:
-
Conversations are not tied to any user IDs, so if that's important to you, you should implement your own user ID system.
-
ChatGPT's model parameters (temperature, frequency penalty, etc.) are unknown, so I set some defaults that I thought would be reasonable.
-
Conversations are limited to roughly the last 3000 tokens, so earlier messages may be forgotten during longer conversations.
- This works in a similar way to ChatGPT, except I'm pretty sure they have some additional way of retrieving context from earlier messages when needed (which can probably be achieved with embeddings, but I consider that out-of-scope for now).
-
It is well known that, as part of the fine-tuning, ChatGPT had the following preamble:
"You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short. Knowledge cutoff: 2021-09 Current date: 2023-01-31"
As OpenAI updates ChatGPT, this preamble may also change. The default prompt prefix in my implementation attempts to replicate a similar behavior to the current ChatGPT model.
If you'd like to contribute to this project, please create a pull request with a detailed description of your changes.
This project is licensed under the MIT License.