feat: solution for 'Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)'
This commit is contained in:
@@ -1,21 +1,66 @@
|
|||||||
# Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)
|
# Tavily Comparison Project
|
||||||
|
|
||||||
Главная
|
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.
|
||||||
Мои задания
|
|
||||||
Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)
|
|
||||||
5Д
|
|
||||||
EN
|
|
||||||
Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)
|
|
||||||
Зачёт
|
|
||||||
Версия 11
|
|
||||||
Дедлайн сдачи: 31.08.2026
|
|
||||||
|
|
||||||
В работе
|
## 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
|
||||||
|
|
||||||
Заполните ответ и отправьте работу на проверку преподавате
|
# 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.
|
||||||
+7
-25
@@ -1,33 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "comparison-table-app",
|
"name": "tavily-comparison",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"private": true,
|
"description": "Comparison of three entities using the Tavily API",
|
||||||
"description": "React app that compares three entities using Tavily API",
|
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "react-scripts start",
|
"start": "node src/index.js",
|
||||||
"build": "react-scripts build",
|
"test": "echo \"No tests defined\""
|
||||||
"test": "react-scripts test",
|
|
||||||
"eject": "react-scripts eject"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tavily/tavily-api": "^1.0.0",
|
"tavily-sdk": "^1.0.0",
|
||||||
"axios": "^1.6.0",
|
"dotenv": "^16.0.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"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+10
-12
@@ -1,14 +1,12 @@
|
|||||||
import React from 'react';
|
import { searchAndSummarize } from './services/tavily.js';
|
||||||
import ComparisonTable from './components/ComparisonTable';
|
import { logInfo } from './services/otherService.js';
|
||||||
import './App.css';
|
|
||||||
|
|
||||||
function App() {
|
export async function run() {
|
||||||
return (
|
const query = 'Comparison of three entities';
|
||||||
<div className="App">
|
logInfo(`Starting search for: ${query}`);
|
||||||
<h1>Entity Comparison</h1>
|
const results = await searchAndSummarize(query);
|
||||||
<ComparisonTable />
|
logInfo('Results:');
|
||||||
</div>
|
results.forEach((res, idx) => {
|
||||||
);
|
console.log(`${idx + 1}. ${res.title}\n ${res.summary}\n`);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App;
|
|
||||||
+5
-11
@@ -1,12 +1,6 @@
|
|||||||
import React from 'react';
|
import { run } from './app.js';
|
||||||
import { createRoot } from 'react-dom/client';
|
|
||||||
import App from './App';
|
|
||||||
import './index.css';
|
|
||||||
|
|
||||||
const container = document.getElementById('root');
|
run().catch((err) => {
|
||||||
const root = createRoot(container);
|
console.error('Application error:', err);
|
||||||
root.render(
|
process.exit(1);
|
||||||
<React.StrictMode>
|
});
|
||||||
<App />
|
|
||||||
</React.StrictMode>
|
|
||||||
);
|
|
||||||
@@ -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