fix plugins

This commit is contained in:
Raven 2024-08-03 18:46:41 -04:00
parent fd062bae78
commit 180efb0268

View File

@ -139,7 +139,7 @@ async function scrapeWebPage(url, length) {
}
response += `\nURL: ${url}`;
console.log(`${getTimestamp()} [INFO] Successfully scraped URL: ${url}`);
// console.log(`${getTimestamp()} [INFO] Successfully scraped URL: ${url}`);
return response;
} catch (err) {
console.error(`${getTimestamp()} [ERROR] Failed to scrape URL: ${url}`, err);
@ -187,9 +187,108 @@ app.post('/api/v1/chat', async (req, res) => {
const ipRegex = /(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/g;
const ipAddresses = userMessage.match(ipRegex);
const urlRegex = /(https?:\/\/[^\s]+)/g;
const urls = userMessage.match(urlRegex);
const newPersonRegex = /\bnew\s+person\b/gi;
const newPersons = userMessage.match(newPersonRegex);
const whatServersRegex = /\bwhat\s+servers\b/gi;
const myMcRegex = /\bmy-mc\b/gi;
const dlinuxRegex = /\bdlinux\b/gi;
const searchRegex = /\b[Ss]earch\s+(.+)\b/;
const searchMatch = userMessage.match(searchRegex);
// Handle scraping and other plugins asynchronously
const pluginTasks = [];
if (ipAddresses) {
console.log(`${getTimestamp()} [INFO] Detected IP addresses in user message: ${ipAddresses}`);
for (const ipAddr of ipAddresses) {
ipAddresses.forEach(ipAddr => {
pluginTasks.push(handleIPPlugin(ipAddr, ip, conversationHistory));
});
}
if (urls) {
console.log(`${getTimestamp()} [INFO] Detected URLs in user message: ${urls}`);
urls.forEach(url => {
pluginTasks.push(handleURLPlugin(url, ip, conversationHistory));
});
}
if (newPersons) {
console.log(`${getTimestamp()} [INFO] Detected new person request in user message`);
newPersons.forEach(() => {
pluginTasks.push(handleNewPersonPlugin(ip, conversationHistory));
});
}
if (whatServersRegex.test(userMessage)) {
console.log(`${getTimestamp()} [INFO] Detected what servers request in user message`);
pluginTasks.push(handleWhatServersPlugin(ip, conversationHistory));
}
if (myMcRegex.test(userMessage)) {
console.log(`${getTimestamp()} [INFO] Detected My-MC.Link request in user message`);
pluginTasks.push(handleMyMcPlugin(ip, conversationHistory));
}
if (dlinuxRegex.test(userMessage)) {
console.log(`${getTimestamp()} [INFO] Detected dlinux request in user message`);
pluginTasks.push(handleDlinuxPlugin(ip, conversationHistory));
}
if (searchMatch) {
const searchQuery = searchMatch[1];
console.log(`${getTimestamp()} [INFO] Detected search query in user message: ${searchQuery}`);
pluginTasks.push(handleSearchPlugin(searchQuery, ip, conversationHistory));
}
// Wait for all plugin tasks to complete before sending the request to llama
await Promise.all(pluginTasks);
console.log(`${getTimestamp()} [INFO] Sending request to llama API for response`);
const sent = {
model: 'model',
messages: conversationHistory[ip]
};
console.log(sent);
const llamaResponse = await fetch(`http://127.0.0.1:8002/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(sent)
});
const response = await llamaResponse.json();
const assistantMessage = response.choices[0].message;
conversationHistory[ip].push(assistantMessage);
console.log(`${getTimestamp()} [INFO] Received response from llama API`);
console.log(`${getTimestamp()} [DEBUG] Finish Reason: ${response.choices[0].finish_reason}`);
console.log(`${getTimestamp()} [STATS] Usage: prompt_tokens=${response.usage.prompt_tokens}, completion_tokens=${response.usage.completion_tokens}, total_tokens=${response.usage.total_tokens}`);
res.json(assistantMessage);
} catch (error) {
console.error(`${getTimestamp()} [ERROR] An error occurred while handling chat request`, error);
res.status(500).json({
message: "An error occurred",
error: error.message
});
} finally {
isProcessing = false;
const endTime = Date.now(); // End time tracking
const processingTime = ((endTime - startTime) / 1000).toFixed(2); // Calculate processing time in seconds
console.log(`${getTimestamp()} [STATS] Processing Time: ${processingTime} seconds`);
console.log(`${getTimestamp()} [INFO] Finished processing chat request for: ${ip}`);
}
});
// Define plugin handling functions
async function handleIPPlugin(ipAddr, ip, conversationHistory) {
try {
const url = new URL('https://api.abuseipdb.com/api/v2/check');
url.searchParams.append('ipAddress', ipAddr);
@ -228,20 +327,10 @@ app.post('/api/v1/chat', async (req, res) => {
}
} catch (err) {
console.error(`${getTimestamp()} [ERROR] Failed to process IP address: ${ipAddr}`, err);
return res.status(500).json({
message: "An error occurred while processing IP",
error: err.message
});
}
}
}
}
const urlRegex = /(https?:\/\/[^\s]+)/g;
const urls = userMessage.match(urlRegex);
if (urls) {
console.log(`${getTimestamp()} [INFO] Detected URLs in user message: ${urls}`);
for (const url of urls) {
async function handleURLPlugin(url, ip, conversationHistory) {
const scrapedContent = await scrapeWebPage(url);
if (scrapedContent) {
conversationHistory[ip].push({
@ -250,16 +339,9 @@ app.post('/api/v1/chat', async (req, res) => {
});
console.log(`${getTimestamp()} [INFO] Added scraped content to conversation history for: ${ip}`);
}
}
}
}
const newPersonRegex = /\bnew\s+person\b/gi;
const newPersons = userMessage.match(newPersonRegex);
if (newPersons) {
// If there are multiple occurrences of "new person", process them one by one
for (const newPerson of newPersons) {
async function handleNewPersonPlugin(ip, conversationHistory) {
try {
const randomUser = await fetchRandomUser();
@ -272,10 +354,9 @@ app.post('/api/v1/chat', async (req, res) => {
response += `Phone: ${randomUser.phone}\n`;
// Add more details as needed
// Get the index of the last message in the array
conversationHistory[ip].push({
role: 'user',
content: "Fetched Info: " + response
role: 'assistant',
content: response
});
console.log("A request for a new person was made. Response: " + response);
@ -284,25 +365,10 @@ app.post('/api/v1/chat', async (req, res) => {
}
} catch (err) {
console.error(err);
await sendRand(errorMessages);
}
}
}
}
async function fetchRandomUser() {
try {
const response = await fetch('https://randomuser.me/api/');
const data = await response.json();
return data.results[0]; // Assuming you only need one random user
} catch (error) {
console.error('Error fetching random user:', error);
return null;
}
}
const whatServersRegex = /\bwhat\s+servers\b/gi;
if (whatServersRegex.test(userMessage)) {
async function handleWhatServersPlugin(ip, conversationHistory) {
try {
const response = await fetch(`https://api.my-mc.link/list_all_servers/${process.env.PATH_KEY}/`, {
headers: {
@ -324,8 +390,8 @@ app.post('/api/v1/chat', async (req, res) => {
}
conversationHistory[ip].push({
role: 'user',
content: "Fetched Info: " + responseMessage
role: 'assistant',
content: responseMessage
});
console.log("A request for server information was made. Response: " + responseMessage);
@ -334,13 +400,10 @@ app.post('/api/v1/chat', async (req, res) => {
}
} catch (error) {
console.error('Error fetching server information:', error);
await sendRand(errorMessages);
}
}
}
const myMcRegex = /\bmy-mc\b/gi;
if (myMcRegex.test(userMessage)) {
async function handleMyMcPlugin(ip, conversationHistory) {
try {
const response = await fetch('https://my-mc.link/wiki.json');
const data = await response.json();
@ -362,8 +425,8 @@ app.post('/api/v1/chat', async (req, res) => {
});
conversationHistory[ip].push({
role: 'user',
content: "Fetched Info: " + responseMessage
role: 'assistant',
content: responseMessage
});
console.log("A request for My-MC.Link wiki information was made.");
} else {
@ -371,13 +434,10 @@ app.post('/api/v1/chat', async (req, res) => {
}
} catch (error) {
console.error('Error fetching My-MC.Link wiki information:', error);
await sendRand(errorMessages);
}
}
}
const dlinuxRegex = /\bdlinux\b/gi;
if (dlinuxRegex.test(userMessage)) {
async function handleDlinuxPlugin(ip, conversationHistory) {
try {
const response = await fetch('https://my-mc.link/dwiki.json');
const data = await response.json();
@ -399,8 +459,8 @@ app.post('/api/v1/chat', async (req, res) => {
});
conversationHistory[ip].push({
role: 'user',
content: "Fetched Info: " + responseMessage
role: 'assistant',
content: responseMessage
});
console.log("A request for dlinux wiki information was made.");
@ -409,17 +469,10 @@ app.post('/api/v1/chat', async (req, res) => {
}
} catch (error) {
console.error('Error fetching dlinux wiki information:', error);
await sendRand(errorMessages);
}
}
}
const searchRegex = /\b[Ss]earch\s+(.+)\b/;
const searchMatch = userMessage.match(searchRegex);
if (searchMatch) {
const searchQuery = searchMatch[1];
console.log(`${getTimestamp()} [INFO] Detected search query in user message: ${searchQuery}`);
async function handleSearchPlugin(searchQuery, ip, conversationHistory) {
const options = {
query: searchQuery,
limit: 5,
@ -449,7 +502,7 @@ app.post('/api/v1/chat', async (req, res) => {
const lastMessageIndex = conversationHistory[ip].length - 1;
if (lastMessageIndex >= 0) {
conversationHistory[ip][lastMessageIndex].content += "\n" + searchResponse;
conversationHistory[ip][lastMessageIndex].content += "\nYou scraped these results, review them provide links: " + searchResponse;
console.log(`${getTimestamp()} [INFO] Processed search query: ${searchQuery}, response: ${searchResponse}`);
} else {
console.error(`${getTimestamp()} [ERROR] Conversation history is unexpectedly empty for: ${ip}`);
@ -462,83 +515,10 @@ app.post('/api/v1/chat', async (req, res) => {
});
console.log(`${getTimestamp()} [INFO] Added scraped content to conversation history for: ${ip}`);
}
// Now send the request to llama API after scraping is done
console.log(`${getTimestamp()} [INFO] Sending request to llama API for response`);
const sent = {
model: 'model',
messages: conversationHistory[ip]
};
//console.log(sent);
const llamaResponse = await fetch(`http://127.0.0.1:8002/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(sent)
});
const response = await llamaResponse.json();
const assistantMessage = response.choices[0].message;
conversationHistory[ip].push(assistantMessage);
console.log(`${getTimestamp()} [INFO] Received response from llama API`);
console.log(`${getTimestamp()} [DEBUG] Finish Reason: ${response.choices[0].finish_reason}`);
console.log(`${getTimestamp()} [STATS] Usage: prompt_tokens=${response.usage.prompt_tokens}, completion_tokens=${response.usage.completion_tokens}, total_tokens=${response.usage.total_tokens}`);
res.json(assistantMessage);
} catch (err) {
console.error(`${getTimestamp()} [ERROR] Failed to perform Google search: ${searchQuery}`, err);
return res.status(500).json({
message: "An error occurred while performing Google search",
error: err.message
});
}
} else {
// If no search plugin is used, send the request to llama API directly
console.log(`${getTimestamp()} [INFO] Sending request to llama API for response`);
const sent = {
model: 'model',
messages: conversationHistory[ip]
};
console.log(sent);
const llamaResponse = await fetch(`http://127.0.0.1:8002/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(sent)
});
const response = await llamaResponse.json();
const assistantMessage = response.choices[0].message;
conversationHistory[ip].push(assistantMessage);
console.log(`${getTimestamp()} [INFO] Received response from llama API`);
console.log(`${getTimestamp()} [DEBUG] Finish Reason: ${response.choices[0].finish_reason}`);
console.log(`${getTimestamp()} [STATS] Usage: prompt_tokens=${response.usage.prompt_tokens}, completion_tokens=${response.usage.completion_tokens}, total_tokens=${response.usage.total_tokens}`);
res.json(assistantMessage);
}
} catch (error) {
console.error(`${getTimestamp()} [ERROR] An error occurred while handling chat request`, error);
res.status(500).json({
message: "An error occurred",
error: error.message
});
} finally {
isProcessing = false;
const endTime = Date.now(); // End time tracking
const processingTime = ((endTime - startTime) / 1000).toFixed(2); // Calculate processing time in seconds
console.log(`${getTimestamp()} [STATS] Processing Time: ${processingTime} seconds`);
console.log(`${getTimestamp()} [INFO] Finished processing chat request for: ${ip}`);
}
});
}
app.get('/api/v1/conversation-history', (req, res) => {
const ip = req.clientIp;