Skip to content

Latest commit

 

History

History
310 lines (173 loc) · 7.13 KB

ChatGPTAPI.md

File metadata and controls

310 lines (173 loc) · 7.13 KB

chatgpt / Exports / ChatGPTAPI

Class: ChatGPTAPI

Hierarchy

Table of contents

Constructors

Accessors

Methods

Constructors

constructor

new ChatGPTAPI(opts)

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 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 = 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/108.0.0.0 Safari/537.36' *

Overrides

AChatGPTAPI.constructor

Defined in

src/chatgpt-api.ts:45

Accessors

clearanceToken

get clearanceToken(): string

Gets the current Cloudflare clearance token (cf_clearance cookie value).

Returns

string

Defined in

src/chatgpt-api.ts:143


sessionToken

get sessionToken(): string

Gets the current session token.

Returns

string

Defined in

src/chatgpt-api.ts:138


user

get user(): User

Gets the currently signed-in user, if authenticated, null otherwise.

Returns

User

Defined in

src/chatgpt-api.ts:133


userAgent

get userAgent(): string

Gets the current user agent.

Returns

string

Defined in

src/chatgpt-api.ts:148

Methods

closeSession

closeSession(): Promise<void>

Closes the active session.

Throws

An error if it fails.

Returns

Promise<void>

Overrides

AChatGPTAPI.closeSession

Defined in

src/chatgpt-api.ts:470


getIsAuthenticated

getIsAuthenticated(): Promise<boolean>

Returns

Promise<boolean>

true if the client has a valid acces token or false if refreshing the token fails.

Overrides

AChatGPTAPI.getIsAuthenticated

Defined in

src/chatgpt-api.ts:367


initSession

initSession(): Promise<void>

Refreshes the client's access token which will succeed only if the session is valid.

Returns

Promise<void>

Overrides

AChatGPTAPI.initSession

Defined in

src/chatgpt-api.ts:156


refreshSession

refreshSession(): Promise<string>

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.

Returns

Promise<string>

A valid access token

Overrides

AChatGPTAPI.refreshSession

Defined in

src/chatgpt-api.ts:386


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.resetSession

Defined in

src/abstract-chatgpt-api.ts:59


sendMessage

sendMessage(message, opts?): Promise<ChatResponse>

Sends a message to ChatGPT, waits for the response to resolve, and returns the response.

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.

Parameters

Name Type Description
message string The prompt message to send
opts SendMessageOptions -

Returns

Promise<ChatResponse>

The response from ChatGPT

Overrides

AChatGPTAPI.sendMessage

Defined in

src/chatgpt-api.ts:180


sendModeration

sendModeration(input): Promise<ModerationsJSONResult>

Parameters

Name Type
input string

Returns

Promise<ModerationsJSONResult>

Defined in

src/chatgpt-api.ts:324