2022-12-03 00:04:53 +00:00
|
|
|
[chatgpt](../readme.md) / [Exports](../modules.md) / ChatGPTAPI
|
|
|
|
|
|
|
|
# Class: ChatGPTAPI
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
## Hierarchy
|
|
|
|
|
|
|
|
- [`AChatGPTAPI`](AChatGPTAPI.md)
|
|
|
|
|
|
|
|
↳ **`ChatGPTAPI`**
|
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
## Table of contents
|
|
|
|
|
|
|
|
### Constructors
|
|
|
|
|
|
|
|
- [constructor](ChatGPTAPI.md#constructor)
|
|
|
|
|
2022-12-12 00:04:20 +00:00
|
|
|
### Accessors
|
|
|
|
|
2022-12-12 17:23:19 +00:00
|
|
|
- [clearanceToken](ChatGPTAPI.md#clearancetoken)
|
|
|
|
- [sessionToken](ChatGPTAPI.md#sessiontoken)
|
2022-12-12 00:04:20 +00:00
|
|
|
- [user](ChatGPTAPI.md#user)
|
2022-12-12 17:23:19 +00:00
|
|
|
- [userAgent](ChatGPTAPI.md#useragent)
|
2022-12-12 00:04:20 +00:00
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
### Methods
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
- [closeSession](ChatGPTAPI.md#closesession)
|
2022-12-05 05:13:36 +00:00
|
|
|
- [getIsAuthenticated](ChatGPTAPI.md#getisauthenticated)
|
2022-12-17 09:57:22 +00:00
|
|
|
- [initSession](ChatGPTAPI.md#initsession)
|
|
|
|
- [refreshSession](ChatGPTAPI.md#refreshsession)
|
|
|
|
- [resetSession](ChatGPTAPI.md#resetsession)
|
2022-12-03 00:04:53 +00:00
|
|
|
- [sendMessage](ChatGPTAPI.md#sendmessage)
|
2022-12-15 23:15:38 +00:00
|
|
|
- [sendModeration](ChatGPTAPI.md#sendmoderation)
|
2022-12-03 00:04:53 +00:00
|
|
|
|
|
|
|
## Constructors
|
|
|
|
|
|
|
|
### constructor
|
|
|
|
|
2022-12-05 05:13:36 +00:00
|
|
|
• **new ChatGPTAPI**(`opts`)
|
|
|
|
|
|
|
|
Creates a new client wrapper around the unofficial ChatGPT REST API.
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-12 17:23:19 +00:00
|
|
|
Note that your IP address and `userAgent` must match the same values that you used
|
|
|
|
to obtain your `clearanceToken`.
|
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
| Name | Type | Description |
|
|
|
|
| :------ | :------ | :------ |
|
|
|
|
| `opts` | `Object` | - |
|
2022-12-12 17:23:19 +00:00
|
|
|
| `opts.accessToken?` | `string` | **`Default Value`** `undefined` * |
|
|
|
|
| `opts.accessTokenTTL?` | `number` | **`Default Value`** 1 hour * |
|
2022-12-05 05:13:36 +00:00
|
|
|
| `opts.apiBaseUrl?` | `string` | **`Default Value`** `'https://chat.openai.com/api'` * |
|
|
|
|
| `opts.backendApiBaseUrl?` | `string` | **`Default Value`** `'https://chat.openai.com/backend-api'` * |
|
2022-12-12 17:23:19 +00:00
|
|
|
| `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` * |
|
2022-12-03 00:04:53 +00:00
|
|
|
| `opts.markdown?` | `boolean` | **`Default Value`** `true` * |
|
2022-12-05 05:13:36 +00:00
|
|
|
| `opts.sessionToken` | `string` | = **Required** OpenAI session token which can be found in a valid session's cookies (see readme for instructions) |
|
2022-12-12 17:23:19 +00:00
|
|
|
| `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'` * |
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
#### Overrides
|
|
|
|
|
|
|
|
[AChatGPTAPI](AChatGPTAPI.md).[constructor](AChatGPTAPI.md#constructor)
|
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:45](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L45)
|
2022-12-12 00:04:20 +00:00
|
|
|
|
|
|
|
## Accessors
|
|
|
|
|
2022-12-12 17:23:19 +00:00
|
|
|
### clearanceToken
|
|
|
|
|
|
|
|
• `get` **clearanceToken**(): `string`
|
|
|
|
|
|
|
|
Gets the current Cloudflare clearance token (`cf_clearance` cookie value).
|
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`string`
|
|
|
|
|
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:143](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L143)
|
2022-12-12 17:23:19 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### sessionToken
|
|
|
|
|
|
|
|
• `get` **sessionToken**(): `string`
|
|
|
|
|
|
|
|
Gets the current session token.
|
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`string`
|
|
|
|
|
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:138](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L138)
|
2022-12-12 17:23:19 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
2022-12-12 00:04:20 +00:00
|
|
|
### user
|
|
|
|
|
|
|
|
• `get` **user**(): [`User`](../modules.md#user)
|
|
|
|
|
|
|
|
Gets the currently signed-in user, if authenticated, `null` otherwise.
|
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
|
|
|
[`User`](../modules.md#user)
|
|
|
|
|
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:133](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L133)
|
2022-12-12 17:23:19 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### userAgent
|
|
|
|
|
|
|
|
• `get` **userAgent**(): `string`
|
|
|
|
|
|
|
|
Gets the current user agent.
|
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`string`
|
|
|
|
|
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:148](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L148)
|
2022-12-03 00:04:53 +00:00
|
|
|
|
|
|
|
## Methods
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
### closeSession
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
▸ **closeSession**(): `Promise`<`void`\>
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
Closes the active session.
|
|
|
|
|
|
|
|
**`Throws`**
|
|
|
|
|
|
|
|
An error if it fails.
|
2022-12-07 04:37:56 +00:00
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
#### Returns
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
`Promise`<`void`\>
|
|
|
|
|
|
|
|
#### Overrides
|
|
|
|
|
|
|
|
[AChatGPTAPI](AChatGPTAPI.md).[closeSession](AChatGPTAPI.md#closesession)
|
2022-12-03 00:04:53 +00:00
|
|
|
|
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:470](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L470)
|
2022-12-07 00:19:50 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
### getIsAuthenticated
|
2022-12-07 00:19:50 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
▸ **getIsAuthenticated**(): `Promise`<`boolean`\>
|
2022-12-07 00:19:50 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
#### Returns
|
2022-12-07 00:19:50 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
`Promise`<`boolean`\>
|
2022-12-07 00:19:50 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
`true` if the client has a valid acces token or `false` if refreshing
|
|
|
|
the token fails.
|
2022-12-07 00:19:50 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
#### Overrides
|
2022-12-07 00:19:50 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
[AChatGPTAPI](AChatGPTAPI.md).[getIsAuthenticated](AChatGPTAPI.md#getisauthenticated)
|
2022-12-07 00:19:50 +00:00
|
|
|
|
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:367](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L367)
|
2022-12-03 00:04:53 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
### initSession
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
▸ **initSession**(): `Promise`<`void`\>
|
|
|
|
|
|
|
|
Refreshes the client's access token which will succeed only if the session
|
|
|
|
is valid.
|
2022-12-03 00:04:53 +00:00
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
`Promise`<`void`\>
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
#### Overrides
|
|
|
|
|
|
|
|
[AChatGPTAPI](AChatGPTAPI.md).[initSession](AChatGPTAPI.md#initsession)
|
2022-12-07 04:37:56 +00:00
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:156](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L156)
|
2022-12-03 00:04:53 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
### refreshSession
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
▸ **refreshSession**(): `Promise`<`string`\>
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-07 04:37:56 +00:00
|
|
|
Attempts to refresh the current access token using the ChatGPT
|
|
|
|
`sessionToken` cookie.
|
|
|
|
|
|
|
|
Access tokens will be cached for up to `accessTokenTTL` milliseconds to
|
|
|
|
prevent refreshing access tokens too frequently.
|
|
|
|
|
|
|
|
**`Throws`**
|
|
|
|
|
|
|
|
An error if refreshing the access token fails.
|
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`Promise`<`string`\>
|
|
|
|
|
2022-12-07 04:37:56 +00:00
|
|
|
A valid access token
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
#### Overrides
|
|
|
|
|
|
|
|
[AChatGPTAPI](AChatGPTAPI.md).[refreshSession](AChatGPTAPI.md#refreshsession)
|
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:386](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L386)
|
2022-12-17 09:57:22 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### resetSession
|
|
|
|
|
|
|
|
▸ **resetSession**(): `Promise`<`any`\>
|
|
|
|
|
|
|
|
Closes the current ChatGPT session and starts a new one.
|
|
|
|
|
|
|
|
Useful for bypassing 401 errors when sessions expire.
|
|
|
|
|
|
|
|
**`Throws`**
|
|
|
|
|
|
|
|
An error if it fails.
|
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`Promise`<`any`\>
|
|
|
|
|
|
|
|
Access credentials for the new session.
|
|
|
|
|
|
|
|
#### Inherited from
|
|
|
|
|
|
|
|
[AChatGPTAPI](AChatGPTAPI.md).[resetSession](AChatGPTAPI.md#resetsession)
|
|
|
|
|
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/abstract-chatgpt-api.ts:59](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/abstract-chatgpt-api.ts#L59)
|
2022-12-03 00:04:53 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### sendMessage
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
▸ **sendMessage**(`message`, `opts?`): `Promise`<[`ChatResponse`](../modules.md#chatresponse)\>
|
2022-12-05 05:13:36 +00:00
|
|
|
|
|
|
|
Sends a message to ChatGPT, waits for the response to resolve, and returns
|
|
|
|
the response.
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-07 04:37:56 +00:00
|
|
|
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.
|
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
#### Parameters
|
|
|
|
|
2022-12-05 05:13:36 +00:00
|
|
|
| Name | Type | Description |
|
|
|
|
| :------ | :------ | :------ |
|
2022-12-07 00:19:50 +00:00
|
|
|
| `message` | `string` | The prompt message to send |
|
|
|
|
| `opts` | [`SendMessageOptions`](../modules.md#sendmessageoptions) | - |
|
2022-12-03 00:04:53 +00:00
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
`Promise`<[`ChatResponse`](../modules.md#chatresponse)\>
|
2022-12-03 00:04:53 +00:00
|
|
|
|
2022-12-07 00:19:50 +00:00
|
|
|
The response from ChatGPT
|
|
|
|
|
2022-12-17 09:57:22 +00:00
|
|
|
#### Overrides
|
|
|
|
|
|
|
|
[AChatGPTAPI](AChatGPTAPI.md).[sendMessage](AChatGPTAPI.md#sendmessage)
|
|
|
|
|
2022-12-03 00:04:53 +00:00
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:180](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L180)
|
2022-12-15 23:15:38 +00:00
|
|
|
|
|
|
|
___
|
|
|
|
|
|
|
|
### sendModeration
|
|
|
|
|
|
|
|
▸ **sendModeration**(`input`): `Promise`<[`ModerationsJSONResult`](../modules.md#moderationsjsonresult)\>
|
|
|
|
|
|
|
|
#### Parameters
|
|
|
|
|
|
|
|
| Name | Type |
|
|
|
|
| :------ | :------ |
|
|
|
|
| `input` | `string` |
|
|
|
|
|
|
|
|
#### Returns
|
|
|
|
|
|
|
|
`Promise`<[`ModerationsJSONResult`](../modules.md#moderationsjsonresult)\>
|
|
|
|
|
|
|
|
#### Defined in
|
|
|
|
|
2022-12-22 07:06:19 +00:00
|
|
|
[src/chatgpt-api.ts:324](https://github.com/transitive-bullshit/chatgpt-api/blob/d1b74a8/src/chatgpt-api.ts#L324)
|