From 31932298fd1a6c559c8ded8d2d39786bc14a8df8 Mon Sep 17 00:00:00 2001 From: Travis Fischer Date: Sun, 19 Feb 2023 02:51:53 -0600 Subject: [PATCH] chore: update ts docs --- docs/classes/ChatGPTAPI.md | 8 +- docs/classes/ChatGPTError.md | 37 +++- docs/classes/ChatGPTUnofficialProxyAPI.md | 104 +++++++++ docs/interfaces/ChatMessage.md | 12 +- docs/modules.md | 253 +++++++++++++++++++++- docs/modules/openai.md | 19 +- docs/readme.md | 123 +++++++++-- 7 files changed, 520 insertions(+), 36 deletions(-) create mode 100644 docs/classes/ChatGPTUnofficialProxyAPI.md diff --git a/docs/classes/ChatGPTAPI.md b/docs/classes/ChatGPTAPI.md index 6fc3d79..f6de9de 100644 --- a/docs/classes/ChatGPTAPI.md +++ b/docs/classes/ChatGPTAPI.md @@ -46,7 +46,7 @@ unofficial ChatGPT model. #### Defined in -[src/chatgpt-api.ts:53](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/chatgpt-api.ts#L53) +[src/chatgpt-api.ts:53](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/chatgpt-api.ts#L53) ## Accessors @@ -60,7 +60,7 @@ unofficial ChatGPT model. #### Defined in -[src/chatgpt-api.ts:353](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/chatgpt-api.ts#L353) +[src/chatgpt-api.ts:353](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/chatgpt-api.ts#L353) • `set` **apiKey**(`apiKey`): `void` @@ -76,7 +76,7 @@ unofficial ChatGPT model. #### Defined in -[src/chatgpt-api.ts:357](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/chatgpt-api.ts#L357) +[src/chatgpt-api.ts:357](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/chatgpt-api.ts#L357) ## Methods @@ -111,4 +111,4 @@ The response from ChatGPT #### Defined in -[src/chatgpt-api.ts:185](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/chatgpt-api.ts#L185) +[src/chatgpt-api.ts:185](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/chatgpt-api.ts#L185) diff --git a/docs/classes/ChatGPTError.md b/docs/classes/ChatGPTError.md index bb37357..61b2857 100644 --- a/docs/classes/ChatGPTError.md +++ b/docs/classes/ChatGPTError.md @@ -16,8 +16,11 @@ ### Properties +- [accountId](ChatGPTError.md#accountid) +- [isFinal](ChatGPTError.md#isfinal) - [statusCode](ChatGPTError.md#statuscode) - [statusText](ChatGPTError.md#statustext) +- [type](ChatGPTError.md#type) ## Constructors @@ -58,13 +61,33 @@ node_modules/.pnpm/typescript@4.9.5/node_modules/typescript/lib/lib.es2022.error ## Properties +### accountId + +• `Optional` **accountId**: `string` + +#### Defined in + +[src/types.ts:51](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L51) + +___ + +### isFinal + +• `Optional` **isFinal**: `boolean` + +#### Defined in + +[src/types.ts:50](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L50) + +___ + ### statusCode • `Optional` **statusCode**: `number` #### Defined in -[src/types.ts:27](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L27) +[src/types.ts:48](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L48) ___ @@ -74,4 +97,14 @@ ___ #### Defined in -[src/types.ts:28](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L28) +[src/types.ts:49](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L49) + +___ + +### type + +• `Optional` **type**: [`ChatGPTErrorType`](../modules.md#chatgpterrortype) + +#### Defined in + +[src/types.ts:52](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L52) diff --git a/docs/classes/ChatGPTUnofficialProxyAPI.md b/docs/classes/ChatGPTUnofficialProxyAPI.md new file mode 100644 index 0000000..bcf26f0 --- /dev/null +++ b/docs/classes/ChatGPTUnofficialProxyAPI.md @@ -0,0 +1,104 @@ +[chatgpt](../readme.md) / [Exports](../modules.md) / ChatGPTUnofficialProxyAPI + +# Class: ChatGPTUnofficialProxyAPI + +## Table of contents + +### Constructors + +- [constructor](ChatGPTUnofficialProxyAPI.md#constructor) + +### Accessors + +- [accessToken](ChatGPTUnofficialProxyAPI.md#accesstoken) + +### Methods + +- [sendMessage](ChatGPTUnofficialProxyAPI.md#sendmessage) + +## Constructors + +### constructor + +• **new ChatGPTUnofficialProxyAPI**(`opts`) + +#### Parameters + +| Name | Type | Description | +| :------ | :------ | :------ | +| `opts` | `Object` | - | +| `opts.accessToken` | `string` | - | +| `opts.apiReverseProxyUrl?` | `string` | **`Default Value`** `https://chat.openai.com/backend-api/conversation` * | +| `opts.debug?` | `boolean` | **`Default Value`** `false` * | +| `opts.fetch?` | (`input`: `RequestInfo` \| `URL`, `init?`: `RequestInit`) => `Promise`<`Response`\> | - | +| `opts.headers?` | `Record`<`string`, `string`\> | **`Default Value`** `undefined` * | +| `opts.model?` | `string` | **`Default Value`** `text-davinci-002-render-sha` * | + +#### Defined in + +[src/chatgpt-unofficial-proxy-api.ts:19](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/chatgpt-unofficial-proxy-api.ts#L19) + +## Accessors + +### accessToken + +• `get` **accessToken**(): `string` + +#### Returns + +`string` + +#### Defined in + +[src/chatgpt-unofficial-proxy-api.ts:65](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/chatgpt-unofficial-proxy-api.ts#L65) + +• `set` **accessToken**(`value`): `void` + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `value` | `string` | + +#### Returns + +`void` + +#### Defined in + +[src/chatgpt-unofficial-proxy-api.ts:69](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/chatgpt-unofficial-proxy-api.ts#L69) + +## Methods + +### sendMessage + +▸ **sendMessage**(`text`, `opts?`): `Promise`<[`ChatMessage`](../interfaces/ChatMessage.md)\> + +Sends a message to ChatGPT, waits for the response to resolve, and returns +the response. + +If you want your response to have historical context, you must provide a valid `parentMessageId`. + +If you want to receive a stream of partial responses, use `opts.onProgress`. +If you want to receive the full response, including message and conversation IDs, +you can use `opts.onConversationResponse` or use the `ChatGPTAPI.getConversation` +helper. + +Set `debug: true` in the `ChatGPTAPI` constructor to log more info on the full prompt sent to the OpenAI completions API. You can override the `promptPrefix` and `promptSuffix` in `opts` to customize the prompt. + +#### Parameters + +| Name | Type | +| :------ | :------ | +| `text` | `string` | +| `opts` | [`SendMessageBrowserOptions`](../modules.md#sendmessagebrowseroptions) | + +#### Returns + +`Promise`<[`ChatMessage`](../interfaces/ChatMessage.md)\> + +The response from ChatGPT + +#### Defined in + +[src/chatgpt-unofficial-proxy-api.ts:96](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/chatgpt-unofficial-proxy-api.ts#L96) diff --git a/docs/interfaces/ChatMessage.md b/docs/interfaces/ChatMessage.md index 4a92f22..a044dc4 100644 --- a/docs/interfaces/ChatMessage.md +++ b/docs/interfaces/ChatMessage.md @@ -21,7 +21,7 @@ #### Defined in -[src/types.ts:22](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L22) +[src/types.ts:34](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L34) ___ @@ -31,7 +31,7 @@ ___ #### Defined in -[src/types.ts:23](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L23) +[src/types.ts:35](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L35) ___ @@ -41,7 +41,7 @@ ___ #### Defined in -[src/types.ts:18](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L18) +[src/types.ts:30](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L30) ___ @@ -51,7 +51,7 @@ ___ #### Defined in -[src/types.ts:21](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L21) +[src/types.ts:33](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L33) ___ @@ -61,7 +61,7 @@ ___ #### Defined in -[src/types.ts:20](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L20) +[src/types.ts:32](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L32) ___ @@ -71,4 +71,4 @@ ___ #### Defined in -[src/types.ts:19](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L19) +[src/types.ts:31](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L31) diff --git a/docs/modules.md b/docs/modules.md index ab98688..70e15df 100644 --- a/docs/modules.md +++ b/docs/modules.md @@ -12,6 +12,7 @@ - [ChatGPTAPI](classes/ChatGPTAPI.md) - [ChatGPTError](classes/ChatGPTError.md) +- [ChatGPTUnofficialProxyAPI](classes/ChatGPTUnofficialProxyAPI.md) ### Interfaces @@ -19,21 +20,144 @@ ### Type Aliases +- [ChatGPTErrorType](modules.md#chatgpterrortype) +- [ContentType](modules.md#contenttype) +- [ConversationJSONBody](modules.md#conversationjsonbody) +- [ConversationResponseEvent](modules.md#conversationresponseevent) - [FetchFn](modules.md#fetchfn) +- [GetAccessTokenFn](modules.md#getaccesstokenfn) - [GetMessageByIdFunction](modules.md#getmessagebyidfunction) +- [Message](modules.md#message) +- [MessageActionType](modules.md#messageactiontype) +- [MessageContent](modules.md#messagecontent) +- [MessageMetadata](modules.md#messagemetadata) +- [Prompt](modules.md#prompt) +- [PromptContent](modules.md#promptcontent) - [Role](modules.md#role) +- [SendMessageBrowserOptions](modules.md#sendmessagebrowseroptions) - [SendMessageOptions](modules.md#sendmessageoptions) - [UpsertMessageFunction](modules.md#upsertmessagefunction) ## Type Aliases +### ChatGPTErrorType + +Ƭ **ChatGPTErrorType**: ``"unknown"`` \| ``"chatgpt:pool:account-on-cooldown"`` \| ``"chatgpt:pool:account-not-found"`` \| ``"chatgpt:pool:no-accounts"`` \| ``"chatgpt:pool:timeout"`` \| ``"chatgpt:pool:rate-limit"`` \| ``"chatgpt:pool:unavailable"`` + +#### Defined in + +[src/types.ts:38](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L38) + +___ + +### ContentType + +Ƭ **ContentType**: ``"text"`` + +#### Defined in + +[src/types.ts:223](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L223) + +___ + +### ConversationJSONBody + +Ƭ **ConversationJSONBody**: `Object` + +https://chat.openapi.com/backend-api/conversation + +#### Type declaration + +| Name | Type | Description | +| :------ | :------ | :------ | +| `action` | `string` | The action to take | +| `conversation_id?` | `string` | The ID of the conversation | +| `messages` | [`Prompt`](modules.md#prompt)[] | Prompts to provide | +| `model` | `string` | The model to use | +| `parent_message_id` | `string` | The parent message ID | + +#### Defined in + +[src/types.ts:179](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L179) + +___ + +### ConversationResponseEvent + +Ƭ **ConversationResponseEvent**: `Object` + +#### Type declaration + +| Name | Type | +| :------ | :------ | +| `conversation_id?` | `string` | +| `error?` | `string` \| ``null`` | +| `message?` | [`Message`](modules.md#message) | + +#### Defined in + +[src/types.ts:237](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L237) + +___ + ### FetchFn Ƭ **FetchFn**: typeof `fetch` #### Defined in -[src/types.ts:3](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L3) +[src/types.ts:3](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L3) + +___ + +### GetAccessTokenFn + +Ƭ **GetAccessTokenFn**: (`{ + email, + password, + sessionToken +}`: { `email`: `string` ; `password`: `string` ; `sessionToken?`: `string` }) => `string` \| `Promise`<`string`\> + +#### Type declaration + +▸ (`{ + email, + password, + sessionToken +}`): `string` \| `Promise`<`string`\> + +##### Parameters + +| Name | Type | +| :------ | :------ | +| `{ + email, + password, + sessionToken +}` | `Object` | +| `{ + email, + password, + sessionToken +}.email` | `string` | +| `{ + email, + password, + sessionToken +}.password` | `string` | +| `{ + email, + password, + sessionToken +}.sessionToken?` | `string` | + +##### Returns + +`string` \| `Promise`<`string`\> + +#### Defined in + +[src/types.ts:263](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L263) ___ @@ -59,7 +183,104 @@ Returns a chat message from a store by it's ID (or null if not found). #### Defined in -[src/types.ts:32](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L32) +[src/types.ts:56](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L56) + +___ + +### Message + +Ƭ **Message**: `Object` + +#### Type declaration + +| Name | Type | +| :------ | :------ | +| `content` | [`MessageContent`](modules.md#messagecontent) | +| `create_time` | `string` \| ``null`` | +| `end_turn` | ``null`` | +| `id` | `string` | +| `metadata` | [`MessageMetadata`](modules.md#messagemetadata) | +| `recipient` | `string` | +| `role` | [`Role`](modules.md#role) | +| `update_time` | `string` \| ``null`` | +| `user` | `string` \| ``null`` | +| `weight` | `number` | + +#### Defined in + +[src/types.ts:243](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L243) + +___ + +### MessageActionType + +Ƭ **MessageActionType**: ``"next"`` \| ``"variant"`` + +#### Defined in + +[src/types.ts:17](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L17) + +___ + +### MessageContent + +Ƭ **MessageContent**: `Object` + +#### Type declaration + +| Name | Type | +| :------ | :------ | +| `content_type` | `string` | +| `parts` | `string`[] | + +#### Defined in + +[src/types.ts:256](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L256) + +___ + +### MessageMetadata + +Ƭ **MessageMetadata**: `any` + +#### Defined in + +[src/types.ts:261](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L261) + +___ + +### Prompt + +Ƭ **Prompt**: `Object` + +#### Type declaration + +| Name | Type | Description | +| :------ | :------ | :------ | +| `content` | [`PromptContent`](modules.md#promptcontent) | The content of the prompt | +| `id` | `string` | The ID of the prompt | +| `role` | [`Role`](modules.md#role) | The role played in the prompt | + +#### Defined in + +[src/types.ts:206](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L206) + +___ + +### PromptContent + +Ƭ **PromptContent**: `Object` + +#### Type declaration + +| Name | Type | Description | +| :------ | :------ | :------ | +| `content_type` | [`ContentType`](modules.md#contenttype) | The content type of the prompt | +| `parts` | `string`[] | The parts to the prompt | + +#### Defined in + +[src/types.ts:225](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L225) ___ @@ -69,7 +290,29 @@ ___ #### Defined in -[src/types.ts:1](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L1) +[src/types.ts:1](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L1) + +___ + +### SendMessageBrowserOptions + +Ƭ **SendMessageBrowserOptions**: `Object` + +#### Type declaration + +| Name | Type | +| :------ | :------ | +| `abortSignal?` | `AbortSignal` | +| `action?` | [`MessageActionType`](modules.md#messageactiontype) | +| `conversationId?` | `string` | +| `messageId?` | `string` | +| `onProgress?` | (`partialResponse`: [`ChatMessage`](interfaces/ChatMessage.md)) => `void` | +| `parentMessageId?` | `string` | +| `timeoutMs?` | `number` | + +#### Defined in + +[src/types.ts:19](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L19) ___ @@ -93,7 +336,7 @@ ___ #### Defined in -[src/types.ts:5](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L5) +[src/types.ts:5](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L5) ___ @@ -119,4 +362,4 @@ Upserts a chat message to a store. #### Defined in -[src/types.ts:35](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L35) +[src/types.ts:59](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L59) diff --git a/docs/modules/openai.md b/docs/modules/openai.md index 4ace9ae..dad2834 100644 --- a/docs/modules/openai.md +++ b/docs/modules/openai.md @@ -10,6 +10,7 @@ - [CompletionResponse](openai.md#completionresponse) - [CompletionResponseChoices](openai.md#completionresponsechoices) - [CompletionResponseUsage](openai.md#completionresponseusage) +- [ReverseProxyCompletionParams](openai.md#reverseproxycompletionparams) ## Type Aliases @@ -38,7 +39,7 @@ #### Defined in -[src/types.ts:38](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L38) +[src/types.ts:62](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L62) ___ @@ -59,7 +60,7 @@ ___ #### Defined in -[src/types.ts:120](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L120) +[src/types.ts:148](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L148) ___ @@ -69,7 +70,7 @@ ___ #### Defined in -[src/types.ts:129](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L129) +[src/types.ts:157](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L157) ___ @@ -87,4 +88,14 @@ ___ #### Defined in -[src/types.ts:141](https://github.com/transitive-bullshit/chatgpt-api/blob/4379d52/src/types.ts#L141) +[src/types.ts:169](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L169) + +___ + +### ReverseProxyCompletionParams + +Ƭ **ReverseProxyCompletionParams**: [`CompletionParams`](openai.md#completionparams) & { `paid?`: `boolean` } + +#### Defined in + +[src/types.ts:144](https://github.com/transitive-bullshit/chatgpt-api/blob/6cf60ee/src/types.ts#L144) diff --git a/docs/readme.md b/docs/readme.md index 939f494..3b578eb 100644 --- a/docs/readme.md +++ b/docs/readme.md @@ -1,6 +1,46 @@ chatgpt / [Exports](modules.md) -# Update February 1, 2023 +

