@chaingpt/generalchat
v0.0.17
Published
SDK for GeneralChat
Downloads
404
Readme
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 generalchatStream Response
Create Your API Key
- Obtain your API key from AI Hub to access the SDK.
Case 1: Simple General Chat
Retrieve a chat response as a stream without custom context injection:
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", // Set to "on" to save chat history; otherwise, keep it "off"
});
stream.on('data', (chunk: any)=>console.log(chunk.toString()));
stream.on('end', () => console.log("Stream ended"));
}
main();Case 2: Use Custom Context From AI Hub
Step 1: Create Your API Key
- Obtain your API key from AI Hub to access the SDK.
Step 2: Add Company Details from AI Hub User Side
- Define your company details, including name, description, website, whitepaper, and chatbot purpose in apiKey (additional information).
Step 3: set Custom Context true (Additional information fetched from AI Hub)
import { GeneralChat } from '@chaingpt/generalchat';
import { AI_TONE, BLOCKCHAIN_NETWORK, PRE_SET_TONES } from "@chaingpt/generalchat/dist/enum/context.enum.js";
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", // Set to "on" to save chat history; otherwise, keep it "off"
useCustomContext: true // When useCustomContext is true and no contextInjection object is provided, the default custom context is fetched based on the API key (additional information).
});
stream.on('data', (chunk: any)=>console.log(chunk.toString()));
stream.on('end', () => console.log("Stream ended"));
}
main()Case 3: Use Custom Context From SDK
🎯 Context Injection (Optional)
- The
contextInjectionobject is optional, and all its fields are optional. You can provide additional company details for a more tailored chatbot experience . Any chunk of information added here will override the company default details that are added from AI Hub
Context Injection Details Options
const contextInjection = {
companyName: "Company name for AI chatbot",
companyDescription: "Company description for AI chatbot",
companyWebsiteUrl: "https://www.example.com",
whitePaperUrl: "https://www.example.com",
purpose: "Purpose of AI Chatbot for this company.",
cryptoToken: true,
tokenInformation: {
tokenName: "Token name for AI chatbot",
tokenSymbol: "$TOKEN",
tokenAddress: "Token address for AI chatbot",
tokenSourceCode: "Token source code for AI chatbot",
tokenAuditUrl: "https://www.example.com",
exploreUrl: "https://www.example.com",
cmcUrl: "https://www.example.com",
coingeckoUrl: "https://www.example.com",
blockchain: [BLOCKCHAIN_NETWORK.ETHEREUM, BLOCKCHAIN_NETWORK.POLYGON_ZKEVM]
},
socialMediaUrls: [{ name: "LinkedIn", url: "https://abc.com" }],
limitation: false,
aiTone: AI_TONE.PRE_SET_TONE,
selectedTone: PRE_SET_TONES.FRIENDLY,
customTone: "Only for custom AI Tone"
};🛠 Example Usage
- Example 1: Using minimal context injection
import { GeneralChat } from '@chaingpt/generalchat';
import { AI_TONE, BLOCKCHAIN_NETWORK, PRE_SET_TONES } from "@chaingpt/generalchat/dist/enum/context.enum.js";
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",
useCustomContext: true,
contextInjection: {
aiTone: AI_TONE.PRE_SET_TONE,
selectedTone: PRE_SET_TONES.FORMAL,
}
});
stream.on('data', (chunk: any) => console.log(chunk.toString()));
stream.on('end', () => console.log("Stream ended"));
}
main();- Example 2: Using extended context injection
import { GeneralChat } from '@chaingpt/generalchat';
import { AI_TONE, BLOCKCHAIN_NETWORK, PRE_SET_TONES } from "@chaingpt/generalchat/dist/enum/context.enum.js";
const generalchat = new GeneralChat({
apiKey: 'Your ChainGPT API Key',
});
async function main() {
const stream = await generalchat.createChatStream({
question: 'Explain blockchain security best practices',
chatHistory: "on",
useCustomContext: true,
contextInjection: {
companyName: "Crypto Solutions",
companyDescription: "A blockchain security firm",
cryptoToken: true,
tokenInformation: {
tokenName: "SecureToken",
tokenSymbol: "$SEC",
blockchain: [BLOCKCHAIN_NETWORK.ETHEREUM]
},
socialMediaUrls: [{ name: "Twitter", url: "https://twitter.com/cryptosolutions" }],
aiTone: AI_TONE.CUSTOM_TONE,
customTone: "Provide detailed security explanations."
}
});
stream.on('data', (chunk: any) => console.log(chunk.toString()));
stream.on('end', () => console.log("Stream ended"));
}
main();Blob Response
Create Your API Key
- Obtain your API key from AI Hub to access the SDK.
Case 1: Simple General Chat
Usage
Retrieve a chat response as a blob without custom context injection:
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();Case 2: Use Custom Context From AI Hub - Blob Response
Step 1: Create Your API Key
- Obtain your API key from AI Hub to access the SDK.
Step 2: Add Company Details from AI Hub User Side
- Define your company details, including name, description, website, whitepaper, and chatbot purpose in apiKey (additional information).
Step 3: set Custom Context true (Additional information fetched from AI Hub)
import { GeneralChat } from '@chaingpt/generalchat';
import { AI_TONE, BLOCKCHAIN_NETWORK, PRE_SET_TONES } from "@chaingpt/generalchat/dist/enum/context.enum.js";
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: "off",
useCustomContext: true // When useCustomContext is true and no contextInjection object is provided, the default custom context is fetched based on the API key (additional information).
});
console.log(response.data.bot);
}
main()Case 3: Use Custom Context From SDK - Blob Response
🎯 Context Injection (Optional)
- The
contextInjectionobject is optional, and all its fields are optional. You can provide additional company details for a more tailored chatbot experience . Any chunck of information Added here will override the your company default details that are added from AI Hub
Context Injection Options
const contextInjection = {
companyName: "Company name for AI chatbot",
companyDescription: "Company description for AI chatbot",
companyWebsiteUrl: "https://www.example.com",
whitePaperUrl: "https://www.example.com",
purpose: "Purpose of AI Chatbot for this company.",
cryptoToken: true,
tokenInformation: {
tokenName: "Token name for AI chatbot",
tokenSymbol: "$TOKEN",
tokenAddress: "Token address for AI chatbot",
tokenSourceCode: "Token source code for AI chatbot",
tokenAuditUrl: "https://www.example.com",
exploreUrl: "https://www.example.com",
cmcUrl: "https://www.example.com",
coingeckoUrl: "https://www.example.com",
blockchain: [BLOCKCHAIN_NETWORK.ETHEREUM, BLOCKCHAIN_NETWORK.POLYGON_ZKEVM]
},
socialMediaUrls: [{ name: "LinkedIn", url: "https://abc.com" }],
limitation: false,
aiTone: AI_TONE.PRE_SET_TONE,
selectedTone: PRE_SET_TONES.FRIENDLY,
customTone: "Only for custom AI Tone"
};🛠 Example Usage
- Example 1: Using minimal context injection
import { GeneralChat } from '@chaingpt/generalchat';
import { AI_TONE, BLOCKCHAIN_NETWORK, PRE_SET_TONES } from "@chaingpt/generalchat/dist/enum/context.enum.js";
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: "off",
useCustomContext: true,
contextInjection: {
aiTone: AI_TONE.PRE_SET_TONE,
selectedTone: PRE_SET_TONES.FORMAL,
}
});
console.log(response.data.bot);
}
main();- Example 2: Using extended context injection
import { GeneralChat } from '@chaingpt/generalchat';
import { AI_TONE, BLOCKCHAIN_NETWORK, PRE_SET_TONES } from "@chaingpt/generalchat/dist/enum/context.enum.js";
const generalchat = new GeneralChat({
apiKey: 'Your ChainGPT API Key',
});
async function main() {
const response = await generalchat.createChatBlob({
question: 'Explain blockchain security best practices',
chatHistory: "on",
useCustomContext: true,
contextInjection: {
companyName: "Crypto Solutions",
companyDescription: "A blockchain security firm",
cryptoToken: true,
tokenInformation: {
tokenName: "SecureToken",
tokenSymbol: "$SEC",
blockchain: [BLOCKCHAIN_NETWORK.ETHEREUM]
},
socialMediaUrls: [{ name: "Twitter", url: "https://twitter.com/cryptosolutions" }],
aiTone: AI_TONE.CUSTOM_TONE,
customTone: "Provide detailed security explanations."
}
});
console.log(response.data.bot);
}
main();Inputs
blockchain types
We support multiple blockchains to fetch and display crypto token information for chat interactions. Below is the list of supported blockchains:
blockchain = [ ETHEREUM, BSC, ARBITRUM, BASE, BLAST, AVALANCHE, POLYGON, SCROLL, OPTIMISM, LINEA, ZKSYNC, POLYGON_ZKEV, GNOSIS, FANTOM, MOONRIVER, MOONBEAM, BOBA, MODE, METIS, LISK, AURORA, SEI, IMMUTABLE_ZK, GRAVITY, TAIKO, CRONOS, FRAXTAL, ABSTRACT, CELO, WORLD_CHAIN, MANTLE, BERACHAIN
]Ai Tones Option:
Our AI supports multiple tone options to customize responses based on user preferences. The following tone selection options are available:
1) DEFAULT_TONE // if we select DEFAULT_TONE then no need for customTone and selectedTone
2) CUSTOM_TONE // if we select CUSTOM_TONE then customTone text is required
3) PRE_SET_TONE // if we select PRE_SET_TONE then selectedTone is requiredPreset Ai Tones:
If PRE_SET_TONE is selected, users can choose from the following tones: If we select PRE_SET_TONE then we select given option
1) PROFESSIONAL
2) FRIENDLY
3) INFORMATIVE
4) FORMAL
5) CONVERSATIONAL
6) AUTHORITATIVE
7) PLAYFUL
8) INSPIRATIONAL
9) CONCISE
10) EMPATHETIC
11) ACADEMIC
12) NEUTRAL
13) SARCASTIC_MEME_STYLE Social Media Option:
We support multiple social media platforms for interaction and sharing
socialMediaUrls = [
{name:"linkedIn",url:"https://www.example.com"},
{name:"telegram",url:"https://www.example.com"},
{name:"instagram",url:"https://www.example.com"},
{name:"twitter",url:"https://www.example.com"},
{name:"youtube",url:"https://www.example.com"},
{name:"medium",url:"https://www.example.com"}
]Manage Chat History :
- If you want to save the user-specific history, you must pass the
sdkUniqueIdparameter along with the chat history. This applies to both streaming and blob cases.
import { GeneralChat } from '@chaingpt/generalchat';
const generalchat = new GeneralChat({
apiKey: 'Your ChainGPT API Key',
});
const uniqueUUid="907208eb-0929-42c3-a372-c21934fbf44f" // any unique uuid
async function main() {
const stream = await generalchat.createChatStream({
question: 'Explain quantum computing in simple terms',
chatHistory: "on", // Set to "on" to save chat history; otherwise, keep it "off"
sdkUniqueId:uniqueUUid
});
stream.on('data', (chunk: any)=>console.log(chunk.toString()));
stream.on('end', () => console.log("Stream ended"));
}
main();
Retrieve chat history:
- Retrieve all 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();- Retrieve all chat history of a Specific User
import { GeneralChat } from '@chaingpt/generalchat';
const generalchat = new GeneralChat({
apiKey: 'Your ChainGPT API Key',
});
const uniqueUUid="907208eb-0929-42c3-a372-c21934fbf44f" // your unique uuid of your specific user
async function main() {
const response = await generalchat.getChatHistory({
limit: 10,
offset: 0,
sortBy: "createdAt",
sortOrder: "ASC",
sdkUniqueId:uniqueUUid // your unique uuid of your specific user
})
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();