npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

chatgpt-api-clone

v1.5.11

Published

A ChatGPT implementation using the official ChatGPT model via OpenAI's API.

Downloads

2

Readme

ChatGPT API

A ChatGPT implementation using the official ChatGPT model via OpenAI's API.

NPM npm MIT License GitHub Repo stars

This is an implementation of ChatGPT using the official ChatGPT raw model, text-chat-davinci-002-20230126. This model name was briefly leaked while I was inspecting the network requests made by the official ChatGPT website, and I discovered that it works with the OpenAI API. Usage of this model currently does not cost any credits.

As far as I'm aware, I was the first one who discovered this, and usage of the model has since been implemented in libraries like acheong08/ChatGPT.

The previous version of this library that used transitive-bullshit/chatgpt-api is still available on the archive/old-version branch.

By itself, the model does not have any conversational support, so this library uses a cache to store conversations and pass them to the model as context. This allows you to have persistent conversations with ChatGPT in a nearly identical way to the official website.

Features

  • Uses the official ChatGPT raw model, text-chat-davinci-002-20230126.
  • Includes an API server you can run to use ChatGPT in non-Node.js applications.
  • Includes a ChatGPTClient class that you can use in your own Node.js applications.
  • Includes a CLI interface where you can chat with ChatGPT.
  • Replicates chat threads from the official ChatGPT website (with conversation IDs and message IDs), with persistent conversations using Keyv.
    • Conversations are stored in memory by default, but you can optionally install a storage adapter to persist conversations to a database.
    • The keyv-file adapter is also included in this package, and can be used to store conversations in a JSON file if you're using the API server or CLI (see settings.example.js).
  • Supports configurable prompt prefixes, and custom names for the user and ChatGPT.
    • In essence, this allows you to turn ChatGPT into a different character.
    • This is currently only configurable on a global level, but I plan to add support for per-conversation customization.

Getting Started

Prerequisites

Usage

Module

npm i @waylaidwanderer/chatgpt-api
import ChatGPTClient from '@waylaidwanderer/chatgpt-api';

const clientOptions = {
  // (Optional) Parameters as described in https://platform.openai.com/docs/api-reference/completions
  modelOptions: {
    // The model is set to text-chat-davinci-002-20230126 by default, but you can override
    // it and any other parameters here
    model: 'text-chat-davinci-002-20230126',
  },
  // (Optional) Set a custom prompt prefix. As per my testing it should work with two newlines
  // promptPrefix: 'You are not ChatGPT...\n\n',
  // (Optional) Set a custom name for the user
  // userLabel: 'User',
  // (Optional) Set a custom name for ChatGPT
  // chatGptLabel: 'ChatGPT',
  // (Optional) Set to true to enable `console.debug()` logging
  debug: false,
};

const cacheOptions = {
  // Options for the Keyv cache, see https://www.npmjs.com/package/keyv
  // This is used for storing conversations, and supports additional drivers (conversations are stored in memory by default)
  // For example, to use a JSON file (`npm i keyv-file`) as a database:
  // store: new KeyvFile({ filename: 'cache.json' }),
};

const chatGptClient = new ChatGPTClient('OPENAI_API_KEY', clientOptions, cacheOptions);

const response = await chatGptClient.sendMessage('Hello!');
console.log(response); // { response: 'Hi! How can I help you today?', conversationId: '...', messageId: '...' }

const response2 = await chatGptClient.sendMessage('Write a poem about cats.', { conversationId: response.conversationId, parentMessageId: response.messageId });
console.log(response2.response); // Cats are the best pets in the world.

const response3 = await chatGptClient.sendMessage('Now write it in French.', { conversationId: response2.conversationId, parentMessageId: response2.messageId });
console.log(response3.response); // Les chats sont les meilleurs animaux de compagnie du monde.

API Server

You can install the package using

npm i -g @waylaidwanderer/chatgpt-api

