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