diff --git a/docs/classes/ChatGPTAPI.md b/docs/classes/ChatGPTAPI.md index 78072d9..84fb046 100644 --- a/docs/classes/ChatGPTAPI.md +++ b/docs/classes/ChatGPTAPI.md @@ -10,7 +10,10 @@ ### Accessors +- [clearanceToken](ChatGPTAPI.md#clearancetoken) +- [sessionToken](ChatGPTAPI.md#sessiontoken) - [user](ChatGPTAPI.md#user) +- [userAgent](ChatGPTAPI.md#useragent) ### Methods @@ -28,26 +31,63 @@ Creates a new client wrapper around the unofficial ChatGPT REST API. +Note that your IP address and `userAgent` must match the same values that you used +to obtain your `clearanceToken`. + #### Parameters | Name | Type | Description | | :------ | :------ | :------ | | `opts` | `Object` | - | -| `opts.accessToken?` | `string` | - | -| `opts.accessTokenTTL?` | `number` | **`Default Value`** 60000 (60 seconds) | +| `opts.accessToken?` | `string` | **`Default Value`** `undefined` * | +| `opts.accessTokenTTL?` | `number` | **`Default Value`** 1 hour * | | `opts.apiBaseUrl?` | `string` | **`Default Value`** `'https://chat.openai.com/api'` * | | `opts.backendApiBaseUrl?` | `string` | **`Default Value`** `'https://chat.openai.com/backend-api'` * | -| `opts.clearanceToken` | `string` | - | +| `opts.clearanceToken` | `string` | = **Required** Cloudflare `cf_clearance` cookie value (see readme for instructions) | +| `opts.debug?` | `boolean` | **`Default Value`** `false` * | +| `opts.headers?` | `Record`<`string`, `string`\> | **`Default Value`** `undefined` * | | `opts.markdown?` | `boolean` | **`Default Value`** `true` * | | `opts.sessionToken` | `string` | = **Required** OpenAI session token which can be found in a valid session's cookies (see readme for instructions) | -| `opts.userAgent?` | `string` | **`Default Value`** `'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'` * | +| `opts.userAgent?` | `string` | **`Default Value`** `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'` * | #### Defined in -[src/chatgpt-api.ts:39](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-api.ts#L39) +[src/chatgpt-api.ts:45](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L45) ## Accessors +### clearanceToken + +• `get` **clearanceToken**(): `string` + +Gets the current Cloudflare clearance token (`cf_clearance` cookie value). + +#### Returns + +`string` + +#### Defined in + +[src/chatgpt-api.ts:136](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L136) + +___ + +### sessionToken + +• `get` **sessionToken**(): `string` + +Gets the current session token. + +#### Returns + +`string` + +#### Defined in + +[src/chatgpt-api.ts:131](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L131) + +___ + ### user • `get` **user**(): [`User`](../modules.md#user) @@ -60,7 +100,23 @@ Gets the currently signed-in user, if authenticated, `null` otherwise. #### Defined in -[src/chatgpt-api.ts:98](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-api.ts#L98) +[src/chatgpt-api.ts:126](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L126) + +___ + +### userAgent + +• `get` **userAgent**(): `string` + +Gets the current user agent. + +#### Returns + +`string` + +#### Defined in + +[src/chatgpt-api.ts:141](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L141) ## Methods @@ -77,7 +133,7 @@ is still valid. #### Defined in -[src/chatgpt-api.ts:250](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-api.ts#L250) +[src/chatgpt-api.ts:319](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L319) ___ @@ -104,7 +160,7 @@ The new conversation instance #### Defined in -[src/chatgpt-api.ts:344](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-api.ts#L344) +[src/chatgpt-api.ts:425](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L425) ___ @@ -121,7 +177,7 @@ the token fails. #### Defined in -[src/chatgpt-api.ts:237](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-api.ts#L237) +[src/chatgpt-api.ts:306](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L306) ___ @@ -147,7 +203,7 @@ A valid access token #### Defined in -[src/chatgpt-api.ts:264](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-api.ts#L264) +[src/chatgpt-api.ts:333](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L333) ___ @@ -178,4 +234,4 @@ The response from ChatGPT #### Defined in -[src/chatgpt-api.ts:121](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-api.ts#L121) +[src/chatgpt-api.ts:166](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-api.ts#L166) diff --git a/docs/classes/ChatGPTConversation.md b/docs/classes/ChatGPTConversation.md index 61b8fa7..f1fd62b 100644 --- a/docs/classes/ChatGPTConversation.md +++ b/docs/classes/ChatGPTConversation.md @@ -41,7 +41,7 @@ Creates a new conversation wrapper around the ChatGPT API. #### Defined in -[src/chatgpt-conversation.ts:21](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-conversation.ts#L21) +[src/chatgpt-conversation.ts:21](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-conversation.ts#L21) ## Properties @@ -51,7 +51,7 @@ Creates a new conversation wrapper around the ChatGPT API. #### Defined in -[src/chatgpt-conversation.ts:10](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-conversation.ts#L10) +[src/chatgpt-conversation.ts:10](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-conversation.ts#L10) ___ @@ -61,7 +61,7 @@ ___ #### Defined in -[src/chatgpt-conversation.ts:11](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-conversation.ts#L11) +[src/chatgpt-conversation.ts:11](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-conversation.ts#L11) ___ @@ -71,7 +71,7 @@ ___ #### Defined in -[src/chatgpt-conversation.ts:12](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-conversation.ts#L12) +[src/chatgpt-conversation.ts:12](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-conversation.ts#L12) ## Methods @@ -104,4 +104,4 @@ The response from ChatGPT #### Defined in -[src/chatgpt-conversation.ts:48](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/chatgpt-conversation.ts#L48) +[src/chatgpt-conversation.ts:48](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/chatgpt-conversation.ts#L48) diff --git a/docs/classes/ChatGPTError.md b/docs/classes/ChatGPTError.md index b80b8c8..776f060 100644 --- a/docs/classes/ChatGPTError.md +++ b/docs/classes/ChatGPTError.md @@ -66,7 +66,7 @@ node_modules/.pnpm/typescript@4.9.3/node_modules/typescript/lib/lib.es2022.error #### Defined in -[src/types.ts:295](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L295) +[src/types.ts:298](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L298) ___ @@ -76,7 +76,7 @@ ___ #### Defined in -[src/types.ts:294](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L294) +[src/types.ts:297](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L297) ___ @@ -86,7 +86,7 @@ ___ #### Defined in -[src/types.ts:292](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L292) +[src/types.ts:295](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L295) ___ @@ -96,4 +96,4 @@ ___ #### Defined in -[src/types.ts:293](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L293) +[src/types.ts:296](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L296) diff --git a/docs/modules.md b/docs/modules.md index 85ca365..0ea1073 100644 --- a/docs/modules.md +++ b/docs/modules.md @@ -17,6 +17,7 @@ - [ConversationJSONBody](modules.md#conversationjsonbody) - [ConversationResponseEvent](modules.md#conversationresponseevent) - [Message](modules.md#message) +- [MessageActionType](modules.md#messageactiontype) - [MessageContent](modules.md#messagecontent) - [MessageFeedbackJSONBody](modules.md#messagefeedbackjsonbody) - [MessageFeedbackRating](modules.md#messagefeedbackrating) @@ -27,6 +28,7 @@ - [ModelsResult](modules.md#modelsresult) - [ModerationsJSONBody](modules.md#moderationsjsonbody) - [ModerationsJSONResult](modules.md#moderationsjsonresult) +- [OpenAIAuth](modules.md#openaiauth) - [Prompt](modules.md#prompt) - [PromptContent](modules.md#promptcontent) - [Role](modules.md#role) @@ -37,6 +39,8 @@ ### Functions +- [getBrowser](modules.md#getbrowser) +- [getOpenAIAuth](modules.md#getopenaiauth) - [markdownToText](modules.md#markdowntotext) ## Type Aliases @@ -47,7 +51,7 @@ #### Defined in -[src/types.ts:109](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L109) +[src/types.ts:109](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L109) ___ @@ -57,7 +61,7 @@ ___ #### Defined in -[src/types.ts:1](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L1) +[src/types.ts:1](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L1) ___ @@ -79,7 +83,7 @@ https://chat.openapi.com/backend-api/conversation #### Defined in -[src/types.ts:134](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L134) +[src/types.ts:134](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L134) ___ @@ -97,7 +101,7 @@ ___ #### Defined in -[src/types.ts:251](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L251) +[src/types.ts:251](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L251) ___ @@ -122,7 +126,17 @@ ___ #### Defined in -[src/types.ts:257](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L257) +[src/types.ts:257](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L257) + +___ + +### MessageActionType + +Ƭ **MessageActionType**: ``"next"`` \| ``"variant"`` + +#### Defined in + +[src/types.ts:276](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L276) ___ @@ -139,7 +153,7 @@ ___ #### Defined in -[src/types.ts:270](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L270) +[src/types.ts:270](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L270) ___ @@ -161,7 +175,7 @@ https://chat.openapi.com/backend-api/conversation/message_feedback #### Defined in -[src/types.ts:193](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L193) +[src/types.ts:193](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L193) ___ @@ -171,7 +185,7 @@ ___ #### Defined in -[src/types.ts:249](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L249) +[src/types.ts:249](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L249) ___ @@ -191,7 +205,7 @@ ___ #### Defined in -[src/types.ts:222](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L222) +[src/types.ts:222](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L222) ___ @@ -201,7 +215,7 @@ ___ #### Defined in -[src/types.ts:220](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L220) +[src/types.ts:220](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L220) ___ @@ -211,7 +225,7 @@ ___ #### Defined in -[src/types.ts:275](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L275) +[src/types.ts:275](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L275) ___ @@ -229,7 +243,7 @@ ___ #### Defined in -[src/types.ts:77](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L77) +[src/types.ts:77](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L77) ___ @@ -247,7 +261,7 @@ https://chat.openapi.com/backend-api/models #### Defined in -[src/types.ts:70](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L70) +[src/types.ts:70](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L70) ___ @@ -266,7 +280,7 @@ https://chat.openapi.com/backend-api/moderations #### Defined in -[src/types.ts:97](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L97) +[src/types.ts:97](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L97) ___ @@ -286,7 +300,29 @@ https://chat.openapi.com/backend-api/moderations #### Defined in -[src/types.ts:114](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L114) +[src/types.ts:114](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L114) + +___ + +### OpenAIAuth + +Ƭ **OpenAIAuth**: `Object` + +Represents everything that's required to pass into `ChatGPTAPI` in order +to authenticate with the unofficial ChatGPT API. + +#### Type declaration + +| Name | Type | +| :------ | :------ | +| `clearanceToken` | `string` | +| `cookies?` | `Record`<`string`, `Protocol.Network.Cookie`\> | +| `sessionToken` | `string` | +| `userAgent` | `string` | + +#### Defined in + +[src/openai-auth.ts:17](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/openai-auth.ts#L17) ___ @@ -304,7 +340,7 @@ ___ #### Defined in -[src/types.ts:161](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L161) +[src/types.ts:161](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L161) ___ @@ -321,7 +357,7 @@ ___ #### Defined in -[src/types.ts:178](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L178) +[src/types.ts:178](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L178) ___ @@ -331,7 +367,7 @@ ___ #### Defined in -[src/types.ts:3](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L3) +[src/types.ts:3](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L3) ___ @@ -341,7 +377,7 @@ ___ #### Defined in -[src/types.ts:286](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L286) +[src/types.ts:289](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L289) ___ @@ -354,7 +390,9 @@ ___ | Name | Type | | :------ | :------ | | `abortSignal?` | `AbortSignal` | +| `action?` | [`MessageActionType`](modules.md#messageactiontype) | | `conversationId?` | `string` | +| `messageId?` | `string` | | `onConversationResponse?` | (`response`: [`ConversationResponseEvent`](modules.md#conversationresponseevent)) => `void` | | `onProgress?` | (`partialResponse`: `string`) => `void` | | `parentMessageId?` | `string` | @@ -362,7 +400,7 @@ ___ #### Defined in -[src/types.ts:277](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L277) +[src/types.ts:278](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L278) ___ @@ -383,7 +421,7 @@ https://chat.openapi.com/api/auth/session #### Defined in -[src/types.ts:8](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L8) +[src/types.ts:8](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L8) ___ @@ -405,10 +443,72 @@ ___ #### Defined in -[src/types.ts:30](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/types.ts#L30) +[src/types.ts:30](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/types.ts#L30) ## Functions +### getBrowser + +▸ **getBrowser**(`launchOptions?`): `Promise`<`Browser`\> + +Launches a non-puppeteer instance of Chrome. Note that in my testing, I wasn't +able to use the built-in `puppeteer` version of Chromium because Cloudflare +recognizes it and blocks access. + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `launchOptions?` | `PuppeteerLaunchOptions` | + +#### Returns + +`Promise`<`Browser`\> + +#### Defined in + +[src/openai-auth.ts:127](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/openai-auth.ts#L127) + +___ + +### getOpenAIAuth + +▸ **getOpenAIAuth**(`__namedParameters`): `Promise`<[`OpenAIAuth`](modules.md#openaiauth)\> + +Bypasses OpenAI's use of Cloudflare to get the cookies required to use +ChatGPT. Uses Puppeteer with a stealth plugin under the hood. + +If you pass `email` and `password`, then it will log into the account and +include a `sessionToken` in the response. + +If you don't pass `email` and `password`, then it will just return a valid +`clearanceToken`. + +This can be useful because `clearanceToken` expires after ~2 hours, whereas +`sessionToken` generally lasts much longer. We recommend renewing your +`clearanceToken` every hour or so and creating a new instance of `ChatGPTAPI` +with your updated credentials. + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `__namedParameters` | `Object` | +| `__namedParameters.browser?` | `Browser` | +| `__namedParameters.email?` | `string` | +| `__namedParameters.password?` | `string` | +| `__namedParameters.timeoutMs?` | `number` | + +#### Returns + +`Promise`<[`OpenAIAuth`](modules.md#openaiauth)\> + +#### Defined in + +[src/openai-auth.ts:39](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/openai-auth.ts#L39) + +___ + ### markdownToText ▸ **markdownToText**(`markdown?`): `string` @@ -425,4 +525,4 @@ ___ #### Defined in -[src/utils.ts:4](https://github.com/transitive-bullshit/chatgpt-api/blob/8e1cde4/src/utils.ts#L4) +[src/utils.ts:4](https://github.com/transitive-bullshit/chatgpt-api/blob/1d621d0/src/utils.ts#L4) diff --git a/docs/readme.md b/docs/readme.md index 3ef7fc6..7ed3284 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -1,7 +1,19 @@ chatgpt / [Exports](modules.md) -> **Note** -> As of December 11, 2022 ~2pm CST, OpenAI has enabled additional Cloudflare restrictions that are currently preventing us from refreshing access tokens. This is affecting _all_ ChatGPT API wrappers at the moment, including the Python ones. See [this issue](https://github.com/transitive-bullshit/chatgpt-api/issues/96) for an ongoing discussion until I release a viable workaround. +# Update December 12, 2022 + +Yesterday, OpenAI added additional Cloudflare protections that make it more difficult to access the unofficial API. + +The demos have been updated to use Puppeteer to log in to ChatGPT and extract the Cloudflare `cf_clearance` cookie and OpenAI session token. 🔥 + +To use the updated version, make sure you're using the latest version of this package and Node.js >= 18. Then update your code to use the examples below, paying special attention to the sections on [Authentication](#authentication) and [Restrictions](#restrictions). + +We're working hard in [this issue](https://github.com/transitive-bullshit/chatgpt-api/issues/96) to improve this process. Keep in mind that this package will be updated to use the official API as soon as it's released. 💪 + +Cheers, +Travis + +---

