Skip to content

Commit

Permalink
Add support for SerpStack API (#669)
Browse files Browse the repository at this point in the history
* Add support for SerpStack API

* update const to SERPSTACK_API_KEY

* update readme

* lint

---------

Co-authored-by: Nathan Sarrazin <[email protected]>
  • Loading branch information
gururise and nsarrazin authored Dec 28, 2023
1 parent 9b99c58 commit f02ffb2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ HF_ACCESS_TOKEN=#LEGACY! Use HF_TOKEN instead
YDC_API_KEY=#your docs.you.com api key here
SERPER_API_KEY=#your serper.dev api key here
SERPAPI_KEY=#your serpapi key here
SERPSTACK_API_KEY=#your serpstack api key here
USE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys

WEBSEARCH_ALLOWLIST=`[]` # if it's defined, allow websites from only this list.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ PUBLIC_APP_DISCLAIMER=

### Web Search config

You can enable the web search through an API by adding `YDC_API_KEY` ([docs.you.com](https://docs.you.com)) or `SERPER_API_KEY` ([serper.dev](https://serper.dev/)) or `SERPAPI_KEY` ([serpapi.com](https://serpapi.com/)) to your `.env.local`.
You can enable the web search through an API by adding `YDC_API_KEY` ([docs.you.com](https://docs.you.com)) or `SERPER_API_KEY` ([serper.dev](https://serper.dev/)) or `SERPAPI_KEY` ([serpapi.com](https://serpapi.com/)) or `SERPSTACK_API_KEY` ([serpstack.com](https://serpstack.com/)) to your `.env.local`.

You can also simply enable the local websearch by setting `USE_LOCAL_WEBSEARCH=true` in your `.env.local`.

Expand Down
44 changes: 43 additions & 1 deletion src/lib/server/websearch/searchWeb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import 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 {
SERPAPI_KEY,
SERPER_API_KEY,
SERPSTACK_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";
Expand All @@ -24,6 +30,9 @@ export async function searchWeb(query: string) {
if (SERPAPI_KEY) {
return await searchWebSerpApi(query);
}
if (SERPSTACK_API_KEY) {
return await searchSerpStack(query);
}
throw new Error("No You.com or Serper.dev or SerpAPI key found");
}

Expand Down Expand Up @@ -100,3 +109,36 @@ export async function searchWebYouApi(query: string) {
organic_results: formattedResultsWithSnippets,
};
}

export async function searchSerpStack(query: string) {
const response = await fetch(
`http://api.serpstack.com/search?access_key=${SERPSTACK_API_KEY}&query=${query}&hl=en&gl=us`,
{
method: "GET",
headers: {
"Content-type": "application/json; charset=UTF-8",
},
}
);

const data = (await response.json()) as Record<string, any>;

if (!response.ok) {
throw new Error(
data["error"] ??
`SerpStack API returned error code ${response.status} - ${response.statusText}`
);
}

const resultsWithSnippets = data["organic_results"].map(
({ title, url, snippet }: { title: string; url: string; snippet: string | undefined }) => ({
title,
link: url,
text: snippet || "",
})
);

return {
organic_results: resultsWithSnippets ?? [],
};
}
9 changes: 8 additions & 1 deletion src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { DEFAULT_SETTINGS } from "$lib/types/Settings";
import {
SERPAPI_KEY,
SERPER_API_KEY,
SERPSTACK_API_KEY,
MESSAGES_BEFORE_LOGIN,
YDC_API_KEY,
USE_LOCAL_WEBSEARCH,
Expand Down Expand Up @@ -77,7 +78,13 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
}))
.toArray(),
settings: {
searchEnabled: !!(SERPAPI_KEY || SERPER_API_KEY || YDC_API_KEY || USE_LOCAL_WEBSEARCH),
searchEnabled: !!(
SERPAPI_KEY ||
SERPER_API_KEY ||
SERPSTACK_API_KEY ||
YDC_API_KEY ||
USE_LOCAL_WEBSEARCH
),
ethicsModalAccepted: !!settings?.ethicsModalAcceptedAt,
ethicsModalAcceptedAt: settings?.ethicsModalAcceptedAt ?? null,
activeModel: settings?.activeModel ?? DEFAULT_SETTINGS.activeModel,
Expand Down

0 comments on commit f02ffb2

Please sign in to comment.