3.1 KiB
chatgpt / Exports / ChatGPTAPI
Class: ChatGPTAPI
Table of contents
Constructors
Accessors
Methods
Constructors
constructor
• new ChatGPTAPI(opts
)
Creates a new client wrapper around OpenAI's chat completion API, mimicing the official ChatGPT webapp's functionality as closely as possible.
Parameters
Name | Type | Description |
---|---|---|
opts |
Object |
- |
opts.apiBaseUrl? |
string |
Default Value 'https://api.openai.com' * |
opts.apiKey |
string |
- |
opts.completionParams? |
Partial <Omit <CreateChatCompletionRequest , "messages" | "n" >> |
- |
opts.debug? |
boolean |
Default Value false * |
opts.fetch? |
(input : RequestInfo | URL , init? : RequestInit ) => Promise <Response > |
- |
opts.getMessageById? |
GetMessageByIdFunction |
- |
opts.maxModelTokens? |
number |
Default Value 4096 * |
opts.maxResponseTokens? |
number |
Default Value 1000 * |
opts.messageStore? |
Keyv <any , Record <string , unknown >> |
- |
opts.systemMessage? |
string |
- |
opts.upsertMessage? |
UpsertMessageFunction |
- |
Defined in
Accessors
apiKey
• get
apiKey(): string
Returns
string
Defined in
• set
apiKey(apiKey
): void
Parameters
Name | Type |
---|---|
apiKey |
string |
Returns
void
Defined in
Methods
sendMessage
▸ sendMessage(text
, opts?
): Promise
<ChatMessage
>
Sends a message to the OpenAI chat completions endpoint, 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
.
Set debug: true
in the ChatGPTAPI
constructor to log more info on the full prompt sent to the OpenAI chat completions API. You can override the systemMessage
in opts
to customize the assistant's instructions.
Parameters
Name | Type |
---|---|
text |
string |
opts |
SendMessageOptions |
Returns
Promise
<ChatMessage
>
The response from ChatGPT