nsarrazin HF staff commited on
Commit
bdb1788
1 Parent(s): 859cb22

Add allow & blocklist for websearch (#651)

Browse files

* Add allow & blocklist for websearch

* fix start whitespace

Files changed (2) hide show
  1. .env +3 -0
  2. src/lib/server/websearch/generateQuery.ts +15 -1
.env CHANGED
@@ -18,6 +18,9 @@ SERPER_API_KEY=#your serper.dev api key here
18
  SERPAPI_KEY=#your serpapi key here
19
  USE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys
20
 
 
 
 
21
  # Parameters to enable open id login
22
  OPENID_CONFIG=`{
23
  "PROVIDER_URL": "",
 
18
  SERPAPI_KEY=#your serpapi key here
19
  USE_LOCAL_WEBSEARCH=#set to true to parse google results yourself, overrides other API keys
20
 
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
+
24
  # Parameters to enable open id login
25
  OPENID_CONFIG=`{
26
  "PROVIDER_URL": "",
src/lib/server/websearch/generateQuery.ts CHANGED
@@ -1,6 +1,18 @@
1
  import type { Message } from "$lib/types/Message";
2
  import { format } from "date-fns";
3
  import { generateFromDefaultEndpoint } from "../generateFromDefaultEndpoint";
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  export async function generateQuery(messages: Message[]) {
6
  const currentDate = format(new Date(), "MMMM d, yyyy");
@@ -61,8 +73,10 @@ Current Question: Where is it being hosted ?`,
61
  },
62
  ];
63
 
64
- return await generateFromDefaultEndpoint({
65
  messages: convQuery,
66
  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}`,
67
  });
 
 
68
  }
 
1
  import type { Message } from "$lib/types/Message";
2
  import { format } from "date-fns";
3
  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(" ");
16
 
17
  export async function generateQuery(messages: Message[]) {
18
  const currentDate = format(new Date(), "MMMM d, yyyy");
 
73
  },
74
  ];
75
 
76
+ const webQuery = await generateFromDefaultEndpoint({
77
  messages: convQuery,
78
  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}`,
79
  });
80
+
81
+ return (queryModifier + " " + webQuery).trim();
82
  }