Skip to content

pdshwc/chatgpt-nodejs

Repository files navigation

Update December 15, 2022

采用大佬Travis Fischer 为底板,修改fetch兼容性问题,由于开发vscode插件,node版本不能保证是多少,采用node-fetch兼容node内置fetch,兼容性更好。

On December 11th, OpenAI added Cloudflare protections that make it more difficult to access the unofficial API.

This package has been updated to use Puppeteer to automatically log in to ChatGPT and extract the necessary auth credentials. 🔥

Even with this in place, however, it's common to run into 429 / 403 errors at the moment using the getOpenAIAuth + ChatGPTAPI approach.

To circumvent these issues, we've also added a full browser-based solution, which uses Puppeteer to login and automate the webapp.

The full browser version is working consistently and can be used via:

import { ChatGPTAPIBrowser } from 'chatgpt'

const api = new ChatGPTAPIBrowser({
  email: process.env.OPENAI_EMAIL,
  password: process.env.OPENAI_PASSWORD
})
await api.init()

const response = await api.sendMessage('Hello World!')

Note that this solution is not lightweight, but it does work a lot more consistently than the REST API-based versions. I'm currently using this solution to power 10 OpenAI accounts concurrently across 10 minimized Chrome windows for my Twitter bot. 😂

If you get a "ChatGPT is at capacity" error when logging in, note that this can also happen on the official webapp as well. Their servers get overloaded at times, and we're all trying our best to offer access to this amazing technology.

To use the updated version, make sure you're using the latest version of this package and Node.js >= 18. Then update your code following the examples below, paying special attention to the sections on Authentication and Restrictions.

We're working hard to improve this process (especially CAPTCHA automation). Keep in mind that this package will be updated to use the official API as soon as it's released, so things should get much easier over time. 💪

Lastly, please consider starring this repo and following me on twitter twitter to help support the project.

Thanks && cheers, Travis


Example usage

ChatGPT API

Node.js client for the unofficial MIT License

Install

npm install chatgpt puppeteer

puppeteer is an optional peer dependency used to automate bypassing the Cloudflare protections via getOpenAIAuth. The main API wrapper uses fetch directly.

Usage

import { ChatGPTAPI, getOpenAIAuth } from 'chatgpt'

async function example() {
  // use puppeteer to bypass cloudflare (headful because of captchas)
  const openAIAuth = await getOpenAIAuth({
    email: process.env.OPENAI_EMAIL,
    password: process.env.OPENAI_PASSWORD
  })

  const api = new ChatGPTAPI({ ...openAIAuth })
  await api.ensureAuth()

  // send a message and wait for the response
  const response = await api.sendMessage(
    'Write a python version of bubble sort.'
  )

  // response is a markdown-formatted string
  console.log(response)
}

Or, if you want to try the full browser-based solution:

import { ChatGPTAPIBrowser } from 'chatgpt'

async function example() {
  // use puppeteer to bypass cloudflare (headful because of captchas)
  const api = new ChatGPTAPIBrowser({
    email: process.env.OPENAI_EMAIL,
    password: process.env.OPENAI_PASSWORD
  })

  await api.init()

  const response = await api.sendMessage('Hello World!')
  console.log(response)
}

ChatGPT responses are formatted as markdown by default. If you want to work with plaintext instead, you can use:

const api = new ChatGPTAPI({ ...openAIAuth, markdown: false })

If you want to automatically track the conversation, you can use ChatGPTAPI.getConversation():

const api = new ChatGPTAPI({ ...openAIAuth, markdown: false })

const conversation = api.getConversation()

// send a message and wait for the response
const response0 = await conversation.sendMessage('What is OpenAI?')

// send a follow-up
const response1 = await conversation.sendMessage('Can you expand on that?')

// send another follow-up
const response2 = await conversation.sendMessage('Oh cool; thank you')

Sometimes, ChatGPT will hang for an extended period of time before beginning to respond. This may be due to rate limiting or it may be due to OpenAI's servers being overloaded.

To mitigate these issues, you can add a timeout like this:

