WebSearch: Add endpoint for Tavily

This commit is contained in:
Cohee 2024-10-23 23:38:17 +03:00
parent b5cdb29bf3
commit 0f320dd362
3 changed files with 50 additions and 0 deletions

View File

@ -34,6 +34,7 @@ export const SECRET_KEYS = {
STABILITY: 'api_key_stability',
BLOCKENTROPY: 'api_key_blockentropy',
CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
TAVILY: 'api_key_tavily',
};
const INPUT_MAP = {

View File

@ -206,6 +206,54 @@ router.post('/searxng', jsonParser, async (request, response) => {
}
});
router.post('/tavily', jsonParser, async (request, response) => {
try {
const apiKey = readSecret(request.user.directories, SECRET_KEYS.TAVILY);
if (!apiKey) {
console.log('No Tavily key found');
return response.sendStatus(400);
}
const { query } = request.body;
const body = {
query: query,
api_key: apiKey,
search_depth: 'basic',
topic: 'general',
include_answer: true,
include_raw_content: false,
include_images: false,
include_image_descriptions: false,
include_domains: [],
max_results: 10,
};
const result = await fetch('https://api.tavily.com/search', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
});
console.log('Tavily query', query);
if (!result.ok) {
const text = await result.text();
console.log('Tavily request failed', result.statusText, text);
return response.status(500).send(text);
}
const data = await result.json();
return response.json(data);
} catch (error) {
console.log(error);
return response.sendStatus(500);
}
});
router.post('/visit', jsonParser, async (request, response) => {
try {
const url = request.body.url;

View File

@ -47,6 +47,7 @@ export const SECRET_KEYS = {
STABILITY: 'api_key_stability',
BLOCKENTROPY: 'api_key_blockentropy',
CUSTOM_OPENAI_TTS: 'api_key_custom_openai_tts',
TAVILY: 'api_key_tavily',
};
// These are the keys that are safe to expose, even if allowKeysExposure is false