Example usage @@ -13,16 +25,18 @@ chatgpt / [Exports](modules.md) [![NPM](https://img.shields.io/npm/v/chatgpt.svg)](https://www.npmjs.com/package/chatgpt) [![Build Status](https://github.com/transitive-bullshit/chatgpt-api/actions/workflows/test.yml/badge.svg)](https://github.com/transitive-bullshit/chatgpt-api/actions/workflows/test.yml) [![MIT License](https://img.shields.io/badge/license-MIT-blue)](https://github.com/transitive-bullshit/chatgpt-api/blob/main/license) [![Prettier Code Formatting](https://img.shields.io/badge/code_style-prettier-brightgreen.svg)](https://prettier.io) -- [Intro](#intro) -- [Install](#install) -- [Usage](#usage) - - [Docs](#docs) - - [Demos](#demos) - - [Session Tokens](#session-tokens) -- [Projects](#projects) -- [Compatibility](#compatibility) -- [Credits](#credits) -- [License](#license) +- [Update December 12, 2022](#update-december-12-2022) + - [Intro](#intro) + - [Install](#install) + - [Usage](#usage) + - [Docs](#docs) + - [Demos](#demos) + - [Authentication](#authentication) + - [Restrictions](#restrictions) + - [Projects](#projects) + - [Compatibility](#compatibility) + - [Credits](#credits) + - [License](#license) ## Intro @@ -39,15 +53,17 @@ npm install chatgpt ## Usage ```ts -import { ChatGPTAPI } from 'chatgpt' +import { ChatGPTAPI, getOpenAIAuth } from 'chatgpt' async function example() { - // sessionToken is required; see below for details - const api = new ChatGPTAPI({ - sessionToken: process.env.SESSION_TOKEN + // uses puppeteer to bypass cloudflare (headful because you may have to solve + // a captcha) + const openAIAuth = await getOpenAIAuth({ + email: process.env.EMAIL, + password: process.env.EMAIL }) - // ensure the API is properly authenticated + const api = new ChatGPTAPI({ ...openAIAuth }) await api.ensureAuth() // send a message and wait for the response @@ -63,28 +79,23 @@ async function example() { ChatGPT responses are formatted as markdown by default. If you want to work with plaintext instead, you can use: ```ts -const api = new ChatGPTAPI({ - sessionToken: process.env.SESSION_TOKEN, - markdown: false -}) +const api = new ChatGPTAPI({ ...openAIAuth, markdown: false }) ``` If you want to automatically track the conversation, you can use `ChatGPTAPI.getConversation()`: ```ts -const api = new ChatGPTAPI({ - sessionToken: process.env.SESSION_TOKEN -}) +const api = new ChatGPTAPI({ ...openAIAuth, markdown: false }) const conversation = api.getConversation() // send a message and wait for the response const response0 = await conversation.sendMessage('What is OpenAI?') -// send a follow-up prompt to the previous message and wait for the response +// send a follow-up const response1 = await conversation.sendMessage('Can you expand on that?') -// send another follow-up to the same conversation +// send another follow-up const response2 = await conversation.sendMessage('Oh cool; thank you') ``` @@ -107,11 +118,14 @@ You can stream responses using the `onProgress` or `onConversationResponse` call ```js async function example() { // To use ESM in CommonJS, you can use a dynamic import - const { ChatGPTAPI } = await import('chatgpt') + const { ChatGPTAPI, getOpenAIAuth } = await import('chatgpt') - const api = new ChatGPTAPI({ - sessionToken: process.env.SESSION_TOKEN + const openAIAuth = await getOpenAIAuth({ + email: process.env.EMAIL, + password: process.env.EMAIL }) + + const api = new ChatGPTAPI({ ...openAIAuth }) await api.ensureAuth() const response = await api.sendMessage('Hello World!') @@ -127,39 +141,50 @@ See the [auto-generated docs](./docs/classes/ChatGPTAPI.md) for more info on met ### Demos -A [basic demo](./src/demo.ts) is included for testing purposes: +To run the included demos: + +1. clone repo +2. install node deps +3. set `EMAIL` and `PASSWORD` in .env + +A [basic demo](./demos/demo.ts) is included for testing purposes: ```bash -# 1. clone repo -# 2. install node deps -# 3. set `SESSION_TOKEN` in .env -# 4. run: npx tsx src/demo.ts ``` -A [conversation demo](./src/demo-conversation.ts) is also included: +A [conversation demo](./demos/demo-conversation.ts) is also included: ```bash -# 1. clone repo -# 2. install node deps -# 3. set `SESSION_TOKEN` in .env -# 4. run: npx tsx src/demo-conversation.ts ``` -### Session Tokens +### Authentication -**This package requires a valid session token from ChatGPT to access it's unofficial REST API.** +#### Restrictions -To get a session token: +**Please read carefully** + +- You must use `node >= 18`. I'm using `v19.2.0` in my testing, but for some reason, all `fetch` requests using Node.js `v16` and `v17` fail at the moment (these use `undici` under the hood, whereas Node.js v18 and above use a built-in `fetch` based on `undici`). +- Cloudflare `cf_clearance` **tokens expire after 2 hours**, so right now we recommend that you refresh your `cf_clearance` token every hour or so. +- Your `user-agent` and `IP address` **must match** from the real browser window you're logged in with to the one you're using for `ChatGPTAPI`. + - This means that you currently can't log in with your laptop and then run the bot on a server or proxy somewhere. +- Cloudflare will still sometimes ask you to complete a CAPTCHA, so you may need to keep an eye on it and manually resolve the CAPTCHA. Automated CAPTCHA bypass is coming soon. +- You should not be using this account while the bot is using it, because that browser window may refresh one of your tokens and invalidate the bot's session. + +

+Getting tokens manually + +To get a session token manually: 1. Go to https://chat.openai.com/chat and log in or sign up. 2. Open dev tools. 3. Open `Application` > `Cookies`. ![ChatGPT cookies](./media/session-token.png) -4. Copy the value for `__Secure-next-auth.session-token` and save it to your environment. +4. Copy the value for `__Secure-next-auth.session-token` and save it to your environment. This will be your `sessionToken`. +5. Copy the value for `cf_clearance` and save it to your environment. This will be your `clearanceToken`. -If you want to run the built-in demo, store this value as `SESSION_TOKEN` in a local `.env` file. +
> **Note** > This package will switch to using the official API once it's released. @@ -197,6 +222,7 @@ All of these awesome projects are built using the `chatgpt` package. 🤯 - [QQ Bot (plugin for KiviBot)](https://github.com/KiviBotLab/kivibot-plugin-chatgpt) - [QQ Bot (oicq)](https://github.com/easydu2002/chat_gpt_oicq) - [QQ Bot (oicq + RabbitMQ)](https://github.com/linsyking/ChatGPT-QQBot) +- [QQ Bot (go-cqhttp)](https://github.com/PairZhu/ChatGPT-QQRobot) - [Lovelines.xyz](https://lovelines.xyz) - [EXM smart contracts](https://github.com/decentldotland/molecule) - [Flutter ChatGPT API](https://github.com/coskuncay/flutter_chatgpt_api) @@ -204,10 +230,12 @@ All of these awesome projects are built using the `chatgpt` package. 🤯 - [Github Action for reviewing PRs](https://github.com/kxxt/chatgpt-action/) - [WhatsApp Bot #1](https://github.com/pascalroget/whatsgpt) (multi-user support) - [WhatsApp Bot #2](https://github.com/amosayomide05/chatgpt-whatsapp-bot) +- [WhatsApp Bot #3](https://github.com/navopw/whatsapp-chatgpt) - [Matrix Bot](https://github.com/jakecoppinger/matrix-chatgpt-bot) - [Rental Cover Letter Generator](https://sharehouse.app/ai) - [Assistant CLI](https://github.com/diciaup/assistant-cli) - [Teams Bot](https://github.com/formulahendry/chatgpt-teams-bot) +- [Askai](https://github.com/yudax42/askai) If you create a cool integration, feel free to open a PR and add it to the list. @@ -215,11 +243,8 @@ If you create a cool integration, feel free to open a PR and add it to the list. This package is ESM-only. It supports: -- Node.js >= 16.8 - - If you need Node.js 14 support, use [`v1.4.0`](https://github.com/transitive-bullshit/chatgpt-api/releases/tag/v1.4.0) -- Edge runtimes like CF workers and Vercel edge functions -- Modern browsers - - Mainly meant for chrome extensions where your code is protected to a degree +- Node.js >= 18 + - Node.js 17, 16, and 14 were supported in earlier versions, but OpenAI's Cloudflare update caused a bug with `undici` on v17 and v16 that we need to debug. So for now, use `node >= 18` - We recommend against using `chatgpt` from client-side browser code because it would expose your private session token - If you want to build a website using `chatgpt`, we recommend using it only from your backend API