0.0.8 • Published 2 months ago

@chaingpt/generalchat v0.0.8

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

ChainGPT General Chat SDK

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

Installation

npm install --save @chaingpt/generalchat
# or
yarn add generalchat

Usage

Retrieve a chat response as a stream:

import { GeneralChat } from '@chaingpt/generalchat';

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

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

main();

Retrieve a chat response as a blob:

import { GeneralChat } from '@chaingpt/generalchat';

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

async function main() {
  const response = await generalchat.createChatBlob({
    question: 'Explain quantum computing in simple terms',
    chatHistory: "on"
  })
  console.log(response.data.bot);
}

main();

Retrieve chat history:

import { GeneralChat } from '@chaingpt/generalchat';

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

async function main() {
  const response = await generalchat.getChatHistory({
    limit: 10,
    offset: 0,
    sortBy: "createdAt",
    sortOrder: "ASC"
  })
  console.log(response.data.rows);
}

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), and error of the class GeneralChatError will be thrown:

import { Errors } from '@chaingpt/generalchat';

async function main() {
  try {
    const stream = await generalchat.createChatStream({
      question: 'Explain quantum computing in simple terms',
      chatHistory: "off"
    });
    stream.on('data', (chunk: any)=>console.log(chunk.toString()));
    stream.on('end', () => console.log("Stream ended"));
  } catch(error) {
    if(error instanceof Errors.GeneralChatError) {
      console.log(error.message)
    }
  }
}

main();
0.0.8

2 months ago

0.0.7

4 months ago

0.0.5

4 months ago

0.0.6

4 months ago

0.0.4

5 months ago

0.0.3

5 months ago