feat: solution for 'Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)'
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
const axios = require('axios');
|
||||
require('dotenv').config();
|
||||
|
||||
const TAVILY_API_KEY = process.env.TAVILY_API_KEY;
|
||||
const TAVILY_ENDPOINT = 'https://api.tavily.com/search';
|
||||
|
||||
async function fetchEntityData(entity) {
|
||||
try {
|
||||
const response = await axios.post(
|
||||
TAVILY_ENDPOINT,
|
||||
{
|
||||
query: entity,
|
||||
search_depth: 2,
|
||||
include_raw_content: true,
|
||||
max_results: 5
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'accept': 'application/json',
|
||||
'Authorization': `Bearer ${TAVILY_API_KEY}`
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (response.data && response.data.results) {
|
||||
// Concatenate all raw content into a single string
|
||||
const texts = response.data.results
|
||||
.map((r) => r.raw_content || '')
|
||||
.filter(Boolean);
|
||||
return texts.join('\n\n');
|
||||
} else {
|
||||
throw new Error('No results returned from Tavily');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error fetching data from Tavily:', err.message);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { fetchEntityData };
|
||||
Reference in New Issue
Block a user