then run it using chatgpt-api.
This takes an optional --settings=<path_to_settings.js> parameter, or looks for settings.js in the current directory if not set, with the following contents:

module.exports = {
  // Your OpenAI API key
  openaiApiKey: '',
  chatGptClient: {
    // (Optional) Parameters as described in https://platform.openai.com/docs/api-reference/completions
    modelOptions: {
      // The model is set to text-chat-davinci-002-20230126 by default, but you can override
      // it and any other parameters here
      model: 'text-chat-davinci-002-20230126',
    },
    // (Optional) Set a custom prompt prefix. As per my testing it should work with two newlines
    // promptPrefix: 'You are not ChatGPT...\n\n',
    // (Optional) Set a custom name for the user
    // userLabel: 'User',
    // (Optional) Set a custom name for ChatGPT
    // chatGptLabel: 'ChatGPT',
    // (Optional) Set to true to enable `console.debug()` logging
    debug: false,
  },
  // Options for the Keyv cache, see https://www.npmjs.com/package/keyv
  // This is used for storing conversations, and supports additional drivers (conversations are stored in memory by default)
  cacheOptions: {},
  // The port the server will run on (optional, defaults to 3000)
  port: 3000,
  // If set, ChatGPTClient will use `keyv-file` to store conversations to this JSON file instead of in memory.
  // `cacheOptions.store` will override this if set
  storageFilePath: './cache.json',
};

Alternatively, you can install and run the package locally:

  1. Clone this repository
  2. Install dependencies with npm install
  3. Rename settings.example.js to settings.js in the root directory and change the settings where required.
  4. Start the server using npm start or npm run server

To start a conversation with ChatGPT, send a POST request to the server's /conversation endpoint with a JSON body in the following format:

{
    "message": "Hello, how are you today?",
    "conversationId": "your-conversation-id (optional)",
    "parentMessageId": "your-parent-message-id (optional)"
}

The server will return a JSON object containing ChatGPT's response:

{
    "response": "I'm doing well, thank you! How are you?",
    "conversationId": "your-conversation-id",
    "messageId": "response-message-id"
}

If the request is unsuccessful, the server will return a JSON object with an error message and a status code of 503.

If there was an error sending the message to ChatGPT:

{
    "error": "There was an error communicating with ChatGPT."
}

CLI

Install the package using the same instructions as the API server.

If installed globally:

chatgpt-cli

If installed locally:

npm run cli

ChatGPT's responses are automatically copied to your clipboard, so you can paste them into other applications.

Caveats

Since text-chat-davinci-002-20230126 is ChatGPT's raw model, I had to do my best to replicate the way the official ChatGPT website uses it. After extensive testing and comparing responses, I believe that the model used by ChatGPT has some additional fine-tuning.
This means my implementation or the raw model may not behave exactly the same in some ways:

  • Conversations are not tied to any user IDs, so if that's important to you, you should implement your own user ID system.

  • ChatGPT's model parameters (temperature, frequency penalty, etc.) are unknown, so I set some defaults that I thought would be reasonable.

  • Conversations are limited to roughly the last 3000 tokens, so earlier messages may be forgotten during longer conversations.

    • This works in a similar way to ChatGPT, except I'm pretty sure they have some additional way of retrieving context from earlier messages when needed (which can probably be achieved with embeddings, but I consider that out-of-scope for now).
  • It is well known that, as part of the fine-tuning, ChatGPT had the following preamble:

    "You are ChatGPT, a large language model trained by OpenAI. You answer as concisely as possible for each response (e.g. don’t be verbose). It is very important that you answer as concisely as possible, so please remember this. If you are generating a list, do not have too many items. Keep the number of items short.
    Knowledge cutoff: 2021-09
    Current date: 2023-01-31"

    As OpenAI updates ChatGPT, this preamble may also change. The default prompt prefix in my implementation attempts to replicate a similar behavior to the current ChatGPT model.

Contributing

If you'd like to contribute to this project, please create a pull request with a detailed description of your changes.

License

This project is licensed under the MIT License.