API Key Usage
To use the API key:
Execute the specified commands.
Paste the generated API key in the placeholder “Your ChainGPT API key”.
Copy 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.
Copy npm install --save @chaingpt/generalchat
# or
yarn add generalchat
Use the library along with your secret key to execute further operations.
Copy 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.
Copy 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.
Copy 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.
Copy 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.
Copy 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 ();
3.4 Language/Framework Compatibility
Our SDK is compatible with JavaScript and is designed for Node.js applications.
3.5 Security Considerations
Authentication : Access to the SDK is secured via an authentication key.
Credits System : Users with credits and a valid API key can access the SDK.
Request Limitation : To prevent misuse, 200 requests per minute are limited, with 1 credit deducted per request.
3.6 Release Version
Release History : We maintain a detailed release history.
First Release : This is the initial release, with future updates for additional features.
3.7 Code Documentation
For detailed SDK code documentation, visit npm package documentation .
3.8 Support/Help
For additional assistance, refer to Discord - https://discord.gg/chaingpt
Disclaimer