+ Example usage +

+ +## Updates + +
+Feb 19, 2023 + +We now provide three ways of accessing the unofficial ChatGPT API, all of which have tradeoffs: + +| Method | Free? | Robust? | Quality? | +| --------------------------- | ------ | -------- | ----------------- | +| `ChatGPTAPI` | ❌ No | ✅ Yes | ☑️ Mimics ChatGPT | +| `ChatGPTUnofficialProxyAPI` | ✅ Yes | ☑️ Maybe | ✅ Real ChatGPT | +| `ChatGPAPIBrowser` (v3) | ✅ Yes | ❌ No | ✅ Real ChatGPT | + +**Note**: I recommend that you use either `ChatGPTAPI` or `ChatGPTUnofficialProxyAPI`. + +1. `ChatGPTAPI` - Uses `text-davinci-003` to mimic ChatGPT via the official OpenAI completions API (most robust approach, but it's not free and doesn't use a model fine-tuned for chat) +2. `ChatGPTUnofficialProxyAPI` - Uses an unofficial proxy server to access ChatGPT's backend API in a way that circumvents Cloudflare (uses the real ChatGPT and is pretty lightweight, but relies on a third-party server and is rate-limited) +3. `ChatGPTAPIBrowser` - (v3.5.1 of this package) Uses Puppeteer to access the official ChatGPT webapp (uses the real ChatGPT, but very flaky, heavyweight, and error prone) + +
+ +
+Previous Updates + +
+ +
+Feb 5, 2023 + +OpenAI has disabled the leaked chat model we were previously using, so we're now defaulting to `text-davinci-003`, which is not free. + +We've found several other hidden, fine-tuned chat models, but OpenAI keeps disabling them, so we're searching for alternative workarounds. + +
+ +
+Feb 1, 2023 This package no longer requires any browser hacks – **it is now using the official OpenAI completions API** with a leaked model that ChatGPT uses under the hood. 🔥 @@ -19,6 +59,9 @@ Please upgrade to `chatgpt@latest` (at least [v4.0.0](https://github.com/transit Huge shoutout to [@waylaidwanderer](https://github.com/waylaidwanderer) for discovering the leaked chat model! +
+
+ If you run into any issues, we do have a pretty active [Discord](https://discord.gg/v9gERj825w) with a bunch of ChatGPT hackers from the Node.js & Python communities. Lastly, please consider starring this repo and following me on twitter twitter to help support the project. @@ -26,12 +69,6 @@ Lastly, please consider starring this repo and - Example usage -

- # ChatGPT API > Node.js client for the unofficial [ChatGPT](https://openai.com/blog/chatgpt/) API. @@ -40,9 +77,12 @@ Thanks && cheers, - [Intro](#intro) - [Install](#install) -- [Usage](#usage) - - [Docs](#docs) - - [Demos](#demos) +- [Usage (ChatGPTAPI)](#usage-chatgptapi) +- [Usage (ChatGPTUnofficialProxyAPI)](#usage-chatgptunofficialproxyapi) + - [Reverse Proxies](#reverse-proxies) + - [Access Tokens](#access-tokens) +- [Docs](#docs) +- [Demos](#demos) - [Projects](#projects) - [Compatibility](#compatibility) - [Credits](#credits) @@ -62,7 +102,7 @@ npm install chatgpt Make sure you're using `node >= 18` so `fetch` is available (or `node >= 14` if you install a [fetch polyfill](https://github.com/developit/unfetch#usage-as-a-polyfill)). -## Usage +## Usage (ChatGPTAPI) Sign up for an [OpenAI API key](https://platform.openai.com/overview) and store it in your environment. @@ -164,11 +204,64 @@ async function example() { -### Docs +## Usage (ChatGPTUnofficialProxyAPI) + +The API is almost exactly the same for the `ChatGPTUnofficialProxyAPI`; you just need to provide a ChatGPT `accessToken` instead of an OpenAI API key. + +```ts +import { ChatGPTUnofficialProxyAPI } from 'chatgpt' + +async function example() { + const api = new ChatGPTUnofficialProxyAPI({ + accessToken: process.env.OPENAI_ACCESS_TOKEN + }) + + const res = await api.sendMessage('Hello World!') + console.log(res.text) +} +``` + +See [demos/demo-reverse-proxy](./demos/demo-reverse-proxy.ts) for a full example: + +```bash +npx tsx demos/demo-reverse-proxy.ts +``` + +### Reverse Proxies + +You can override the reverse proxy by passing `apiReverseProxyUrl` to `ChatGPTUnofficialProxyAPI`: + +```ts +const api = new ChatGPTUnofficialProxyAPI({ + accessToken: process.env.OPENAI_ACCESS_TOKEN, + apiReverseProxyUrl: 'https://your-example-server.com/api/conversation' +}) +``` + +Known reverse proxies run by community members include: + +| Reverse Proxy URL | Author | Rate Limits | Last Checked | +| ------------------------------------------------ | -------------------------------------------- | ----------- | ------------ | +| `https://chat.duti.tech/api/conversation` | [@acheong08](https://github.com/acheong08) | 50 req/min | 2/19/2023 | +| `https://gpt.pawan.krd/backend-api/conversation` | [@PawanOsman](https://github.com/PawanOsman) | ? | 2/19/2023 | + +### Access Tokens + +To use `ChatGPTUnofficialProxyAPI`, you'll need a ChatGPT access token. You can either: + +1. Use [acheong08/OpenAIAuth](https://github.com/acheong08/OpenAIAuth), which is a python script to login and get an access token automatically. This works with email + password accounts (e.g., it does not support accounts where you auth using Microsoft / Google). + +2. You can manually get an `accessToken` by logging in to the ChatGPT webapp and then opening `https://chat.openai.com/api/auth/session`, which will return a JSON object containing your `accessToken` string. + +**Note**: using a reverse proxy will expose your access token to a third-party. There shouldn't be any adverse effects possible from this, but please consider the risks before using this method. + +## Docs See the [auto-generated docs](./docs/classes/ChatGPTAPI.md) for more info on methods and parameters. -### Demos +## Demos + +Most of the demos use `ChatGPTAPI`. It should be pretty easy to convert them to use `ChatGPTUnofficialProxyAPI` if you'd rather use that approach. The only thing that needs to change is how you initialize the api with an `accessToken` instead of an `apiKey`. To run the included demos: @@ -199,7 +292,7 @@ npx tsx demos/demo-conversation.ts A [persistence demo](./demos/demo-persistence.ts) shows how to store messages in Redis for persistence: ```bash -npx tsx demos/demo-conversation.ts +npx tsx demos/demo-persistence.ts ``` Any [keyv adaptor](https://github.com/jaredwray/keyv) is supported for persistence, and there are overrides if you'd like to use a different way of storing / retrieving messages. @@ -288,7 +381,7 @@ If you create a cool integration, feel free to open a PR and add it to the list. - This package supports `node >= 14`. - This module assumes that `fetch` is installed. - In `node >= 18`, it's installed by default. - - In `node < 18`, you need to install a polyfill like `unfetch/polyfill` ([guide](https://github.com/developit/unfetch#usage-as-a-polyfill)) or `isomorphic-fetch` ([guide](https://github.com/matthew-andrews/isomorphic-fetch#readme)). + - In `node < 18`, you need to install a polyfill like `unfetch/polyfill` ([guide](https://github.com/developit/unfetch#usage-as-a-polyfill)) or `isomorphic-fetch` ([guide](https://github.com/matthew-andrews/isomorphic-fetch#readme)). - If you want to build a website using `chatgpt`, we recommend using it only from your backend API ## Credits