chatgpt-lib

0.3.0 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i chatgpt-lib

Weekly Downloads

3

Version

0.3.0

License

MIT

Unpacked Size

9.27 kB

Total Files

5

Last publish

Collaborators

  • mixanik