feat: solution for 'Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)'
This commit is contained in:
+11
-13
@@ -1,14 +1,12 @@
|
||||
import React from 'react';
|
||||
import ComparisonTable from './components/ComparisonTable';
|
||||
import './App.css';
|
||||
import { searchAndSummarize } from './services/tavily.js';
|
||||
import { logInfo } from './services/otherService.js';
|
||||
|
||||
function App() {
|
||||
return (
|
||||
<div className="App">
|
||||
<h1>Entity Comparison</h1>
|
||||
<ComparisonTable />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default App;
|
||||
export async function run() {
|
||||
const query = 'Comparison of three entities';
|
||||
logInfo(`Starting search for: ${query}`);
|
||||
const results = await searchAndSummarize(query);
|
||||
logInfo('Results:');
|
||||
results.forEach((res, idx) => {
|
||||
console.log(`${idx + 1}. ${res.title}\n ${res.summary}\n`);
|
||||
});
|
||||
}
|
||||
+5
-11
@@ -1,12 +1,6 @@
|
||||
import React from 'react';
|
||||
import { createRoot } from 'react-dom/client';
|
||||
import App from './App';
|
||||
import './index.css';
|
||||
import { run } from './app.js';
|
||||
|
||||
const container = document.getElementById('root');
|
||||
const root = createRoot(container);
|
||||
root.render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>
|
||||
);
|
||||
run().catch((err) => {
|
||||
console.error('Application error:', err);
|
||||
process.exit(1);
|
||||
});
|
||||
@@ -0,0 +1,3 @@
|
||||
export function logInfo(message) {
|
||||
console.log(`[OtherService] ${message}`);
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
import { TavilyClient } from 'tavily-sdk';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const apiKey = process.env.TAVILY_API_KEY;
|
||||
if (!apiKey) {
|
||||
throw new Error('TAVILY_API_KEY environment variable is not set.');
|
||||
}
|
||||
|
||||
const client = new TavilyClient({ apiKey });
|
||||
|
||||
export async function searchAndSummarize(query) {
|
||||
try {
|
||||
// Perform a search with a maximum of 3 results
|
||||
const searchResponse = await client.search({
|
||||
query,
|
||||
maxResults: 3,
|
||||
});
|
||||
|
||||
// Summarize each result's content
|
||||
const summaries = await Promise.all(
|
||||
searchResponse.results.map(async (result) => {
|
||||
const summaryResponse = await client.summarize({
|
||||
text: result.content,
|
||||
});
|
||||
return {
|
||||
title: result.title,
|
||||
summary: summaryResponse.summary,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
return summaries;
|
||||
} catch (error) {
|
||||
console.error('Error during Tavily operation:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user