⚠️ Archived on 19-12-2022 (but why?) ⚠️
Simple javascript wrapper for ChatGPT's unofficial web API
npm i chatgpt-lib
- 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
Application
tab - find
Cookies
inStorage
section, open them up and click onhttps://chat.openai.com
- find cookie with name
__Secure-next-auth.session-token
and copy its value
- Create file
config.json
and paste your session token asSessionToken
key 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
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 |
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