WebSearch: Add endpoint for Tavily

0f320dd362940454de63238ce50861f4be93df89

Cohee <18619528+Cohee1207@users.noreply.github.com>

3 files changed, +50 -0Showing whitespace changes
public/scripts/secrets.js+1 -0
@@ -34,6 +34,7 @@ export const SECRET_KEYS = {
3434 STABILITY: 'api_key_stability',
3535 BLOCKENTROPY: 'api_key_blockentropy',
3636 CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
37+ TAVILY: 'api_key_tavily',
3738};
3839
3940const INPUT_MAP = {
src/endpoints/search.js+48 -0
@@ -206,6 +206,54 @@ router.post('/searxng', jsonParser, async (request, response) => {
206206 }
207207});
208208
209+router.post('/tavily', jsonParser, async (request, response) => {
210+ try {
211+ const apiKey = readSecret(request.user.directories, SECRET_KEYS.TAVILY);
212+
213+ if (!apiKey) {
214+ console.log('No Tavily key found');
215+ return response.sendStatus(400);
216+ }
217+
218+ const { query } = request.body;
219+
220+ const body = {
221+ query: query,
222+ api_key: apiKey,
223+ search_depth: 'basic',
224+ topic: 'general',
225+ include_answer: true,
226+ include_raw_content: false,
227+ include_images: false,
228+ include_image_descriptions: false,
229+ include_domains: [],
230+ max_results: 10,
231+ };
232+
233+ const result = await fetch('https://api.tavily.com/search', {
234+ method: 'POST',
235+ headers: {
236+ 'Content-Type': 'application/json',
237+ },
238+ body: JSON.stringify(body),
239+ });
240+
241+ console.log('Tavily query', query);
242+
243+ if (!result.ok) {
244+ const text = await result.text();
245+ console.log('Tavily request failed', result.statusText, text);
246+ return response.status(500).send(text);
247+ }
248+
249+ const data = await result.json();
250+ return response.json(data);
251+ } catch (error) {
252+ console.log(error);
253+ return response.sendStatus(500);
254+ }
255+});
256+
209257router.post('/visit', jsonParser, async (request, response) => {
210258 try {
211259 const url = request.body.url;
src/endpoints/secrets.js+1 -0
@@ -47,6 +47,7 @@ export const SECRET_KEYS = {
4747 STABILITY: 'api_key_stability',
4848 BLOCKENTROPY: 'api_key_blockentropy',
4949 CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
50+ TAVILY: 'api_key_tavily',
5051};
5152
5253// These are the keys that are safe to expose, even if allowKeysExposure is false