chatgpt-api/docs
Travis Fischer 3a0259c128 docs: update readme and auto-gen docs 2023-02-01 04:58:25 -06:00
..
classes docs: update readme and auto-gen docs 2023-02-01 04:58:25 -06:00
interfaces docs: update readme and auto-gen docs 2023-02-01 04:58:25 -06:00
modules docs: update readme and auto-gen docs 2023-02-01 04:58:25 -06:00
.nojekyll feat: add docs; tweak ts 2022-12-02 18:04:53 -06:00
modules.md docs: update readme and auto-gen docs 2023-02-01 04:58:25 -06:00
readme.md docs: update readme and auto-gen docs 2023-02-01 04:58:25 -06:00

readme.md

chatgpt / Exports

Update February 1, 2023

This package no longer requires any browser hacks – it is now using the official OpenAI API with a leaked, unofficial ChatGPT model. 🔥

import { ChatGPTAPI } from 'chatgpt'

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

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

The updated solution is significantly more lightweight and robust compared with previous versions.

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


Example usage

ChatGPT API

Node.js client for the unofficial 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.

You can use it to start building projects powered by ChatGPT like chatbots, websites, etc...

Install

npm install chatgpt

Usage

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)
}

If you want to track the conversation, use the conversationId and id in the result object, and pass them to sendMessage as conversationId and parentMessageId respectively.

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?', {
  conversationId: res.conversationId,
  parentMessageId: res.id
})
console.log(res.text)

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

You can add streaming via the onProgress handler:

// timeout after 2 minutes (which will also abort the underlying HTTP request)
const res = await api.sendMessage('Write me a 500 word essay on frogs.', {
  onProgress: (partialResponse) => console.log(partialResponse)
})

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('this is a timeout test', {
  timeoutMs: 2 * 60 * 1000
})
Usage in CommonJS (Dynamic import)
async function example() {
  // To use ESM in CommonJS, you can use a dynamic import
  const { ChatGPTAPI } = await import('chatgpt')

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

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

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_API_KEY in .env

A basic demo is included for testing purposes:

npx tsx demos/demo.ts

A 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".

A conversation demo:

npx tsx demos/demo-conversation.ts

Lastly, a persitence demo shows how to store messages in Redis for persistence:

npx tsx demos/demo-conversation.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 very important for remembering the context of previous conversations.

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
  • 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