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-wrapper

v1.1.6

Published

NodeJS ChatGPT API wrapper

Downloads

15

Readme

Help

Rate me

ChatGPT-wrapper

ChatGPT API wrapper

Official docs - https://platform.openai.com/docs/api-reference/chat

Features

  • types included
  • docs are included
  • Stream included

Install

npm i chatgpt-wrapper

or

yarn add chatgpt-wrapper

Usage

Import

CommonJS

const { ChatGPT } = require('chatgpt-wrapper');

Modules

import { ChatGPT } from 'chatgpt-wrapper';

with Types

import { ChatGPT, Message, ReqBody, ResBody } from 'chatgpt-wrapper';

New instance

  • API_KEY (Required): Visit your API Keys page to retrieve the API key

  • ORG (Optional): For users who belong to multiple organizations, you can specify which organization is used for an API request. Usage from these API requests will count against the specified organization's subscription quota. Get Org ID here.

  • URL (Optional): API endpoint. Default set to 'Create chat completion' method.

  • MODEL (Optional): Model for requests, where not specified. Default is 'gpt-3.5-turbo'. Models list.

const chat = new ChatGPT({
  API_KEY: '...', // Your API KEY (Required)
  ORG: '...',     // Your organization (Optional)
  URL: '...',     // API endpoint (Optional)
  MODEL: '...',   // Custom default model (Optional)
});

Error Handling

Don't forget to catch errors from your requests since OpenAI API sometimes returns an error message instead of response.

"API error" errors returns APIError type.

async/await

try {
  const answer = await chat.send('question');
  // ...
} catch (err) {
  // handle error
}

Promise

chat.send('question')
  .then((answer) => { /* ... */ })
  .catch((err) => { /* handle error */ });

Methods

.send(content, [fetchOptions])

send(content: ReqBody | string, fetchOptions: RequestInit = {}): Promise<ResBody>

Use this method to send a request to ChatGPT API

Raw string equals to

{
  model: 'gpt-3.5-turbo',
  messages: [{
    role: 'user',
    content: 'YOUR STRING',
  }],
}

⚠️ To use stream option, use .stream() method! ⚠️

Examples:

const answer = await chat.send('what is JavaScript');

console.log(answer.choices[0].message);
chat.send('what is JavaScript').then((answer) => {
  console.log(answer.choices[0].message);
});
const answer = await chat.send({
  model: 'gpt-3.5-turbo-0301',
  messages: [{
    role: 'user',
    content: 'what is JavaScript',
  }],
  max_tokens: 200,
});

console.log(answer.choices[0].message);

.stream(content, [fetchOptions])

stream(content: ReqBody | string, fetchOptions: RequestInit = {}): Promise<ResBody>

Use this method to send a request to ChatGPT API and get steam response back

Raw string equals to

{
  model: 'gpt-3.5-turbo',
  stream: true,
  messages: [{
    role: 'user',
    content: 'YOUR STRING',
  }],
}

Examples:

(async () => {
  const answer = await chat.stream('what is JavaScript in 200 words');

  answer.pipe(process.stdout);
})();

How to implement "stop" command

Since you can pass options to fetch, you can abort request with AbortController. See fetch docs.

Example:

const controller = new AbortController();
const doStop = () => controller.abort();

// ...

const answer = await chat.stream('generate some long story', {
    signal: controller.signal,
});

answer.pipe(process.stdout);

Now, if you call doStop(), the controller will abort the request along with the stream.

Types

Message

Message in chat format

Source: index.ts#L4

FunctionModel

Function model description. See more

Source: index.ts#L46

ReqBody

Request body

Source: index.ts#L70

ResBody

Response body

Source: index.ts#L188

APIError

OpenAI API error

Source: index.ts#L263