Skip to content

Commit bdb1788

Browse files
authored
Add allow & blocklist for websearch (#651)
* Add allow & blocklist for websearch * fix start whitespace
1 parent 859cb22 commit bdb1788

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ SERPER_API_KEY=#your serper.dev api key here
1818
SERPAPI_KEY=#your serpapi key here
1919
USE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys
2020

21+
WEBSEARCH_ALLOWLIST=`[]` # if it's defined, allow websites from only this list.
22+
WEBSEARCH_BLOCKLIST=`[]` # if it's defined, block websites from this list.
23+
2124
# Parameters to enable open id login
2225
OPENID_CONFIG=`{
2326
"PROVIDER_URL": "",

src/lib/server/websearch/generateQuery.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import type { Message } from "$lib/types/Message";
22
import { format } from "date-fns";
33
import { generateFromDefaultEndpoint } from "../generateFromDefaultEndpoint";
4+
import { WEBSEARCH_ALLOWLIST, WEBSEARCH_BLOCKLIST } from "$env/static/private";
5+
import { z } from "zod";
6+
7+
const listSchema = z.array(z.string()).default([]);
8+
9+
const allowList = listSchema.parse(JSON.parse(WEBSEARCH_ALLOWLIST));
10+
const blockList = listSchema.parse(JSON.parse(WEBSEARCH_BLOCKLIST));
11+
12+
const queryModifier = [
13+
...allowList.map((item) => "site:" + item),
14+
...blockList.map((item) => "-site:" + item),
15+
].join(" ");
416

517
export async function generateQuery(messages: Message[]) {
618
const currentDate = format(new Date(), "MMMM d, yyyy");
@@ -61,8 +73,10 @@ Current Question: Where is it being hosted ?`,
6173
},
6274
];
6375

64-
return await generateFromDefaultEndpoint({
76+
const webQuery = await generateFromDefaultEndpoint({
6577
messages: convQuery,
6678
preprompt: `You are tasked with generating web search queries. Give me an appropriate query to answer my question for google search. Answer with only the query. Today is ${currentDate}`,
6779
});
80+
81+
return (queryModifier + " " + webQuery).trim();
6882
}

0 commit comments

Comments
 (0)