Coder Social home page Coder Social logo

analogpvt / anapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from transitive-bullshit/agentic

0.0 0.0 0.0 2.16 MB

Node.js client for the unofficial ChatGPT API. ๐Ÿ”ฅ

License: MIT License

Shell 2.89% JavaScript 2.17% TypeScript 94.93%

anapi's Introduction

Update February 1, 2023

This package no longer requires any browser hacks โ€“ย it is now using the official OpenAI completions API with a leaked model that ChatGPT uses under the hood. ๐Ÿ”ฅ

import { ChatGPTAPI } from 'chatgpt'

const api = new ChatGPTAPI({
  apiKey: process.env.OPENAI_API_KEY
})

const res = await api.sendMessage('Hello World!')
console.log(res.text)

Please upgrade to chatgpt@latest (at least v4.0.0). The updated version is significantly more lightweight and robust compared with previous versions. You also don't have to worry about IP issues or rate limiting!

Huge shoutout to @waylaidwanderer for discovering the leaked chat model! ๐Ÿ’ช

If you run into any issues, we do have a pretty active Discord with a bunch of ChatGPT hackers from the Node.js & Python communities.

Lastly, please consider starring this repo and following me on twitter twitter to help support the project.

Thanks && cheers, Travis


Example usage

ChatGPT API

Node.js client for the unofficial ChatGPT API.

NPM Build Status MIT License Prettier Code Formatting

Intro

This package is a Node.js wrapper around ChatGPT by OpenAI. TS batteries included. โœจ

You can use it to start building projects powered by ChatGPT like chatbots, websites, etc...

Install

npm install chatgpt

Make sure you're using node >= 18 so fetch is available (or node >= 14 if you install a fetch polyfill).

Usage

Sign up for an OpenAI API key and store it in your environment.

import { ChatGPTAPI } from 'chatgpt'

async function example() {
  const api = new ChatGPTAPI({
    apiKey: process.env.OPENAI_API_KEY
  })

  const res = await api.sendMessage('Hello World!')
  console.log(res.text)
}

If you want to track the conversation, use the conversationId and id in the result object, and pass them to sendMessage as conversationId and parentMessageId respectively.

const api = new ChatGPTAPI({ apiKey: process.env.OPENAI_API_KEY })

// send a message and wait for the response
let res = await api.sendMessage('What is OpenAI?')
console.log(res.text)

// send a follow-up
res = await api.sendMessage('Can you expand on that?', {
  conversationId: res.conversationId,
  parentMessageId: res.id
})
console.log(res.text)

// send another follow-up
// send a follow-up
res = await api.sendMessage('What were we talking about?', {
  conversationId: res.conversationId,
  parentMessageId: res.id
})
console.log(res.text)

You can add streaming via the onProgress handler:

// timeout after 2 minutes (which will also abort the underlying HTTP request)
const res = await api.sendMessage('Write me a 500 word essay on frogs.', {
  onProgress: (partialResponse) => console.log(partialResponse)
})

You can add a timeout using the timeoutMs option:

// timeout after 2 minutes (which will also abort the underlying HTTP request)
const response = await api.sendMessage('this is a timeout test', {
  timeoutMs: 2 * 60 * 1000
})
Usage in CommonJS (Dynamic import)
async function example() {
  // To use ESM in CommonJS, you can use a dynamic import
  const { ChatGPTAPI } = await import('chatgpt')

  const api = new ChatGPTAPI({ apiKey: process.env.OPENAI_API_KEY })

  const res = await api.sendMessage('Hello World!')
  console.log(res.text)
}

Docs

See the auto-generated docs for more info on methods and parameters. Here are the docs for the browser-based version.

Demos

To run the included demos:

  1. clone repo
  2. install node deps
  3. set OPENAI_API_KEY in .env

A basic demo is included for testing purposes:

npx tsx demos/demo.ts

A demo showing on progress handler:

npx tsx demos/demo-on-progress.ts

The on progress demo uses the optional onProgress parameter to sendMessage to receive intermediary results as ChatGPT is "typing".

A conversation demo:

npx tsx demos/demo-conversation.ts

Lastly, a persitence demo shows how to store messages in Redis for persistence:

npx tsx demos/demo-conversation.ts

Any keyv adaptor is supported for persistence, and there are overrides if you'd like to use a different way of storing / retrieving messages.

Note that persisting message is very important for remembering the context of previous conversations.

Projects

All of these awesome projects are built using the chatgpt package. ๐Ÿคฏ

If you create a cool integration, feel free to open a PR and add it to the list.

Compatibility

  • This package is ESM-only.
  • This package supports node >= 14.
  • This module assumes that fetch is installed.
    • In node >= 18, it's installed by default.
    • In node < 18, you need to install a polyfill like unfetch/polyfill (guide)
  • If you want to build a website using chatgpt, we recommend using it only from your backend API

Credits

License

MIT ยฉ Travis Fischer

If you found this project interesting, please consider sponsoring me or following me on twitter twitter

anapi's People

Contributors

transitive-bullshit avatar skippyyy avatar optionsx avatar waylaidwanderer avatar simon300000 avatar danielehrhardt avatar kodjunkie avatar timmsgithub avatar easydu2002 avatar mrloldev avatar xtremehpx avatar oceanlvr avatar yunyu950908 avatar ikechan8370 avatar wong2 avatar timkmecl avatar leecobaby avatar barnesoir avatar fuergaosi233 avatar noelzappy avatar nucks avatar 0x7030676e31 avatar czzonet avatar zeke avatar yudax42 avatar leinstay avatar vikiboss avatar trietphm avatar thebrokenrail avatar thanhsonng avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.