From 8c0706aa7cabf9aa1e9d3eaee8dc0ed471b98800 Mon Sep 17 00:00:00 2001 From: n4ze3m Date: Sat, 25 Jan 2025 16:32:04 +0530 Subject: [PATCH] feat: Change default search provider to DuckDuckGo and add custom headers for Google search requests --- src/services/search.ts | 2 +- src/web/search-engines/google.ts | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/services/search.ts b/src/services/search.ts index deecc73..5cfbe0d 100644 --- a/src/services/search.ts +++ b/src/services/search.ts @@ -6,7 +6,7 @@ const storage2 = new Storage({ }) const TOTAL_SEARCH_RESULTS = 2 -const DEFAULT_PROVIDER = "google" +const DEFAULT_PROVIDER = "duckduckgo" const AVAILABLE_PROVIDERS = ["google", "duckduckgo"] as const diff --git a/src/web/search-engines/google.ts b/src/web/search-engines/google.ts index 94dd3c4..59e7175 100644 --- a/src/web/search-engines/google.ts +++ b/src/web/search-engines/google.ts @@ -28,12 +28,23 @@ export const localGoogleSearch = async (query: string) => { const htmlString = await fetch( `https://www.${baseGoogleDomain}/search?hl=en&q=` + query, { - signal: abortController.signal + signal: abortController.signal, + headers: { + "User-Agent": navigator.userAgent, + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", + "Accept-Language": "en-US,en;q=0.5", + "Accept-Encoding": "gzip, deflate, br", + "DNT": "1", + "Connection": "keep-alive", + "Upgrade-Insecure-Requests": "1", + "Sec-Fetch-Dest": "document", + "Sec-Fetch-Mode": "navigate", + "Sec-Fetch-Site": "none", + "Sec-Fetch-User": "?1" + } } - ) - .then((response) => response.text()) + ).then((response) => response.text()) .catch() - const parser = new DOMParser() const doc = parser.parseFromString(htmlString, "text/html") @@ -89,9 +100,9 @@ export const webGoogleSearch = async (query: string) => { baseUrl: cleanUrl(ollamaUrl) }) - + const textSplitter = await getPageAssistTextSplitter() - + const chunks = await textSplitter.splitDocuments(docs) const store = new MemoryVectorStore(ollamaEmbedding)