Skip to content

Commit

Permalink
Enrich context with google
Browse files Browse the repository at this point in the history
  • Loading branch information
msveshnikov committed Mar 26, 2024
1 parent 940b695 commit d53310c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 14 deletions.
6 changes: 3 additions & 3 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ app.post("/interact", verifyToken, async (req, res) => {
if (userInput?.toLowerCase()?.includes("search") || userInput?.toLowerCase()?.includes("google")) {
const searchQuery = userInput.replace(/search\s*|google\s*/gi, "").trim();
searchResults = await fetchSearchResults(searchQuery);
// if (searchResults?.length > 0) {
// topResultContent = await fetchPageContent(searchResults[0].link);
// }
if (searchResults?.length > 0) {
topResultContent = await fetchPageContent(searchResults[0].link);
}
}

const contextPrompt = `System: ${systemPrompt} ${chatHistory
Expand Down
16 changes: 5 additions & 11 deletions server/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,26 @@ export async function fetchSearchResults(query) {
return null;
}
}
console.log(await fetchSearchResults("Java"));

//console.log(await fetchSearchResults("Java"));

export async function fetchPageContent(url) {
try {
const randomUserAgent = userAgents[Math.floor(Math.random() * userAgents.length)];
const response = await fetch(url, { headers: { "User-Agent": randomUserAgent } });
const html = await response.text();
const $ = load(html);

// Remove script and style elements
$('script, style').remove();

// Extract text from all remaining elements
const content = $('body *')
$("script, style").remove();
const content = $("body *")
.map((_, el) => $(el).text().trim())
.get()
.join(' ');

.join(" ");
return content;
} catch (error) {
return null;
}
}

// console.log(await fetchPageContent("https://www.slunecnice.cz/sw/java/"));
// console.log(await fetchPageContent("https://www.java.com/"));

export const google = async (term, lang) => {
const fetchData = async (term, lang) => {
Expand Down

0 comments on commit d53310c

Please sign in to comment.