@shahroze/openaichat
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

ChainGPT OpenAI Chat SDK

This library provides convenient access to the ChainGPT OpenAI Chat REST API from TypeScript or JavaScript.

Installation

npm install --save @shahroze/openaichat
# or
yarn add openaichat

Usage

import { OpenAIChat } from '@shahroze/openaichat';

const openaichat = new OpenAIChat({
  apiKey: 'Your ChainGPT API Key',
});

async function main() {
  const stream = await openaichat.createChatStream({
    question: 'Explain quantum computing in simple terms'
  });
  stream.on('data', (chunk: any)=>console.log(chunk.toString()););
  stream.on('end', ()=>console.log("Stream ended"););
}

main();

Handling errors

When the library is unable to connect to the API, or if the API returns a non-success status code (i.e., 4xx or 5xx response), a subclass of OpenAIChatError will be thrown:

import { Errors } from '@shahroze/openaichat';

async function main() {
  openaichat.createChatStream({
    question: 'Explain quantum computing in simple terms'
  })
  .then((res)=>{})
  .catch((err)=>{
      if (err instanceof Errors.OpenAIChatError) {
        if (err instanceof Errors.InvalidApiKeyError) {
            console.log("Invalid Api Key Error")
        } else if (err instanceof Errors.RateLimitExceededError) {
            console.log("Rate limit exceeded Error")
        }
      }
  });
}

main();

Error codes are as followed:

Status Code Error Type
403 InvalidApiKeyError
429 RateLimitExceededError
N/A OpenAIChatError

Package Sidebar

Install

npm i @shahroze/openaichat

Weekly Downloads

0

Version

0.0.2

License

MIT

Unpacked Size

22.8 kB

Total Files

39

Last publish

Collaborators

  • shahroze