// timeout after 2 minutes (which will also abort the underlying HTTP request)
const response = await api.sendMessage('this is a timeout test', {
  timeoutMs: 2 * 60 * 1000
})

You can stream responses using the onProgress or onConversationResponse callbacks. See the docs for more details.

Usage in CommonJS (Dynamic import)
async function example() {
  // To use ESM in CommonJS, you can use a dynamic import
  const { ChatGPTAPI, getOpenAIAuth } = await import('chatgpt')

  const openAIAuth = await getOpenAIAuth({
    email: process.env.OPENAI_EMAIL,
    password: process.env.OPENAI_PASSWORD
  })

  const api = new ChatGPTAPI({ ...openAIAuth })
  await api.ensureAuth()

  const response = await api.sendMessage('Hello World!')
  console.log(response)
}

Docs

See the auto-generated docs for more info on methods and parameters. Here are the docs for the browser-based version.

Demos

To run the included demos:

  1. clone repo
  2. install node deps
  3. set OPENAI_EMAIL and OPENAI_PASSWORD in .env

A basic demo is included for testing purposes:

npx tsx demos/demo.ts

A conversation demo is also included:

npx tsx demos/demo-conversation.ts

A browser-based conversation demo is also included:

npx tsx demos/demo-conversation-browser.ts

Authentication

On December 11, 2022, OpenAI added some additional Cloudflare protections which make it more difficult to access the unofficial API.

You'll need a valid OpenAI "session token" and Cloudflare "clearance token" in order to use the API.

We've provided an automated, Puppeteer-based solution getOpenAIAuth to fetch these for you, but you may still run into cases where you have to manually pass the CAPTCHA. We're working on a solution to automate this further.

You can also get these tokens manually, but keep in mind that the clearanceToken only lasts for max 2 hours.

Getting tokens manually

To get session token manually:

  1. Go to https://chat.openai.com/chat and log in or sign up.
  2. Open dev tools.
  3. Open Application > Cookies. ChatGPT cookies
  4. Copy the value for __Secure-next-auth.session-token and save it to your environment. This will be your sessionToken.
  5. Copy the value for cf_clearance and save it to your environment. This will be your clearanceToken.
  6. Copy the value of the user-agent header from any request in your Network tab. This will be your userAgent.

Pass sessionToken, clearanceToken, and userAgent to the ChatGPTAPI constructor.

Note This package will switch to using the official API once it's released, which will make this process much simpler.

Restrictions

These restrictions are for the getOpenAIAuth + ChatGPTAPI solution, which uses the unofficial API. The browser-based solution, ChatGPTAPIBrowser, doesn't have many of these restrictions, though you'll still have to manually bypass CAPTCHAs by hand.

Please read carefully

  • You must use node >= 18 at the moment. I'm using v19.2.0 in my testing.
  • Cloudflare cf_clearance tokens expire after 2 hours, so right now we recommend that you refresh your cf_clearance token every hour or so.
  • Your user-agent and IP address must match from the real browser window you're logged in with to the one you're using for ChatGPTAPI.
    • This means that you currently can't log in with your laptop and then run the bot on a server or proxy somewhere.
  • Cloudflare will still sometimes ask you to complete a CAPTCHA, so you may need to keep an eye on it and manually resolve the CAPTCHA. Automated CAPTCHA bypass is coming soon.
  • You should not be using this account while the bot is using it, because that browser window may refresh one of your tokens and invalidate the bot's session.

Note Prior to v1.0.0, this package used a headless browser via Playwright to automate the web UI. Here are the docs for the initial browser version.

Projects

All of these awesome projects are built using the chatgpt package. 🤯

If you create a cool integration, feel free to open a PR and add it to the list.

Compatibility

This package is ESM-only. It supports:

  • Node.js >= 18
    • Node.js 17, 16, and 14 were supported in earlier versions, but OpenAI's Cloudflare update caused a bug with undici on v17 and v16 that needs investigation. So for now, use node >= 18
  • We recommend against using chatgpt from client-side browser code because it would expose your private session token
  • If you want to build a website using chatgpt, we recommend using it only from your backend API

Credits

License

MIT

About

基于nodejs的非官方chatgpt

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages