Code Monkey home page Code Monkey logo

chatgpt-wrapper's Introduction

Help

Rate me

ChatGPT-wrapper

ChatGPT API wrapper

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

Features

  • types included
  • docs 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

Visit your API Keys page to retrieve the API key

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

Error Handling

Do not forget to catch errors from your requests since OpenAI API sometimes returns 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: ReqBody | string): Promise<ResBody>

Use this method to send 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: ReqBody | string): Promise<NodeJS.ReadableStream>

Use this method to send 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);
})();

Types

Message

Message in chat format

Source: index.ts#L4

ReqBody

Request body

Source: index.ts#L21

ResBody

Response body

Source: index.ts#L120

APIError

OpenAI API error

Source: index.ts#L195

chatgpt-wrapper's People

Contributors

debanjandhar12 avatar nachasic avatar tabmk 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.