chatnio-node
v1.1.9
Published
A forked Javascript/Typescript library for the Chat Nio API on Node.
Downloads
7
Readme
Chat Nio Javascript Library
- Authors: Deeptrain Team
- Free software: MIT license
- Documentation: https://docs.chatnio.net
Table of Contents
Features
Check official documentation website
- Chat
- Conversation
- Quota
- Subscription and Package
Dependencies
- axios (for http request)
- websockets(for chat stream)
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 filetest.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)
:warning: 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
}
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);
});
- Conversation API:Manage chatting of different topics in conversation
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);
- Quota API:Elastic billing, usage-based
import { getQuota, buyQuota } from 'chatnio-node';
// get quota
const quota = await getQuota();
// buy quota
const state = await buyQuota(100);
- Subscription and Package:Subscribe for different plans
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);
Example
Get Conversation Content
:bulb: For each new conversation, the first message received will be used as the conversation's name.
:warning: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
}
Build
Use tsc
to build
npm run build
# or
yarn run build
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.
See also
Official repository:Deeptrain-Community/chatnio
Other SDK: