feat: solution for 'Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)'

This commit is contained in:
2026-06-29 17:20:01 +03:00
parent 520acee860
commit 66a7a38c5d
5 changed files with 155 additions and 192 deletions
+22 -27
View File
@@ -1,41 +1,36 @@
const axios = require('axios');
require('dotenv').config();
import axios from "axios";
import dotenv from "dotenv";
dotenv.config();
const TAVILY_API_KEY = process.env.TAVILY_API_KEY;
const TAVILY_ENDPOINT = 'https://api.tavily.com/search';
const TAVILY_ENDPOINT = "https://api.tavily.com/search";
async function fetchEntityData(entity) {
export async function fetchSummary(entity) {
try {
const response = await axios.post(
TAVILY_ENDPOINT,
{
api_key: TAVILY_API_KEY,
query: entity,
search_depth: 2,
include_raw_content: true,
max_results: 5
include_raw: true,
},
{
headers: {
'Content-Type': 'application/json',
'accept': 'application/json',
'Authorization': `Bearer ${TAVILY_API_KEY}`
}
}
{ headers: { "Content-Type": "application/json" } }
);
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');
const results = response.data.results;
if (!results || results.length === 0) {
return `No summary available for ${entity}.`;
}
} catch (err) {
console.error('Error fetching data from Tavily:', err.message);
throw err;
}
}
module.exports = { fetchEntityData };
// Use the content of the first result as a concise summary
const firstResult = results[0];
const content = firstResult.content || firstResult.raw_content || "No content available.";
// Truncate to 200 characters for brevity
return content.length > 200 ? content.slice(0, 197) + "..." : content;
} catch (error) {
console.error(`Error fetching summary for ${entity}:`, error.message);
return `Error fetching summary for ${entity}.`;
}
}