Socket
Socket
Sign inDemoInstall

@chaingpt/openaichat

Package Overview
Dependencies
11
Maintainers
3
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @chaingpt/openaichat

SDK for OpenAIChat


Version published
Maintainers
3
Created

Readme

Source

ChainGPT OpenAI Chat SDK

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

Installation

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

Usage

Retrieve a chat response as a stream:

import { OpenAIChat } from '@chaingpt/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',
    model: "general_assistant",
    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 { OpenAIChat } from '@chaingpt/openaichat';

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

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

main();

Retrieve chat history:

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

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

async function main() {
  const response = await openaichat.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 OpenAIChatError will be thrown:

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

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

main();

Keywords

FAQs

Last updated on 19 Dec 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc