This package no longer requires any browser hacks โย it is now using the official OpenAI completions API with a leaked model that ChatGPT uses under the hood. ๐ฅ
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)
Please upgrade to chatgpt@latest
(at least v4.0.0). The updated version is significantly more lightweight and robust compared with previous versions. You also don't have to worry about IP issues or rate limiting!
Huge shoutout to @waylaidwanderer for discovering the leaked chat model! ๐ช
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 to help support the project.
Thanks && cheers, Travis
Node.js client for the unofficial ChatGPT API.
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...
npm install chatgpt
Make sure you're using node >= 18
so fetch
is available (or node >= 14
if you install a fetch polyfill).
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)
}
See the auto-generated docs for more info on methods and parameters. Here are the docs for the browser-based version.
To run the included demos:
- clone repo
- install node deps
- 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".
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.
All of these awesome projects are built using the chatgpt
package. ๐คฏ
- Twitter Bot powered by ChatGPT โจ
- Mention @ChatGPTBot on Twitter with your prompt to try it out
- ChatGPT API Server - API server for this package with support for multiple OpenAI accounts, proxies, and load-balancing requests between accounts.
- Lovelines.xyz
- Chrome Extension (demo)
- VSCode Extension #1 (demo, updated version, marketplace)
- VSCode Extension #2 (marketplace)
- VSCode Extension #3 (marketplace)
- VSCode Extension #4 (marketplace)
- Raycast Extension #1 (demo)
- Raycast Extension #2
- Telegram Bot #1
- Telegram Bot #2
- Telegram Bot #3 (group privacy mode, ID-based auth)
- Telegram Bot #4 (queue system, ID-based chat thread)
- Deno Telegram Bot
- Go Telegram Bot
- Telegram Bot for YouTube Summaries
- GitHub ProBot
- Discord Bot #1
- Discord Bot #2
- Discord Bot #3
- Discord Bot #4 (selfbot)
- Discord Bot #5
- Discord Bot #6 (Shakespeare bot)
- WeChat Bot #1
- WeChat Bot #2
- WeChat Bot #3
- WeChat Bot #4
- WeChat Bot #5
- QQ Bot (plugin for Yunzai-bot)
- QQ Bot (plugin for KiviBot)
- QQ Bot (oicq)
- QQ Bot (oicq + RabbitMQ)
- QQ Bot (go-cqhttp)
- EXM smart contracts
- Flutter ChatGPT API
- Carik Bot
- Github Action for reviewing PRs
- WhatsApp Bot #1 (multi-user support)
- WhatsApp Bot #2
- WhatsApp Bot #3
- WhatsApp Bot #4 (schedule periodic messages)
- Matrix Bot
- Rental Cover Letter Generator
- Assistant CLI
- Teams Bot
- Askai
- TalkGPT
- ChatGPT With Voice
- iOS Shortcut
- Slack Bot #1
- Slack Bot #2 (with queueing mechanism)
- Electron Bot
- Kodyfire CLI
- Twitch Bot
- Continuous Conversation
- Figma plugin
- NestJS server
- NestJS ChatGPT Starter Boilerplate
- Wordsmith: Add-in for Microsoft Word
- QuizGPT: Create Kahoot quizzes with ChatGPT
If you create a cool integration, feel free to open a PR and add it to the list.
- 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 likeunfetch/polyfill
(guide)
- In
- If you want to build a website using
chatgpt
, we recommend using it only from your backend API
- Huge thanks to @waylaidwanderer, @abacaj, @wong2, @simon300000, @RomanHotsiy, @ElijahPepe, and all the other contributors ๐ช
- The original browser version was inspired by this Go module by Daniel Gross
- OpenAI for creating ChatGPT ๐ฅ
MIT ยฉ Travis Fischer
If you found this project interesting, please consider sponsoring me or following me on twitter