2022-12-03 00:04:53 +00:00
[chatgpt ](../readme.md ) / [Exports ](../modules.md ) / ChatGPTAPI
# Class: ChatGPTAPI
## Table of contents
### Constructors
- [constructor ](ChatGPTAPI.md#constructor )
2023-02-14 06:31:49 +00:00
### Accessors
- [apiKey ](ChatGPTAPI.md#apikey )
2022-12-03 00:04:53 +00:00
### Methods
- [sendMessage ](ChatGPTAPI.md#sendmessage )
## Constructors
### constructor
2022-12-05 05:13:36 +00:00
• **new ChatGPTAPI** (`opts`)
2023-02-01 10:58:25 +00:00
Creates a new client wrapper around OpenAI's completion API using the
unofficial ChatGPT model.
2022-12-12 17:23:19 +00:00
2022-12-03 00:04:53 +00:00
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `opts` | `Object` | - |
2023-02-01 10:58:25 +00:00
| `opts.apiBaseUrl?` | `string` | ** `Default Value` ** `'https://api.openai.com'` * |
| `opts.apiKey` | `string` | - |
2023-02-14 06:31:49 +00:00
| `opts.apiReverseProxyUrl?` | `string` | ** `Default Value` ** `undefined` * |
2023-02-02 00:12:29 +00:00
| `opts.assistantLabel?` | `string` | ** `Default Value` ** `'ChatGPT'` * |
2023-02-07 01:15:18 +00:00
| `opts.completionParams?` | `Partial` < [`CompletionParams`](../modules/openai.md#completionparams)\> | - |
2023-02-01 10:58:25 +00:00
| `opts.debug?` | `boolean` | ** `Default Value` ** `false` * |
2023-02-14 06:31:49 +00:00
| `opts.fetch?` | (`input`: `RequestInfo` \| `URL` , `init?` : `RequestInit` ) => `Promise` < `Response`\> | - |
2023-02-01 10:58:25 +00:00
| `opts.getMessageById?` | [`GetMessageByIdFunction` ](../modules.md#getmessagebyidfunction ) | - |
2023-02-02 00:12:29 +00:00
| `opts.maxModelTokens?` | `number` | ** `Default Value` ** `4096` * |
| `opts.maxResponseTokens?` | `number` | ** `Default Value` ** `1000` * |
2023-02-01 10:58:25 +00:00
| `opts.messageStore?` | `Keyv` < `any`, `Record` < `string`, `unknown` \>\> | - |
| `opts.upsertMessage?` | [`UpsertMessageFunction` ](../modules.md#upsertmessagefunction ) | - |
2023-02-02 00:12:29 +00:00
| `opts.userLabel?` | `string` | ** `Default Value` ** `'User'` * |
2022-12-12 17:23:19 +00:00
#### Defined in
2023-02-19 12:29:07 +00:00
[src/chatgpt-api.ts:53 ](https://github.com/transitive-bullshit/chatgpt-api/blob/607fccf/src/chatgpt-api.ts#L53 )
2023-02-14 06:31:49 +00:00
## Accessors
### apiKey
• `get` **apiKey** (): `string`
#### Returns
`string`
#### Defined in
2023-02-19 12:29:07 +00:00
[src/chatgpt-api.ts:353 ](https://github.com/transitive-bullshit/chatgpt-api/blob/607fccf/src/chatgpt-api.ts#L353 )
2023-02-14 06:31:49 +00:00
• `set` **apiKey** (`apiKey`): `void`
#### Parameters
| Name | Type |
| :------ | :------ |
| `apiKey` | `string` |
#### Returns
`void`
#### Defined in
2023-02-19 12:29:07 +00:00
[src/chatgpt-api.ts:357 ](https://github.com/transitive-bullshit/chatgpt-api/blob/607fccf/src/chatgpt-api.ts#L357 )
2022-12-03 00:04:53 +00:00
## Methods
### sendMessage
2023-02-01 10:58:25 +00:00
▸ **sendMessage** (`text`, `opts?` ): `Promise` < [`ChatMessage`](../interfaces/ChatMessage.md)\>
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
2023-02-02 00:12:29 +00:00
If you want your response to have historical context, you must provide a valid `parentMessageId` .
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.
2023-02-02 00:12:29 +00:00
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.
2022-12-03 00:04:53 +00:00
#### Parameters
2022-12-15 23:15:38 +00:00
| Name | Type |
| :------ | :------ |
2023-02-01 10:58:25 +00:00
| `text` | `string` |
| `opts` | [`SendMessageOptions` ](../modules.md#sendmessageoptions ) |
2022-12-15 23:15:38 +00:00
#### Returns
2023-02-01 10:58:25 +00:00
`Promise` < [`ChatMessage`](../interfaces/ChatMessage.md)\>
The response from ChatGPT
2022-12-15 23:15:38 +00:00
#### Defined in
2023-02-19 12:29:07 +00:00
[src/chatgpt-api.ts:185 ](https://github.com/transitive-bullshit/chatgpt-api/blob/607fccf/src/chatgpt-api.ts#L185 )