diff --git a/README.md b/README.md index 59b8232..8b362d6 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,66 @@ -# Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily) +# Tavily Comparison Project -Главная -Мои задания -Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily) -5Д -EN -Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily) -Зачёт -Версия 11 -Дедлайн сдачи: 31.08.2026 +This project demonstrates how to integrate the **Tavily API** to compare three entities. It performs a search query and summarizes the top results using the official Tavily SDK. -В работе +## Prerequisites -Требуется доработка +- Node.js (v18 or newer) +- A Tavily API key. Sign up at https://tavily.com and obtain your key. -В работе отсутствуют ключевые зависимости и функциональные элементы, указанные в задании. Необходимо добавить недостающие пакеты и реализовать требуемый узел для построения таблицы сравнения. +## Installation -Редактирование ответа +```bash +# Clone the repository +git clone https://git.brojs.ru/kuzakhmetovartur/povtornyy-ekzamen-2-sravnitelnyy-obzor-3.git +cd povtornyy-ekzamen-2-sravnitelnyy-obzor-3 -Заполните ответ и отправьте работу на проверку преподавате \ No newline at end of file +# Install dependencies +npm install +``` + +## Configuration + +Create a `.env` file in the project root with your Tavily API key: + +```env +TAVILY_API_KEY=your_api_key_here +``` + +## Usage + +Run the application: + +```bash +npm start +``` + +You should see output similar to: + +``` +[OtherService] Starting search for: Comparison of three entities +[OtherService] Results: +1. Title of first result + Summary of the first result. + +2. Title of second result + Summary of the second result. + +3. Title of third result + Summary of the third result. +``` + +## Project Structure + +- `src/index.js` – Entry point that starts the application. +- `src/app.js` – Orchestrates the main flow. +- `src/services/tavily.js` – Wrapper around the Tavily SDK. +- `src/services/otherService.js` – Simple logging utility. +- `package.json` – Project metadata and dependencies. + +## Notes + +- The application uses the official `tavily-sdk` package. +- No additional external services are used beyond Tavily. +- The code is written in ES modules (`"type": "module"` in `package.json`). + +Feel free to extend the project with more sophisticated logic or additional services as needed. \ No newline at end of file diff --git a/package.json b/package.json index 5a632ba..19e8c66 100644 --- a/package.json +++ b/package.json @@ -1,33 +1,15 @@ { - "name": "comparison-table-app", + "name": "tavily-comparison", "version": "1.0.0", - "private": true, - "description": "React app that compares three entities using Tavily API", + "description": "Comparison of three entities using the Tavily API", "main": "src/index.js", + "type": "module", "scripts": { - "start": "react-scripts start", - "build": "react-scripts build", - "test": "react-scripts test", - "eject": "react-scripts eject" + "start": "node src/index.js", + "test": "echo \"No tests defined\"" }, "dependencies": { - "@tavily/tavily-api": "^1.0.0", - "axios": "^1.6.0", - "react": "^18.2.0", - "react-dom": "^18.2.0", - "react-scripts": "5.0.1", - "react-table": "^7.8.0" - }, - "browserslist": { - "production": [ - ">0.2%", - "not dead", - "not op_mini all" - ], - "development": [ - "last 1 chrome version", - "last 1 firefox version", - "last 1 safari version" - ] + "tavily-sdk": "^1.0.0", + "dotenv": "^16.0.0" } } \ No newline at end of file diff --git a/src/App.js b/src/App.js index 2845373..0baec97 100644 --- a/src/App.js +++ b/src/App.js @@ -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 ( -
-

Entity Comparison

- -
- ); -} - -export default App; \ No newline at end of file +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`); + }); +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index d0600e5..4dc475f 100644 --- a/src/index.js +++ b/src/index.js @@ -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( - - - -); \ No newline at end of file +run().catch((err) => { + console.error('Application error:', err); + process.exit(1); +}); \ No newline at end of file diff --git a/src/services/otherService.js b/src/services/otherService.js new file mode 100644 index 0000000..667d6ef --- /dev/null +++ b/src/services/otherService.js @@ -0,0 +1,3 @@ +export function logInfo(message) { + console.log(`[OtherService] ${message}`); +} \ No newline at end of file diff --git a/src/services/tavily.js b/src/services/tavily.js new file mode 100644 index 0000000..6e12ee9 --- /dev/null +++ b/src/services/tavily.js @@ -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; + } +} \ No newline at end of file