Limit Search Results to 5

This commit is contained in:
Raven 2024-08-03 16:27:17 -04:00
parent e25557190c
commit ae82fda49f

View File

@ -425,52 +425,58 @@ app.post('/api/v1/chat', async (req, res) => {
} }
const searchRegex = /\b[Ss]earch\s+(.+)\b/; const searchRegex = /\b[Ss]earch\s+(.+)\b/;
const searchMatch = userMessage.match(searchRegex); const searchMatch = userMessage.match(searchRegex);
if (searchMatch) { if (searchMatch) {
const searchQuery = searchMatch[1]; const searchQuery = searchMatch[1];
console.log(`${getTimestamp()} [INFO] Detected search query in user message: ${searchQuery}`); console.log(`${getTimestamp()} [INFO] Detected search query in user message: ${searchQuery}`);
try { const options = {
googleIt({ 'query': searchQuery }).then(async results => { query: searchQuery,
let searchResponse = `Search Query: ${searchQuery}\n`; limit: 5,
searchResponse += `Top Google search results:\n`; disableConsole: true
};
for (let i = 0; i < results.length; i++) { try {
const result = results[i]; googleIt(options).then(async results => {
searchResponse += `${i + 1}. ${result.title} - ${result.link}\n`; let searchResponse = `Search Query: ${searchQuery}\n`;
searchResponse += `Top Google search results:\n`;
try { for (let i = 0; i < results.length; i++) {
const scrapeResult = await scrapeWebPage(result.link, 200); const result = results[i];
searchResponse += `Scraped Data: ${scrapeResult}\n`; searchResponse += `${i + 1}. ${result.title} - ${result.link}\n`;
} catch (scrapeErr) {
console.error(`${getTimestamp()} [ERROR] Failed to scrape URL: ${result.link}`, scrapeErr);
searchResponse += `Failed to scrape URL: ${result.link}\n`;
}
}
const lastMessageIndex = conversationHistory[ip].length - 1; try {
if (lastMessageIndex >= 0) { const scrapeResult = await scrapeWebPage(result.link, 200);
conversationHistory[ip][lastMessageIndex].content += "\n" + searchResponse; searchResponse += `Scraped Data: ${scrapeResult}\n`;
console.log(`${getTimestamp()} [INFO] Processed search query: ${searchQuery}, response: ${searchResponse}`); } catch (scrapeErr) {
} else { console.error(`${getTimestamp()} [ERROR] Failed to scrape URL: ${result.link}`, scrapeErr);
console.error(`${getTimestamp()} [ERROR] Conversation history is unexpectedly empty for: ${ip}`); searchResponse += `Failed to scrape URL: ${result.link}\n`;
} }
}).catch(err => { }
console.error(`${getTimestamp()} [ERROR] Failed to perform Google search: ${searchQuery}`, err);
return res.status(500).json({ const lastMessageIndex = conversationHistory[ip].length - 1;
message: "An error occurred while performing Google search", if (lastMessageIndex >= 0) {
error: err.message conversationHistory[ip][lastMessageIndex].content += "\n" + searchResponse;
}); console.log(`${getTimestamp()} [INFO] Processed search query: ${searchQuery}, response: ${searchResponse}`);
}); } else {
} catch (err) { console.error(`${getTimestamp()} [ERROR] Conversation history is unexpectedly empty for: ${ip}`);
console.error(`${getTimestamp()} [ERROR] An unexpected error occurred:`, err); }
return res.status(500).json({ }).catch(err => {
message: "An unexpected error occurred", console.error(`${getTimestamp()} [ERROR] Failed to perform Google search: ${searchQuery}`, err);
error: err.message return res.status(500).json({
}); message: "An error occurred while performing Google search",
} error: err.message
} });
});
} catch (err) {
console.error(`${getTimestamp()} [ERROR] An unexpected error occurred:`, err);
return res.status(500).json({
message: "An unexpected error occurred",
error: err.message
});
}
}
// End Plugins --- // End Plugins ---