Adding: Web Access allowing the AI to browse URLs
This commit is contained in:
parent
a099502f1a
commit
a338fb725a
26
default.env
26
default.env
@ -1,16 +1,30 @@
|
|||||||
|
# Discord Token
|
||||||
THE_TOKEN = "DISCORD_TOKEN_HERE"
|
THE_TOKEN = "DISCORD_TOKEN_HERE"
|
||||||
|
|
||||||
|
# The Channel IDs the bot will operate in seperated by commas
|
||||||
CHANNEL_IDS = 1094494101631680653,1094628334727614605
|
CHANNEL_IDS = 1094494101631680653,1094628334727614605
|
||||||
|
|
||||||
|
# The INIT prompt for all conversations.
|
||||||
INIT_PROMPT = "Assistant name: ChatAI. You code, write and provide any information without any mistakes."
|
INIT_PROMPT = "Assistant name: ChatAI. You code, write and provide any information without any mistakes."
|
||||||
ROOT_PORT = 8000
|
|
||||||
DATA_DIR = /home/USERNAME/weights
|
# Loading Emebed Refresh Timing
|
||||||
CACHE = 1
|
REFRESH_INTERVAL=10
|
||||||
N_THREADS = 4
|
|
||||||
|
# Max Content to fetch from given URLs
|
||||||
|
MAX_CONTENT_LENGTH=2000
|
||||||
|
|
||||||
|
# Max tokens for Generations
|
||||||
MAX_TOKENS = 1024
|
MAX_TOKENS = 1024
|
||||||
|
|
||||||
# Bot specific settings
|
# END BOT SPECIFIC SETTINGS
|
||||||
REFRESH_INTERVAL=7
|
|
||||||
|
|
||||||
# ROOT_IP is only used when running the bot without docker compose
|
# ROOT_IP is only used when running the bot without docker compose
|
||||||
ROOT_IP = 192.168.0.15
|
ROOT_IP = 192.168.0.15
|
||||||
|
|
||||||
|
# PORT is only used when running the bot without docker compose
|
||||||
|
ROOT_PORT = 8000
|
||||||
|
|
||||||
|
# Directory to your models (llama.cpp specfic settings)
|
||||||
|
DATA_DIR = /home/USERNAME/weights
|
||||||
|
CACHE = 1
|
||||||
|
N_THREADS = 4
|
||||||
|
158
llamabot.js
158
llamabot.js
@ -153,17 +153,17 @@ client.on('messageCreate', async (message) => {
|
|||||||
|
|
||||||
// if we are over the discord char limit we need chunks...
|
// if we are over the discord char limit we need chunks...
|
||||||
if (response.length > limit) {
|
if (response.length > limit) {
|
||||||
const chunks = response.match(new RegExp(`.{1,${limit}}`, "g"));
|
const chunks = response.match(new RegExp(`.{1,${limit}}`, "g"));
|
||||||
for (let i = 0; i < chunks.length; i++) {
|
for (let i = 0; i < chunks.length; i++) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
message.channel.send(chunks[i]);
|
message.channel.send(chunks[i]);
|
||||||
}, i * 3000); // delay of 3 seconds between each chunk to save on API requests
|
}, i * 3000); // delay of 3 seconds between each chunk to save on API requests
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// We are good to go, send the response
|
// We are good to go, send the response
|
||||||
await message.channel.send(response);
|
await message.channel.send(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
setPresenceOnline()
|
setPresenceOnline()
|
||||||
setBusy(message.author.id, false);
|
setBusy(message.author.id, false);
|
||||||
} else {
|
} else {
|
||||||
@ -175,6 +175,8 @@ client.on('messageCreate', async (message) => {
|
|||||||
conversation.busy = false;
|
conversation.busy = false;
|
||||||
}
|
}
|
||||||
conversations.set(userID, conversation); // Update user's conversation map in memory
|
conversations.set(userID, conversation); // Update user's conversation map in memory
|
||||||
|
console.log(conversation)
|
||||||
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
sendRand(errorMessages)
|
sendRand(errorMessages)
|
||||||
@ -184,8 +186,60 @@ client.on('messageCreate', async (message) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
import cheerio from 'cheerio';
|
||||||
|
|
||||||
async function generateResponse(conversation, message) {
|
async function generateResponse(conversation, message) {
|
||||||
|
|
||||||
|
// Check if message contains a URL
|
||||||
|
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
||||||
|
const urls = message.content.match(urlRegex);
|
||||||
|
|
||||||
|
if (urls) {
|
||||||
|
// If there are multiple URLs, process them one by one
|
||||||
|
for (const url of urls) {
|
||||||
|
try {
|
||||||
|
const res = await fetch(url);
|
||||||
|
const html = await res.text();
|
||||||
|
const $ = cheerio.load(html);
|
||||||
|
|
||||||
|
// Extract page title, meta description and content
|
||||||
|
const pageTitle = $('head title').text().trim();
|
||||||
|
const pageDescription = $('head meta[name="description"]').attr('content');
|
||||||
|
const pageContent = $('body').text().trim();
|
||||||
|
|
||||||
|
// Construct response message with page details
|
||||||
|
let response = `Title: ${pageTitle}\n`;
|
||||||
|
if (pageDescription) {
|
||||||
|
response += `Description: ${pageDescription}\n`;
|
||||||
|
}
|
||||||
|
if (pageContent) {
|
||||||
|
const MAX_CONTENT_LENGTH = 1900;
|
||||||
|
let plainTextContent = $('<div>').html(pageContent).text().trim().replace(/[\r\n]+/g, ' ');
|
||||||
|
|
||||||
|
if (plainTextContent.length > MAX_CONTENT_LENGTH) {
|
||||||
|
plainTextContent = plainTextContent.substring(0, MAX_CONTENT_LENGTH) + '...';
|
||||||
|
response += `Content: ${plainTextContent}\n`;
|
||||||
|
} else {
|
||||||
|
response += `Content: ${plainTextContent}\n`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
response += `URL: ${url}`;
|
||||||
|
|
||||||
|
// Append bot message to conversation history
|
||||||
|
conversation.messages.push({
|
||||||
|
role: 'user',
|
||||||
|
content: "Data from the page is: " + response,
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log("A URL was provided, response: " + response)
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
sendRand(errorMessages);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
const controller = new AbortController();
|
const controller = new AbortController();
|
||||||
const timeout = setTimeout(() => {
|
const timeout = setTimeout(() => {
|
||||||
controller.abort();
|
controller.abort();
|
||||||
@ -198,45 +252,45 @@ async function generateResponse(conversation, message) {
|
|||||||
// define a function that shows the system load percentage and updates the message
|
// define a function that shows the system load percentage and updates the message
|
||||||
const showSystemLoad = async () => {
|
const showSystemLoad = async () => {
|
||||||
time = Number(time) + Number(process.env.REFRESH_INTERVAL);
|
time = Number(time) + Number(process.env.REFRESH_INTERVAL);
|
||||||
cpuStat.usagePercent(function(err, percent, seconds) {
|
cpuStat.usagePercent(function (err, percent, seconds) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return console.log(err);
|
return console.log(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
const systemLoad = percent;
|
const systemLoad = percent;
|
||||||
const freeMemory = os.freemem() / 1024 / 1024 / 1024;
|
const freeMemory = os.freemem() / 1024 / 1024 / 1024;
|
||||||
const totalMemory = os.totalmem() / 1024 / 1024 / 1024;
|
const totalMemory = os.totalmem() / 1024 / 1024 / 1024;
|
||||||
const usedMemory = totalMemory - freeMemory;
|
const usedMemory = totalMemory - freeMemory;
|
||||||
|
|
||||||
const embedData = {
|
const embedData = {
|
||||||
color: 0x0099ff,
|
color: 0x0099ff,
|
||||||
title: 'Please wait.. I am thinking...',
|
title: 'Please wait.. I am thinking...',
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
name: 'System Load',
|
name: 'System Load',
|
||||||
value: `${systemLoad.toFixed(2)}%`,
|
value: `${systemLoad.toFixed(2)}%`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Memory Usage',
|
name: 'Memory Usage',
|
||||||
value: `${usedMemory.toFixed(2)} GB / ${totalMemory.toFixed(2)} GB`,
|
value: `${usedMemory.toFixed(2)} GB / ${totalMemory.toFixed(2)} GB`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'Time',
|
name: 'Time',
|
||||||
value: `~${time} seconds.`,
|
value: `~${time} seconds.`,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
// if the message object doesn't exist, create it
|
// if the message object doesn't exist, create it
|
||||||
if (!botMessage) {
|
if (!botMessage) {
|
||||||
(async () => {
|
(async () => {
|
||||||
botMessage = await message.channel.send({ embeds: [embedData] });
|
botMessage = await message.channel.send({ embeds: [embedData] });
|
||||||
})();
|
})();
|
||||||
} else {
|
} else {
|
||||||
botMessage.edit({ embeds: [embedData] }); // otherwise, update the message
|
botMessage.edit({ embeds: [embedData] }); // otherwise, update the message
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// call the function initially
|
// call the function initially
|
||||||
await showSystemLoad();
|
await showSystemLoad();
|
||||||
@ -248,15 +302,15 @@ async function generateResponse(conversation, message) {
|
|||||||
const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, {
|
const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'accept': 'application/json',
|
'accept': 'application/json',
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
messages: messagesCopy,
|
messages: messagesCopy,
|
||||||
max_tokens: Number(process.env.MAX_TOKENS) // add the max_tokens parameter here
|
max_tokens: Number(process.env.MAX_TOKENS) // add the max_tokens parameter here
|
||||||
}),
|
}),
|
||||||
signal: controller.signal
|
signal: controller.signal
|
||||||
});
|
});
|
||||||
|
|
||||||
const responseData = await response.json();
|
const responseData = await response.json();
|
||||||
console.log(JSON.stringify(responseData));
|
console.log(JSON.stringify(responseData));
|
||||||
|
Loading…
Reference in New Issue
Block a user