forked from huggingface/chat-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for SearXNG as a websearch source (huggingface#805)
* Created searchSearxng.ts for querying and parsing searxng instance. Added searxng to the searchWeb.ts to ensure inclusion in the search initialisation script. Added searxng to +layout.server.ts to ensure the search button is enabled if url provided in .env.local assignment Updated .env to include searxng configuration Updated README.md under Web Search Config to mention addition of searxng URL use in .env * Changes requested consolidated env variable to SEARXNG_QUERY_URL (felt considering the content the word QUERY was more relevant) refactored the searchSearxng.ts to generate the query with the new variable updated related references to variable (.env, searchWeb.ts and +layout.server.ts) Updated readme and variable comment on .env to reflect the same * urefectored to display SearXNG in search dialoge when used. * lint * types --------- Co-authored-by: Nathan Sarrazin <[email protected]>
1 parent
881070b
commit 7c22da3
Showing
6 changed files
with
52 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { SEARXNG_QUERY_URL } from "$env/static/private"; | ||
|
||
export async function searchSearxng(query: string) { | ||
const abortController = new AbortController(); | ||
setTimeout(() => abortController.abort(), 10000); | ||
|
||
// Insert the query into the URL template | ||
let url = SEARXNG_QUERY_URL.replace("<query>", query); | ||
|
||
// Check if "&format=json" already exists in the URL | ||
if (!url.includes("&format=json")) { | ||
url += "&format=json"; | ||
} | ||
|
||
// Call the URL to return JSON data | ||
const jsonResponse = await fetch(url, { | ||
signal: abortController.signal, | ||
}) | ||
.then((response) => response.json() as Promise<{ results: { url: string }[] }>) | ||
.catch((error) => { | ||
console.error("Failed to fetch or parse JSON", error); | ||
throw new Error("Failed to fetch or parse JSON"); | ||
}); | ||
|
||
// Extract 'url' elements from the JSON response and trim to the top 5 URLs | ||
const urls = jsonResponse.results.slice(0, 5).map((item) => item.url); | ||
|
||
if (!urls.length) { | ||
throw new Error(`Response doesn't contain any "url" elements`); | ||
} | ||
|
||
// Map URLs to the correct object shape | ||
return { organic_results: urls.map((link) => ({ link })) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters