Paste the generated API key in the placeholder “Your ChainGPT API key”.
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();
3. SDK Components
3.1 Core Libraries
Our General Chatbot SDK offers TypeScript/JavaScript libraries compatible with Node.js. To install:
Run the installation command.
npm install --save @chaingpt/generalchat
# or
yarn add generalchat
Use the library along with your secret key to execute further operations.
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();
3.2 Advanced Features
ChainGPT General Chatbot SDK provides the following features:
Stream Response
Functionality: 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();
Credit Deduction: Each chat API request deducts 1 credit from the user's account in the WebApp. An additional credit is deducted if the chat history feature is enabled.
Blob Response
Functionality: Retrieve the chat response in the form of 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 termsQQQ',
chatHistory: "on"
})
console.log(response.data.bot);
}
main();
Credit Deduction: Same as Stream Response.
Get Chat History
Functionality: The SDK stores chat history for later use, retrievable by specific code.
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();
Credit Deduction: No additional credits are deducted if the chat history feature was enabled during Stream/Blob response API calls.
3.3 Error Handling
If there is a failure in connecting to the API or if the API returns a non-success status code (4xx or 5xx), a GeneralChatError class error will be thrown.