From 84d50dfbc6aeb0d1dcde0bb4d52a8a37addff761 Mon Sep 17 00:00:00 2001 From: Yash <67926590+Yash094@users.noreply.github.com> Date: Sat, 11 Jan 2025 01:19:06 +0530 Subject: [PATCH 1/2] replace context AI --- .env.example | 5 +-- package.json | 5 ++- src/config.json | 17 +++++----- src/events/interaction.js | 28 +++++++++------ src/events/message.js | 71 +++++++++++++++++---------------------- src/utils/core.js | 29 +++++++++++----- src/utils/nebula.js | 45 +++++++++++++++++++++++++ 7 files changed, 126 insertions(+), 74 deletions(-) create mode 100644 src/utils/nebula.js diff --git a/.env.example b/.env.example index 3bd073b..536ce2f 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,6 @@ DISCORD_BOT_TOKEN= DISCORD_SUPPORT_ROLE_ID=, -CONTEXT_ID= ASKAI_CHANNEL= -REDIS_SERVER_URL= \ No newline at end of file +REDIS_SERVER_URL= +THIRDWEB_SECRET_KEY= +NEBULA_API_ENDPOINT= diff --git a/package.json b/package.json index 70c7569..63eccab 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "license": "GPL-3.0", "private": true, "dependencies": { - "@context-labs/sdk": "^0.0.107", + "axios": "^1.7.9", "discord.js": "^14.14.1", "dotenv": "^16.0.3", "fs-extra": "^11.1.1", @@ -20,6 +20,5 @@ "ioredis": "^5.4.1", "moment": "^2.30.1", "nodemon": "^3.1.0" - }, - "devDependencies": {} + } } diff --git a/src/config.json b/src/config.json index 2db506c..526ab69 100644 --- a/src/config.json +++ b/src/config.json @@ -1,10 +1,9 @@ { - "command_prefix": "!", - "utc_offset": -8, - "ai_thinking_message": "**šŸ¤– Beep Boop Boop Beep:** thinking...", - "getting_started_ai_message": "Hello, kindly use \\`!ask\\` or \\`!askai\\` followed by your question to get started.", - "helpful_message": "Thank you so much for your feedback!", - "not_helpful_messsage": "Thank you for your valuable feedback, this will help us improve the responses of our AI assistant.\n\nIn the meantime, would you like to contact a human customer success agent? Just click the link or the button below to submit a ticket.", - "reminder_mention": "Hey there! If you need assistance, please don't hesitate to reach out on our official support platform and create a ticket: https://thirdweb.com/support. Our dedicated team is always ready to help you out. Thank you for choosing to build with us!" - } - \ No newline at end of file + "command_prefix": "!", + "utc_offset": -8, + "ai_thinking_message": "Nebula is thinking...", + "getting_started_ai_message": "Hello, kindly use \\`!ask\\` or \\`!askai\\` followed by your question to get started.", + "helpful_message": "Thank you so much for your feedback!", + "not_helpful_messsage": "Thank you for your valuable feedback, this will help us improve the responses of our AI assistant.\n\nIn the meantime, would you like to contact a human customer success agent? Just click the link or the button below to submit a ticket.", + "reminder_mention": "Hey there! If you need assistance, please don't hesitate to reach out on our official support platform and create a ticket: https://thirdweb.com/support. Our dedicated team is always ready to help you out. Thank you for choosing to build with us!" +} diff --git a/src/events/interaction.js b/src/events/interaction.js index 7f49d7c..6a1189d 100644 --- a/src/events/interaction.js +++ b/src/events/interaction.js @@ -3,10 +3,9 @@ const { sendEmbedMessage, serverTime, CloseButtonComponent } = require("../utils/core"); -const { ContextSDK } = require("@context-labs/sdk"); +const { sendAIFeedback } = require("../utils/nebula") const redis = require("./database"); const config = require("../config.json"); -const context = new ContextSDK({}); module.exports = { name: Events.InteractionCreate, @@ -27,13 +26,16 @@ module.exports = { if (err) { console.error(err); } else { - await context.setQueryFeedback({ - queryId: result, - helpful: true, - }); + const data = JSON.parse(result); + + await sendAIFeedback( + data.session_id, + data.request_id, + true, + ); } }); - await interaction.message.edit({ components: [] }); + // await interaction.message.edit({ components: [] }); await redis.del(messageId); // log the feedback @@ -41,20 +43,24 @@ module.exports = { } if (interaction.customId === "not-helpful") { + await interaction.reply({ embeds: [sendEmbedMessage(config.not_helpful_messsage)], content: `šŸ”” <@${interaction.user.id}>`, ephemeral: true, components: [CloseButtonComponent()], }); + await redis.get(messageId, async (err, result) => { if (err) { console.error(err); } else { - await context.setQueryFeedback({ - queryId: result, - helpful: false, - }); + const data = JSON.parse(result); + await sendAIFeedback( + data.session_id, + data.request_id, + true, + ); } }); await interaction.message.edit({ components: [] }); diff --git a/src/events/message.js b/src/events/message.js index 7ac7bbc..8950e79 100644 --- a/src/events/message.js +++ b/src/events/message.js @@ -3,19 +3,18 @@ const { sendEmbedMessage, serverTime, FeedbackButtonComponent } = require("../utils/core"); + +const { getAIResponse } = require("../utils/nebula") const { version } = require("../../package.json"); const config = require("../config.json"); const redis = require("./database"); -const { ContextSDK } = require("@context-labs/sdk"); // discord bot env const { DISCORD_SUPPORT_ROLE_ID, - ASKAI_CHANNEL, - CONTEXT_ID } = process.env; + ASKAI_CHANNEL} = process.env; const roleIDs = DISCORD_SUPPORT_ROLE_ID.split(","); -const context = new ContextSDK({}); module.exports = { name: Events.MessageCreate, @@ -68,42 +67,34 @@ module.exports = { let aiMessageLoading = await message.channel.send({ embeds: [sendEmbedMessage(config.ai_thinking_message)], }); - - await context.query({ - botId: CONTEXT_ID, - query: question, - onComplete: async (query) => { - // respond to the user with the answer from the AI - await message.channel.messages.fetch(aiMessageLoading.id).then((msg) => { - msg.edit({ - content: `Hey <@${message.author.id}> šŸ‘‡`, - embeds: [ - sendEmbedMessage(`**Response:**\n${query.output.toString()}`), - ], - components: [FeedbackButtonComponent()], - - }) - redis.set(msg.id, query._id); - } - ); - - }, - onError: async (error) => { - console.error(error); - - // send a message indicates unseccesful response from the AI - await message.channel.messages.fetch(aiMessageLoading.id).then((msg) => - msg.edit({ - content: `Hey <@${message.author.id}> šŸ‘‡`, - embeds: [ - sendEmbedMessage(`**Response:**\nI'm sorry, I couldn't find a response to your question. Please try again later.`), - ], - - }) - ); - - }, - }); + let response = await getAIResponse(question) + await message.channel.messages.fetch(aiMessageLoading.id).then((msg) => { + msg.edit({ + content: `Hey <@${message.author.id}> šŸ‘‡`, + embeds: [ + sendEmbedMessage(`${response.message}`), + ], + components: [FeedbackButtonComponent()], + + }) + const data = { + request_id: response.request_id, + session_id: response.session_id, + }; + redis.set(msg.id, JSON.stringify(data)); + } + ); + // to do + // send a message indicates unseccesful response from the AI + // await message.channel.messages.fetch(aiMessageLoading.id).then((msg) => + // msg.edit({ + // content: `Hey <@${message.author.id}> šŸ‘‡`, + // embeds: [ + // sendEmbedMessage(`**Response:**\nI'm sorry, I couldn't find a response to your question. Please try again later.`), + // ], + + // }) + // ); } else { // if the command is not from the channel diff --git a/src/utils/core.js b/src/utils/core.js index 2b89970..36080ca 100644 --- a/src/utils/core.js +++ b/src/utils/core.js @@ -9,16 +9,27 @@ const config = require('../config.json'); * @returns pre-defined embed style */ const sendEmbedMessage = (message) => { - return new EmbedBuilder() - .setDescription(message) - .setColor(`#f213a4`) - .addFields( - { name: 'Need Help?', value: 'Submit a ticket here: https://thirdweb.com/support', inline: false} - ) - .setTimestamp() - .setFooter({ text: 'thirdweb', iconURL: 'https://ipfs.io/ipfs/QmTWMy6Dw1PDyMxHxNcmDmPE8zqFCQMfD6m2feHVY89zgu/Icon/Favicon-01.png' }); + return new EmbedBuilder() + .setTitle('Nebula Response') + .setDescription(message) + .setColor('#f213a4') + .setThumbnail('https://ipfs.io/ipfs/QmTWMy6Dw1PDyMxHxNcmDmPE8zqFCQMfD6m2feHVY89zgu/Icon/Favicon-01.png') // Nebula thumbnail + .addFields( + { + name: 'Nebula Features', + value: 'šŸ”— Proprietary Blockchain Model\nāš” Execute Transactions with Ease\nšŸ” Seamless Wallet Integration\nšŸ› ļø Easy-to-use API\nšŸ¤– Autonomous Agents for EVM Chains', + inline: false + } + ) + .setTimestamp() + .setFooter({ + text: 'Powered by thirdweb - Nebula | Disclaimer: Nebula may make mistakes. Please use with discretion.', + iconURL: 'https://ipfs.io/ipfs/QmTWMy6Dw1PDyMxHxNcmDmPE8zqFCQMfD6m2feHVY89zgu/Icon/Favicon-01.png' + }); +}; + + -} /** * close buttons * @param {string} message diff --git a/src/utils/nebula.js b/src/utils/nebula.js new file mode 100644 index 0000000..62245a2 --- /dev/null +++ b/src/utils/nebula.js @@ -0,0 +1,45 @@ +const axios = require('axios'); + +const { THIRDWEB_SECRET_KEY, NEBULA_API_ENDPOINT } = process.env; + +async function getAIResponse(query) { + try { + const response = await axios.post(NEBULA_API_ENDPOINT + '/chat', { + message: query + }, { + headers: { + 'Content-Type': 'application/json', + 'x-secret-key': THIRDWEB_SECRET_KEY + } + }); + + + const data = response.data; + + return data; // Return the data for further use + } catch (error) { + console.error('Error:', error); + throw error; // Re-throw the error for handling in the calling function + } +} + +async function sendAIFeedback(session_id, request_id, helpful) { + + + const response = await axios.post(NEBULA_API_ENDPOINT + '/feedback', { + feedback_rating: helpful ? 1 : -1, + request_id: request_id, + session_id: session_id + + }, { + headers: { + 'Content-Type': 'application/json', + 'x-secret-key': THIRDWEB_SECRET_KEY + } + }); + return response + + +} + +module.exports = { getAIResponse, sendAIFeedback }; From 159e51472ffd11eacb027e96fd764088748a9930 Mon Sep 17 00:00:00 2001 From: Yash <67926590+Yash094@users.noreply.github.com> Date: Sat, 11 Jan 2025 01:20:33 +0530 Subject: [PATCH 2/2] fix comment. --- src/events/interaction.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/events/interaction.js b/src/events/interaction.js index 6a1189d..9f099ae 100644 --- a/src/events/interaction.js +++ b/src/events/interaction.js @@ -35,7 +35,7 @@ module.exports = { ); } }); - // await interaction.message.edit({ components: [] }); + await interaction.message.edit({ components: [] }); await redis.del(messageId); // log the feedback