ChatGPT API For Nodejs client

ChatGPT API

Node.js client for the official ChatGPT API.

NPM Build Status MIT License Prettier Code Formatting

Intro

This package is a Node.js wrapper around ChatGPT by OpenAI. TS batteries included. โœจ

Example usage


Updates

March 1, 2023


The official OpenAI chat completions API has been released, and it is now the default for this package! ๐Ÿ”ฅ

MethodFree?Robust?Quality?ChatGPTAPIโŒ Noโœ… Yesโœ…๏ธ Real ChatGPT modelsChatGPTUnofficialProxyAPIโœ… Yesโ˜‘๏ธ Maybeโœ… Real ChatGPT

Note: We strongly recommend using ChatGPTAPI since it uses the officially supported API from OpenAI. We may remove support for ChatGPTUnofficialProxyAPI in a future release.

  1. ChatGPTAPI - Uses the gpt-3.5-turbo-0301 model with the official OpenAI chat completions API (official, robust approach, but it's not free)
  2. ChatGPTUnofficialProxyAPI - Uses an unofficial proxy server to access ChatGPT's backend API in a way that circumvents Cloudflare (uses the real ChatGPT and is pretty lightweight, but relies on a third-party server and is rate-limited)

Previous Updates

If you run into any issues, we do have a pretty active Discord with a bunch of ChatGPT hackers from the Node.js & Python communities.

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

Thanks && cheers, Travis


CLI

To run the CLI, you'll need an OpenAI API key:

export OPENAI_API_KEY="sk-TODO"
npx chatgpt "your prompt here"

By default, the response is streamed to stdout, the results are stored in a local config file, and every invocation starts a new conversation. You can use -c to continue the previous conversation and --no-stream to disable streaming.

Usage:
  $ chatgpt <prompt>

Commands:
  <prompt>  Ask ChatGPT a question
  rm-cache  Clears the local message cache
  ls-cache  Prints the local message cache path

For more info, run any command with the `--help` flag:
  $ chatgpt --help
  $ chatgpt rm-cache --help
  $ chatgpt ls-cache --help

Options:
  -c, --continue          Continue last conversation (default: false)
  -d, --debug             Enables debug logging (default: false)
  -s, --stream            Streams the response (default: true)
  -s, --store             Enables the local message cache (default: true)
  -t, --timeout           Timeout in milliseconds
  -k, --apiKey            OpenAI API key
  -n, --conversationName  Unique name for the conversation
  -h, --help              Display this message
  -v, --version           Display version number


Install

npm install chatgpt

Make sure you're using node >= 18 so fetch is available (or node >= 14 if you install a fetch polyfill).


Usage

To use this module from Node.js, you need to pick between two methods:

MethodFree?Robust?Quality?ChatGPTAPIโŒ Noโœ… Yesโœ…๏ธ Real ChatGPT modelsChatGPTUnofficialProxyAPIโœ… Yesโ˜‘๏ธ Maybeโœ… Real ChatGPT

  1. ChatGPTAPI - Uses the gpt-3.5-turbo-0301 model with the official OpenAI chat completions API (official, robust approach, but it's not free). You can override the model, completion params, and system message to fully customize your assistant.

  2. ChatGPTUnofficialProxyAPI - Uses an unofficial proxy server to access ChatGPT's backend API in a way that circumvents Cloudflare (uses the real ChatGPT and is pretty lightweight, but relies on a third-party server and is rate-limited)

Both approaches have very similar APIs, so it should be simple to swap between them.

Note: We strongly recommend using ChatGPTAPI since it uses the officially supported API from OpenAI. We may remove support for ChatGPTUnofficialProxyAPI in a future release.


Usage - ChatGPTAPI

Sign up for an OpenAI API key and store it in your environment.

import { ChatGPTAPI } from 'chatgpt'

async function example() {
  const api = new ChatGPTAPI({
    apiKey: process.env.OPENAI_API_KEY
  })

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

You can override the default model (gpt-3.5-turbo-0301) and any OpenAI chat completion params using completionParams:

const api = new ChatGPTAPI({
  apiKey: process.env.OPENAI_API_KEY,
  completionParams: {
    temperature: 0.5,
    top_p: 0.8
  }
})

If you want to track the conversation, you'll need to pass the parentMessageId like this:

const api = new ChatGPTAPI({ apiKey: process.env.OPENAI_API_KEY })

// send a message and wait for the response
let res = await api.sendMessage('What is OpenAI?')
console.log(res.text)

// send a follow-up
res = await api.sendMessage('Can you expand on that?', {
  parentMessageId: res.id
})
console.log(res.text)

// send another follow-up
res = await api.sendMessage('What were we talking about?', {
  parentMessageId: res.id
})
console.log(res.text)

You can add streaming via the onProgress handler:

const res = await api.sendMessage('Write a 500 word essay on frogs.', {
  // print the partial response as the AI is "typing"
  onProgress: (partialResponse) => console.log(partialResponse.text)
})

// print the full text at the end
console.log(res.text)

You can add a timeout using the timeoutMs option:

// timeout after 2 minutes (which will also abort the underlying HTTP request)
const response = await api.sendMessage(
  'write me a really really long essay on frogs',
  {
    timeoutMs: 2 * 60 * 1000
  }
)

If you want to see more info about what's actually being sent to OpenAI's chat completions API, set the debug: true option in the ChatGPTAPI constructor:

const api = new ChatGPTAPI({
  apiKey: process.env.OPENAI_API_KEY,
  debug: true
})

We default to a basic systemMessage. You can override this in either the ChatGPTAPI constructor or sendMessage:

const res = await api.sendMessage('what is the answer to the universe?', {
  systemMessage: `You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each responseIf you are generating a list, do not have too many items.
Current date: ${new Date().toISOString()}\n\n`
})

Note that we automatically handle appending the previous messages to the prompt and attempt to optimize for the available tokens (which defaults to 4096).

Usage in CommonJS (Dynamic import)


Usage - ChatGPTUnofficialProxyAPI

The API for ChatGPTUnofficialProxyAPI is almost exactly the same. You just need to provide a ChatGPT accessToken instead of an OpenAI API key.

import { ChatGPTUnofficialProxyAPI } from 'chatgpt'

async function example() {
  const api = new ChatGPTUnofficialProxyAPI({
    accessToken: process.env.OPENAI_ACCESS_TOKEN
  })

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

See demos/demo-reverse-proxy for a full example:

npx tsx demos/demo-reverse-proxy.ts

ChatGPTUnofficialProxyAPI messages also contain a conversationid in addition to parentMessageId, since the ChatGPT webapp can't reference messages across different accounts & conversations.


Reverse Proxy

You can override the reverse proxy by passing apiReverseProxyUrl:

const api = new ChatGPTUnofficialProxyAPI({
  accessToken: process.env.OPENAI_ACCESS_TOKEN,
  apiReverseProxyUrl: 'https://your-example-server.com/api/conversation'
})

Known reverse proxies run by community members include:

Reverse Proxy URLAuthorRate LimitsLast Checkedhttps://bypass.churchless.tech/api/conversation@acheong085 req / 10 seconds by IP3/24/2023https://api.pawan.krd/backend-api/conversation@PawanOsman50 req / 15 seconds (~3 r/s)3/23/2023

Note: info on how the reverse proxies work is not being published at this time in order to prevent OpenAI from disabling access.


Access Token

To use ChatGPTUnofficialProxyAPI, you'll need an OpenAI access token from the ChatGPT webapp. To do this, you can use any of the following methods which take an email and password and return an access token:

These libraries work with email + password accounts (e.g., they do not support accounts where you auth via Microsoft / Google).

Alternatively, you can manually get an accessToken by logging in to the ChatGPT webapp and then opening https://chat.openai.com/api/auth/session, which will return a JSON object containing your accessToken string.

Access tokens last for days.

Note: using a reverse proxy will expose your access token to a third-party. There shouldn't be any adverse effects possible from this, but please consider the risks before using this method.


Docs

See the auto-generated docs for more info on methods and parameters.


Demos

Most of the demos use ChatGPTAPI. It should be pretty easy to convert them to use ChatGPTUnofficialProxyAPI if you'd rather use that approach. The only thing that needs to change is how you initialize the api with an accessToken instead of an apiKey.

To run the included demos:

  1. clone repo
  2. install node deps
  3. set OPENAI_API_KEY in .env

basic demo is included for testing purposes:

npx tsx demos/demo.ts

demo showing on progress handler:

npx tsx demos/demo-on-progress.ts

The on progress demo uses the optional onProgress parameter to sendMessage to receive intermediary results as ChatGPT is "typing".

conversation demo:

npx tsx demos/demo-conversation.ts

persistence demo shows how to store messages in Redis for persistence:

npx tsx demos/demo-persistence.ts

Any keyv adaptor is supported for persistence, and there are overrides if you'd like to use a different way of storing / retrieving messages.

Note that persisting message is required for remembering the context of previous conversations beyond the scope of the current Node.js process, since by default, we only store messages in memory. Here's an external demo of using a completely custom database solution to persist messages.

Note: Persistence is handled automatically when using ChatGPTUnofficialProxyAPI because it is connecting indirectly to ChatGPT.


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.
  • This package supports node >= 14.
  • This module assumes that fetch is installed.
  • In node >= 18, it's installed by default.
  • In node < 18, you need to install a polyfill like unfetch/polyfill (guide) or isomorphic-fetch (guide).
  • If you want to build a website using chatgpt, we recommend using it only from your backend API


Credits


License

MIT ยฉ Travis Fischer

If you found this project interesting, please consider sponsoring me or following me on twitter twitter

Related Posts

Popular Posts

BrowserVideoEdit: A feature-rich video editor created using fabric.js and Next.js, all within the convenience of your web browser

A weather app that allows users to view real-time weather information based on their locations

Add Login and Register page into your Nuxt 3 project using Supabase authentication

A powerful Flutter package that allows you to easily create and control glitch effects

เด’เดฐเต‡เดฆเดฟเดตเดธเด‚ เดฐเดฃเตเดŸเตเดชเต‡เดฐเต†เดฏเตเด‚ เดชเต†เดฃเตเดฃเตเด•เดฃเตเดŸเต, เด•เต‹เดŸเตเดŸเดฏเด‚ เดชเต‚เดžเตเดžเดพเดฐเตโ€ เดธเตเดตเดฆเต‡เดถเดฟเดฏเดพเดฏ เดฆเดจเตเดคเดกเต‹เด•เตเดŸเดฑเตเดฎเดพเดฏเดฟ เดตเดฟเดตเดพเดนเด‚ เดฐเดœเดฟเดธเตเดฑเตเดฑเตผ เดšเต†เดฏเตเดคเต , เดชเดฟเดจเตเดจเต€เดŸเต เดตเต‡เดฃเตเดŸเต†เดจเตเดจเตเดตเต†เดšเตเดšเต.

A Library for Rendering 3D Models in React.js and Next.js Views

Recent Posts

เด‡เดŸเตเด•เตเด•เดฟเดฏเดฟเดฒเต† เดฎเดฒเดฏเต‹เดฐ เดฎเต‡เด–เดฒเด•เดณเดฟเตฝ เดฐเดพเดคเตเดฐเดฟเดฏเดพเดคเตเดฐ เดจเดฟเดฐเต‹เดงเดฟเดšเตเดšเต. เดฐเดพเดคเตเดฐเดฟ เดเดดเต เดฎเตเดคเตฝ เดฐเดพเดตเดฟเดฒเต† เด†เดฑเต เดตเดฐเต†เดฏเดพเดฃเต เดจเดฟเดฐเต‹เดงเดจเด‚

เดเดจเตเดคเดฏเดพเตผ เดˆเดธเตเดฑเตเดฑเดฟเตฝ เดชเตเดฐเดณเดฏเดคเตเดคเดฟเตฝ เดคเด•เตผเดจเตเดจ เดชเดพเดฒเดคเตเดคเดฟเดจเต เดชเด•เดฐเด‚ เดชเตเดคเดฟเดฏ เดชเดพเดฒเด‚ เดจเดฟเตผเดฎเตเดฎเดฟเด•เตเด•เตเดตเดพเตป เดคเดพเดคเตเด•เตเด•เดพเดฒเดฟเด• เดชเดพเดฒเด‚ เดชเตŠเดณเดฟเดšเตเดšเต เดจเต€เด•เตเด•เดฟ

Explore the Investment Opportunities: A Comprehensive Guide to Different Types of Mutual Funds

Title: Understanding Mutual Funds: A Beginner's Guide to Investing

เดคเต€เดตเตเดฐเดฎเดด เดฎเตเดจเตเดจเดฑเดฟเดฏเดฟเดชเตเดชเดฟเดจเตเดฑเต† เดชเดถเตเดšเดพเดคเดฒเดคเตเดคเดฟเตฝ เดธเด‚เดธเตเดฅเดพเดจเด‚ เดœเดพเด—เตเดฐเดคเดฏเดฟเตฝ

250,000 เด…เดชเต‡เด•เตเดทเด•เตพ เดตเตผเดฆเตเดงเดฟเดšเตเดšเดคเดฟเดจเดพเตฝ เดŸเตเดฐเดพเตปเดธเตโ€Œเดชเต‹เตผเดŸเตเดŸเต เด•เดฎเตเดฎเต€เดทเดฃเตผ เดชเดฐเดฟเดถเต‹เดงเดจ เดชเตเดจเดฐเดพเดฐเด‚เดญเดฟเด•เตเด•เตเด‚

เดเดฒเด•เตเด•เดฏเดฟเตฝ เด•เต€เดŸเดจเดพเดถเดฟเดจเดฟ เดธเดพเดจเตเดจเดฟเดงเตเดฏเด‚; เด†เดฑเดฐ เดฒเด•เตเดทเดคเตเดคเดฟเดฒเดงเดฟเด•เด‚ เดŸเดฟเตป เด…เดฐเดตเดฃ เดจเดถเดฟเดชเตเดชเดฟเด•เตเด•เดพเตป เดŸเต†เตปเดกเตผ เด•เตเดทเดฃเดฟเดšเตเดšเต เดฆเต‡เดตเดธเตเดตเด‚ เดฌเต‹เตผเดกเตโ€Œ

เดญเต€เดฎเตป เดชเดพเดฑเด•เตเด•เดทเดฃเด™เตเด™เตพ เด…เดŸเตผเดจเตเดจเต เดฆเต‡เดถเต€เดฏ เดชเดพเดคเดฏเดฟเดฒเต‡เด•เตเด•เต เดตเต€เดดเตเดจเตเดจเดคเต เดชเดคเดฟเดตเดพเด•เตเดจเตเดจเต. เด•เตเดŸเตเดŸเดฟเด•เตเด•เดพเดจเดคเตเดคเดฟเดจเตเด‚ เดฎเตเดฃเตเดŸเด•เตเด•เดฏเดคเตเดคเดฟเดจเตเดฎเดฟเดŸเดฏเดฟเตฝ เดจเดฟเดฒเดจเดฟเตฝเด•เตเด•เตเดจเตเดจเดคเต เดตเตป เด…เดชเด•เดŸ เดญเต€เดทเดฃเดฟ

เดšเด•เตเดฐเดตเดพเดคเดšเตเดšเตเดดเดฟ:เด…เดคเดฟเดถเด•เตเดคเดฎเดพเดฏ เดฎเดด เดตเดฐเตเดจเตเดจเต

เดชเตเดฒเดธเต เดตเตบ เดชเตเดฐเดตเต‡เดถเดจเด‚. เด…เด•เตเดทเดฏเดฏเดฟเตฝ เดคเดฟเด•เตเด•เดฟ เดคเดฟเดฐเด•เตเด•เต‡เดฃเตเดŸ, เดจเต†เดฑเตเดฑเดฟเดตเดฟเดฑเตเดฑเดฟ/เดœเดพเดคเดฟ เดคเต†เดณเดฟเดฏเดฟเด•เตเด•เดพเตป เดชเดคเตเดคเดพเด‚เดคเดฐเด‚ เดธเตผเดŸเตเดŸเดฟเดซเดฟเด•เตเด•เดฑเตเดฑเต เดฎเดคเดฟ