Option for running websearch locally (#563)
Browse files* Fully local websearch
* better comments
* fix else if
Files changed (4)
13 unmodified lines14151617181913 unmodified linesYDC_API_KEY=#your docs.you.com api key hereSERPER_API_KEY=#your serper.dev api key hereSERPAPI_KEY=#your serpapi key here# Parameters to enable open id loginOPENID_CONFIG=`{13 unmodified lines1415161718192013 unmodified linesYDC_API_KEY=#your docs.you.com api key hereSERPER_API_KEY=#your serper.dev api key hereSERPAPI_KEY=#your serpapi key hereUSE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys# Parameters to enable open id loginOPENID_CONFIG=`{
14 unmodified lines15161718192024 unmodified lines454647484950515253545556575814 unmodified linesconst MAX_N_PAGES_SCRAPE = 10 as const;const MAX_N_PAGES_EMBED = 5 as const;export async function runWebSearch(conv: Conversation,prompt: string,24 unmodified linesconst results = await searchWeb(webSearch.searchQuery);webSearch.results =(results.organic_results &&results.organic_results.map((el: { title: string; link: string; text?: string }) => {const { title, link, text } = el;const { hostname } = new URL(link);return { title, link, hostname, text };})) ??[];webSearch.results = webSearch.results.filter(({ link }) => !link.includes("youtube.com")) // filter out youtube links.slice(0, MAX_N_PAGES_SCRAPE); // limit to first 10 links onlylet paragraphChunks: { source: WebSearchSource; text: string }[] = [];14 unmodified lines151617181920212224 unmodified lines474849505152535455565758596014 unmodified linesconst MAX_N_PAGES_SCRAPE = 10 as const;const MAX_N_PAGES_EMBED = 5 as const;const DOMAIN_BLOCKLIST = ["youtube.com", "twitter.com"];export async function runWebSearch(conv: Conversation,prompt: string,24 unmodified linesconst results = await searchWeb(webSearch.searchQuery);webSearch.results =(results.organic_results &&results.organic_results.map((el: { title?: string; link: string; text?: string }) => {const { title, link, text } = el;const { hostname } = new URL(link);return { title, link, hostname, text };})) ??[];webSearch.results = webSearch.results.filter(({ link }) => !DOMAIN_BLOCKLIST.some((el) => link.includes(el))) // filter out blocklist links.slice(0, MAX_N_PAGES_SCRAPE); // limit to first 10 links onlylet paragraphChunks: { source: WebSearchSource; text: string }[] = [];
123456782 unmodified lines111213141516import type { YouWebSearch } from "../../types/WebSearch";import { WebSearchProvider } from "../../types/WebSearch";import { SERPAPI_KEY, SERPER_API_KEY, YDC_API_KEY } from "$env/static/private";import { getJson } from "serpapi";import type { GoogleParameters } from "serpapi";// get which SERP api is providing web resultsexport function getWebSearchProvider() {2 unmodified lines// Show result as JSONexport async function searchWeb(query: string) {if (SERPER_API_KEY) {return await searchWebSerper(query);}1234567892 unmodified lines121314151617181920import type { YouWebSearch } from "../../types/WebSearch";import { WebSearchProvider } from "../../types/WebSearch";import { SERPAPI_KEY, SERPER_API_KEY, USE_LOCAL_WEBSEARCH, YDC_API_KEY } from "$env/static/private";import { getJson } from "serpapi";import type { GoogleParameters } from "serpapi";import { searchWebLocal } from "./searchWebLocal";// get which SERP api is providing web resultsexport function getWebSearchProvider() {2 unmodified lines// Show result as JSONexport async function searchWeb(query: string) {if (USE_LOCAL_WEBSEARCH) {return await searchWebLocal(query);}if (SERPER_API_KEY) {return await searchWebSerper(query);}
123456789101112131415161718192021222324252627282930313233343536373839404142434445import { JSDOM, VirtualConsole } from "jsdom";
export async function searchWebLocal(query: string) { const abortController = new AbortController(); setTimeout(() => abortController.abort(), 10000);
const htmlString = await fetch("https://www.google.com/search?hl=en&q=" + query, { signal: abortController.signal, }) .then((response) => response.text()) .catch();
const virtualConsole = new VirtualConsole();
virtualConsole.on("error", () => { // No-op to skip console errors. });
// put the html string into a DOM const dom = new JSDOM(htmlString ?? "", { virtualConsole, });
const { document } = dom.window; // get all a documents with href tag
const links = document.querySelectorAll("a");
if (!links.length) { throw new Error(`webpage doesn't have any "a" element`); }
// take url that start wirth /url?q= // and do not contain google.com links // and strip them up to '&sa=' const linksHref = Array.from(links) .filter((el) => el.href?.startsWith("/url?q=") && !el.href.includes("google.com/")) .map((el) => { const link = el.href; return link.slice("/url?q=".length, link.indexOf("&sa=")); });
// remove duplicate links and map links to the correct object shape return { organic_results: [...new Set(linksHref)].map((link) => ({ link })) };}.env+1
src/lib/server/websearch/runWebSearch.ts+4-2
src/lib/server/websearch/searchWeb.ts+5-1
src/lib/server/websearch/searchWebLocal.tsAdded+45