Skip to content

peachest/chatnio-api-node

 
 

Repository files navigation

Chat Nio Javascript Library

NPM VersionNPM DownloadsGitHub last commit (branch)GitHub License

A forked Javascript/Typescript library for the Chat Nio API on Node.

English · 简体中文

Quick Start · Report Bug · Propose Feature

Official Web APP · Documentation

Stat

Table of Contents

Features

Check official documentation website

  • Chat
  • Conversation
  • Quota
  • Subscription and Package

Dependencies

[↑ back to top]

Installation

npm install chatnio-node
# or using yarn, pnpm
yarn add chatnio-node

Quick Start

  • Sign up and sign in official Chatnio website
  • Click the avatar at the top right corner
  • Select 'API Settings'
  • Copy your private API Key
  • Install chatnio-node and write the source file test.js
// test.mjs
import {setKey, Chat} from "chatnio-node"

// Place your API Key here. Don't do this in code that will be published to public
setKey("{{ API Key }}")
const chat = new Chat()
const res = await chat.ask({
    message: "hello! What's your name?"
})
console.log(res)
process.exit(0)

⚠️ For security, you should not place your API Key directly in your source code, but use envrionment variables or .env file in your app.

  • Execute the program
node test.mjs
  • The output result may look like the following:
{
  message: "Hello! I am OpenAI's language model, known as GPT-3. I don't have a specific name, but you can call me GPT-3 or AI. How can I assist you today?",
  keyword: '',
  quota: 0.000255
}

[↑ back to top]

Usage

  • Import
import { Chat } from 'chatnio-node';
// or
const { Chat } = require("chatnio-node")

[↑ 回到顶部]

API

  • Authentication API:Set your own API Key before chatting
import { setKey, setEndpoint } from 'chatnio-node';

// set your API Key
setKey("sk-...");

// set custom api endpoint (default: https://api.chatnio.net)
// setEndpoint("https://example.com/api");
  • Chat API:Chat with multiple available models
import { Chat } from 'chatnio-node';

const chat = new Chat(-1); // id -1 (default): create new conversation

// using stream
chat.askStream({ 
    message: "hello world", 
    model: "gpt-3.5-turbo-0613", // default gpt-3
    web: false 
}, (res) => {
  console.log(res);
});

// don't use stream
chat.ask({ message: "hi" })
  .then(res => {
    console.log(res);
  })
  .catch(err => {
    console.log(err);
  });
import { getConversations, getConversation, deleteConversation } from 'chatnio-node';

// get all conversations of current user
const conversations = await getConversations();

// load conversation by id
const conversation = await getConversation(1);

// delete conversation by id
const state = await deleteConversation(1);
import { getQuota, buyQuota } from 'chatnio-node';

// get quota
const quota = await getQuota();

// buy quota
const state = await buyQuota(100);
import { getPackage, getSubscription, buySubscription } from 'chatnio-node';

// get package
const pkg = await getPackage();

// get subscription
const subscription = await getSubscription();

// buy subscription
const state = await buySubscription(1, 1);

[↑ back to top]

Example

Get Conversation Content

💡 For each new conversation, the first message received will be used as the conversation's name.

⚠️Although the getConversations() interface returns a data type of Conversation[], the message field of each Conversation is null . To obtain the complete history of a conversation, load the conversation by getConversation(id) interface.

import {Chat, getConversation, getConversations, setKey} from 'chatnio-node'

setKey("")

const msg1 = "Hello! My name is chat-nio."
const msg2 = "What's my name?"
// Create a new conversation. The first message received will be used as the conversation's name.
const chat = new Chat(-1)
let res = await chat.ask({
    message: msg1
})
console.log(res);
res = await chat.ask({
    message: msg2
})
console.log(res);

// Get the list of recent conversations, use the name to filter out conversation, and find the ID
const conversations = await getConversations();
const id = conversations.find(conv => conv.name === msg1).id

// Retrieve detailed conversation information by id
const conversation = await getConversation(id)
console.log(conversation);

// exit the program
process.exit(0)

Output may look like:

{
  message: 'Hello chat-nio! How can I assist you today?',
  keyword: '',
  quota: 0.00156
}
{ message: 'Your name is chat-nio.', keyword: '', quota: 0.002385 }
{
  auth: false,
  user_id: 4470,
  id: 45,
  name: 'Hello! My name is chat-nio.',
  message: [
    { srole: 'user', content: 'Hello! My name is chat-nio.' },
    { role: 'assistant', content: 'Hello chat-nio! How can I assist you today?' },
    { role: 'user', content: "What's my name?" },
    { role: 'assistant', content: 'Your name is chat-nio.' }
  ],
  model: 'gpt-3.5-turbo',
  enable_web: false,
  shared: false,
  context: 0
}

[↑ back to top]

Build

Use tsc to build

npm run build
# or
yarn run build

[↑ back to top]

FAQs

Where to find API Key

  • Sign in official Chatnio website
  • Click the avatar at the top right corner
  • Select 'API Settings' .
  • Copy your private API Key

Check API 快速入门 - Chat Nio for detailed

Where to find available models

Default is gpt-3.5-turbo if you don’t specified.

Check api.chatnio.net/v1/models for all available models.

[↑ back to top]

See also

Official repository:Deeptrain-Community/chatnio

Other SDK:

[↑ back to top]

About

Node Javascript / Typescript library for the Chat Nio API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • JavaScript 54.9%
  • TypeScript 45.1%