36 lines
1.1 KiB
JavaScript
36 lines
1.1 KiB
JavaScript
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";
|
|
|
|
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: true,
|
|
},
|
|
{ headers: { "Content-Type": "application/json" } }
|
|
);
|
|
|
|
const results = response.data.results;
|
|
if (!results || results.length === 0) {
|
|
return `No summary available for ${entity}.`;
|
|
}
|
|
|
|
// 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}.`;
|
|
}
|
|
} |