class AiChat
makes requests to api.shockbs.is-a.dev/v1/ai/chat
and get the AI model's response, chatCompletion, allows switching models and clearing conversation history by clicking buttons.
this does no cover the full version of /v1/ai read more
Constructor
Options
model
(string): The AI model to use (e.g., "gpt-4", "gpt-3.5", gpt-3"). This field is required and case-insensitive.replyMention
(boolean): Whether the bot should mention the user in its replies. Default isfalse
.maxInteractions
(number): Maximum number of interactions allowed in a single conversation. Must be between 2 and 30. This field is required.components
(array): An array of components to include in the bot's responses. Default isnull
.custom
(string): System instructions for the AI to follow while generating responses. Must be a string with a maximum length of 1500 characters. Default isnull
.blacklistedUsers
(array): An array containing strings of blacklisted user id(s).embed
(object): An object to configure the embed appearance in responses.color
(string): The color of the embed. Default is"#34FFC2"
.
dashboard
(object): Configuration for the dashboard shown from the buttons of the response message.enabled
(boolean): whether to show or not. required value.buttonStyle
(number): style of the button.buttonText
(string): label of the button.clearConversationOnSwitchModel
(boolean): whether to clear conversation history after the user switched models or not.
New Update: the GPT-4
from this service is no longer pure! It is highly recommended to pick GPT-3.5
Methods
clear()
Clears the entire cache for conversation history.
Returns
Promise<void>
clearConversation(id)
Clears the conversation history for a specific user.
Parameters
id
(string): The user's ID.
Returns
Promise<boolean>
Throws
ReferenceError
: Ifid
is not provided.TypeError
: Ifid
is not a string.Error
: If the conversation with the givenid
is not found.
getData(id)
Retrieves the conversation data for a specific user.
Parameters
id
(string): The user's ID.
Returns
Promise<object>
Throws
ReferenceError
: Ifid
is not provided.TypeError
: Ifid
is not a string.Error
: If the conversation with the givenid
is not found.
getCount(id)
Gets the number of interactions for a specific user.
Parameters
id
(string): The user's ID.
Returns
Promise<number>
Throws
ReferenceError
: Ifid
is not provided.TypeError
: Ifid
is not a string.Error
: If the conversation with the givenid
is not found.
handleMessage(message)
Handles a new message from the messageCreate event and sends a reply with the AI model.
handleInteraction(interaction)
Handles a new interaction and filters out those that came from the AI dashboard and the buttons commint from response messages.
Parameters
message
(object): The message object.
Returns
Promise<void>
Example
first initialize gptChat as a global variable
import shockbs from "shockbs";
// const shockbs = require("shockbs");
const chatData = new shockbs.AiChat({
model: "GPT-4",
// replyMention: false,
maxInteractions: 30,
// components: null,
// custom: null,
// blacklistedUsers: [],
dashboard: {
enabled: true,
// buttonStyle: ButtonStyle.Primary,
// buttonString: "設置",
// clearConversationOnSwitchModel: true
}
/*embed: {
color: "#FF00F6"
}*/
})
processing messages:
if (!(message.mentions?.users?.first() || {})?.id === client.user.id || !message.content?.length) return;
message.channel.startTyping();
await chatData.handleMessage(message);
to make the buttons work
first you may export chatData or use it in the same file if you are not going to export it:
export default chatData.handleInteraction;
calling it from interactionCreate event:
// always treat the as shockbs package as global imports/variable
// or conversation history will be unsupported and seriously affect the performance hence increase response and process time
import handle from "../../../../../../../../foo/bar/../../foo/bar/../../foo/bar/../../events/messageCreate.js"; // importing it if you are importing it from another file, replace with your file path.
client.on("interactionCreate",async(interaction)=> {
handle(interaction);
...
// your other stuffs
})