0.3.0 • Published 3 years ago
chatgpt-lib v0.3.0
chatgpt-lib
Simple javascript wrapper for ChatGPT's unofficial web API
Installation
npm i chatgpt-lib
Setup & Usage
- You will need session token for this to work.
To obtain it:- go to https://chat.openai.com/chat and login
- press F12 to open browser devtools and go to
Applicationtab - find
CookiesinStoragesection, open them up and click onhttps://chat.openai.com - find cookie with name
__Secure-next-auth.session-tokenand copy its value
- Create file
config.jsonand paste your session token asSessionTokenkey value:
{
"SessionToken": "<insert-your-session-token-here>"
}- Here's minimal code for prompting a question to chatGPT
const cgpt = require('chatgpt-lib');
const config = require('./config');
...
const chatbot = new cgpt.ChatGPT(config);
let answer = await chatbot.ask("Hey, how are you doing today?");
console.log(answer);
answer = await chatbot.ask("Can you explain to me how quantum superposition works?");
console.log(answer);You can also start conversation just like on the ChatGPT's web page, but in CLI mode:
// index.js contents
const cgpt = require('chatgpt-lib');
const config = require('./config');
const chatbot = new cgpt.ChatGPT(config);
chatbot.initCliConversation();Then just run in like node index.js

Docs
| Class | Method | Params | Description |
|---|---|---|---|
| ChatGPT | ask(prompt) | prompt : text to send | Prompts ChatGPT with given text |
| ChatGPT | resetThread() | - | Resets conversation with ChatGPT |
| ChatGPT | validateToken(token) | token : token to validate | Checks if jwt has expired |
| ChatGPT | getTokens() | - | Fetches auth and session tokens |
| ChatGPT | initCliConversation() | - | Starts conversation in CLI mode |
Credits
Inspired by python version - https://github.com/acheong08/ChatGPT
ChatGPT for creating model - https://chat.openai.com/chat
funny fact, initial code was written by giving and asking ChatGPT to rewrite python version in javascript