Skip to content
This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
/ chatgpt-lib Public archive

Simple javascript wrapper for ChatGPT's unofficial web API

License

Notifications You must be signed in to change notification settings

Mix-Anik/chatgpt-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

⚠️ Archived on 19-12-2022 (but why?) ⚠️

chatgpt-lib

npm npm GitHub

Simple javascript wrapper for ChatGPT's unofficial web API

Installation

npm i chatgpt-lib

Setup & Usage

  1. 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 in Storage section, open them up and click on https://chat.openai.com
    • find cookie with name __Secure-next-auth.session-token and copy its value
  2. Create file config.json and paste your session token as SessionToken key value:
{
  "SessionToken": "<insert-your-session-token-here>"
}
  1. 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

image

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