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 = {
34 STABILITY: 'api_key_stability',34 STABILITY: 'api_key_stability',
35 BLOCKENTROPY: 'api_key_blockentropy',35 BLOCKENTROPY: 'api_key_blockentropy',
36 CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',36 CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
37 TAVILY: 'api_key_tavily',
37};38};
3839
39const INPUT_MAP = {40const INPUT_MAP = {
src/endpoints/search.js+48 -0
@@ -206,6 +206,54 @@ router.post('/searxng', jsonParser, async (request, response) => {
206 }206 }
207});207});
208208
209router.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
209router.post('/visit', jsonParser, async (request, response) => {257router.post('/visit', jsonParser, async (request, response) => {
210 try {258 try {
211 const url = request.body.url;259 const url = request.body.url;
src/endpoints/secrets.js+1 -0
@@ -47,6 +47,7 @@ export const SECRET_KEYS = {
47 STABILITY: 'api_key_stability',47 STABILITY: 'api_key_stability',
48 BLOCKENTROPY: 'api_key_blockentropy',48 BLOCKENTROPY: 'api_key_blockentropy',
49 CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',49 CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
50 TAVILY: 'api_key_tavily',
50};51};
5152
52// These are the keys that are safe to expose, even if allowKeysExposure is false53// These are the keys that are safe to expose, even if allowKeysExposure is false