feat: solution for 'Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)'
This commit is contained in:
@@ -7,13 +7,13 @@
|
|||||||
EN
|
EN
|
||||||
Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)
|
Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)
|
||||||
Зачёт
|
Зачёт
|
||||||
Версия 6
|
Версия 7
|
||||||
Дедлайн сдачи: 31.08.2026
|
Дедлайн сдачи: 31.08.2026
|
||||||
|
|
||||||
В работе
|
В работе
|
||||||
|
|
||||||
Требуется доработка
|
Требуется доработка
|
||||||
|
|
||||||
В вашем решении отсутствует упоминание и использование Qdrant, хотя это требование явно указано в задании. Пожалуйста, добавьте интеграцию с Qdrant или замените его на другой поддерживаемый вами векторный хранилище, чтобы решение соответствовало публичному стеку.
|
В представленном решении отсутствует интеграция с Qdrant, как требуется в публичном стеке задания. Кроме того, не реализовано требуемое сравнение в виде markdown‑таблицы и явный вердикт. Пожалуйста, доработайте эти части, чтобы решение соответствовало требованиям.
|
||||||
|
|
||||||
Редактиров
|
Редактиро
|
||||||
+4
-12
@@ -1,24 +1,16 @@
|
|||||||
{
|
{
|
||||||
"name": "tavily-qdrant-demo",
|
"name": "entity-comparison",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Demo project integrating Tavily API with Qdrant vector store",
|
"description": "Compare three entities using Tavily, OpenAI embeddings, and Qdrant",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
|
"type": "module",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node src/index.js"
|
"start": "node src/index.js"
|
||||||
},
|
},
|
||||||
"keywords": [
|
|
||||||
"tavily",
|
|
||||||
"qdrant",
|
|
||||||
"vector",
|
|
||||||
"search",
|
|
||||||
"express"
|
|
||||||
],
|
|
||||||
"author": "Your Name",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@qdrant/js-client-rest": "^1.0.0",
|
"@qdrant/js-client-rest": "^1.0.0",
|
||||||
"axios": "^1.7.2",
|
"axios": "^1.7.2",
|
||||||
"dotenv": "^16.4.5",
|
"dotenv": "^16.4.5",
|
||||||
"express": "^4.18.2"
|
"openai": "^4.21.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+88
-49
@@ -1,61 +1,100 @@
|
|||||||
const express = require('express');
|
import dotenv from "dotenv";
|
||||||
const { fetchEntityData } = require('./tavily');
|
import { fetchSummary } from "./tavily.js";
|
||||||
const {
|
import { QdrantWrapper } from "./qdrant.js";
|
||||||
initClient,
|
import { OpenAI } from "openai";
|
||||||
createCollection,
|
|
||||||
upsertEmbeddings,
|
|
||||||
searchEmbeddings
|
|
||||||
} = require('./qdrant');
|
|
||||||
require('dotenv').config();
|
|
||||||
|
|
||||||
const app = express();
|
dotenv.config();
|
||||||
const PORT = process.env.PORT || 3000;
|
|
||||||
|
|
||||||
// Middleware to parse JSON
|
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
|
||||||
app.use(express.json());
|
const openai = new OpenAI({ apiKey: OPENAI_API_KEY });
|
||||||
|
|
||||||
// Initialize Qdrant client and collection on startup
|
const entities = [
|
||||||
(async () => {
|
"Apple Inc.",
|
||||||
try {
|
"Microsoft Corporation",
|
||||||
initClient();
|
"Google LLC",
|
||||||
await createCollection();
|
];
|
||||||
} catch (err) {
|
|
||||||
console.error('Failed to initialize Qdrant:', err.message);
|
async function getEmbedding(text) {
|
||||||
process.exit(1);
|
const response = await openai.embeddings.create({
|
||||||
|
model: "text-embedding-3-small",
|
||||||
|
input: text,
|
||||||
|
});
|
||||||
|
return response.data[0].embedding;
|
||||||
|
}
|
||||||
|
|
||||||
|
function cosineSimilarity(vecA, vecB) {
|
||||||
|
const dot = vecA.reduce((sum, a, i) => sum + a * vecB[i], 0);
|
||||||
|
const normA = Math.sqrt(vecA.reduce((sum, a) => sum + a * a, 0));
|
||||||
|
const normB = Math.sqrt(vecB.reduce((sum, b) => sum + b * b, 0));
|
||||||
|
return dot / (normA * normB);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const qdrant = new QdrantWrapper();
|
||||||
|
await qdrant.createCollection();
|
||||||
|
|
||||||
|
const entityData = {};
|
||||||
|
|
||||||
|
// Fetch summaries, embeddings and upsert
|
||||||
|
for (const entity of entities) {
|
||||||
|
console.log(`Processing ${entity}...`);
|
||||||
|
const summary = await fetchSummary(entity);
|
||||||
|
const embedding = await getEmbedding(summary);
|
||||||
|
await qdrant.upsertEntity(entity, embedding, { name: entity, summary });
|
||||||
|
entityData[entity] = { summary, embedding };
|
||||||
}
|
}
|
||||||
})();
|
|
||||||
|
|
||||||
// Route to fetch data for an entity and store embeddings
|
// Compute pairwise similarities
|
||||||
app.get('/fetch/:entity', async (req, res) => {
|
const similarities = {};
|
||||||
const entity = req.params.entity;
|
for (const a of entities) {
|
||||||
try {
|
similarities[a] = {};
|
||||||
const text = await fetchEntityData(entity);
|
for (const b of entities) {
|
||||||
await upsertEmbeddings(entity, text);
|
if (a === b) continue;
|
||||||
res.json({ status: 'success', entity, textLength: text.length });
|
const sim = cosineSimilarity(
|
||||||
} catch (err) {
|
entityData[a].embedding,
|
||||||
res.status(500).json({ status: 'error', message: err.message });
|
entityData[b].embedding
|
||||||
|
);
|
||||||
|
similarities[a][b] = sim.toFixed(4);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// Route to search embeddings
|
// Generate markdown table
|
||||||
app.get('/search', async (req, res) => {
|
let markdown = "# Entity Comparison\n\n";
|
||||||
const query = req.query.q;
|
markdown += "| Entity | Summary | Similarity to Apple | Similarity to Microsoft | Similarity to Google |\n";
|
||||||
if (!query) {
|
markdown += "|--------|---------|---------------------|------------------------|---------------------|\n";
|
||||||
return res.status(400).json({ status: 'error', message: 'Missing query parameter q' });
|
|
||||||
|
for (const entity of entities) {
|
||||||
|
const row = [
|
||||||
|
entity,
|
||||||
|
`"${entityData[entity].summary.replace(/\n/g, " ")}"`,
|
||||||
|
similarities[entity]["Apple Inc."],
|
||||||
|
similarities[entity]["Microsoft Corporation"],
|
||||||
|
similarities[entity]["Google LLC"],
|
||||||
|
];
|
||||||
|
markdown += `| ${row.join(" | ")} |\n`;
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
const results = await searchEmbeddings(query);
|
// Verdict
|
||||||
res.json({ status: 'success', query, results });
|
let maxSim = -1;
|
||||||
} catch (err) {
|
let pair = [];
|
||||||
res.status(500).json({ status: 'error', message: err.message });
|
for (let i = 0; i < entities.length; i++) {
|
||||||
|
for (let j = i + 1; j < entities.length; j++) {
|
||||||
|
const a = entities[i];
|
||||||
|
const b = entities[j];
|
||||||
|
const sim = parseFloat(similarities[a][b]);
|
||||||
|
if (sim > maxSim) {
|
||||||
|
maxSim = sim;
|
||||||
|
pair = [a, b];
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// Health check
|
markdown += `\n**Verdict:** The entities with the highest similarity are **${pair[0]}** and **${pair[1]}** (similarity: ${maxSim.toFixed(4)}).\n`;
|
||||||
app.get('/', (req, res) => {
|
|
||||||
res.send('Tavily-Qdrant Demo Server');
|
|
||||||
});
|
|
||||||
|
|
||||||
app.listen(PORT, () => {
|
console.log(markdown);
|
||||||
console.log(`Server running on http://localhost:${PORT}`);
|
}
|
||||||
|
|
||||||
|
main().catch((err) => {
|
||||||
|
console.error(err);
|
||||||
|
process.exit(1);
|
||||||
});
|
});
|
||||||
+38
-101
@@ -1,116 +1,53 @@
|
|||||||
const { QdrantClient } = require('@qdrant/js-client-rest');
|
import { QdrantClient } from "@qdrant/js-client-rest";
|
||||||
const axios = require('axios');
|
import dotenv from "dotenv";
|
||||||
require('dotenv').config();
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
const QDRANT_URL = process.env.QDRANT_URL;
|
const QDRANT_URL = process.env.QDRANT_URL;
|
||||||
const QDRANT_API_KEY = process.env.QDRANT_API_KEY;
|
const QDRANT_API_KEY = process.env.QDRANT_API_KEY;
|
||||||
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
|
|
||||||
const VECTOR_SIZE = 1536; // OpenAI Ada embeddings size
|
|
||||||
|
|
||||||
const COLLECTION_NAME = 'entities';
|
export class QdrantWrapper {
|
||||||
|
constructor() {
|
||||||
let client = null;
|
this.client = new QdrantClient({
|
||||||
|
url: QDRANT_URL,
|
||||||
// Initialize Qdrant client
|
apiKey: QDRANT_API_KEY,
|
||||||
function initClient() {
|
|
||||||
client = new QdrantClient({
|
|
||||||
url: QDRANT_URL,
|
|
||||||
apiKey: QDRANT_API_KEY
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create or recreate collection
|
|
||||||
async function createCollection() {
|
|
||||||
if (!client) initClient();
|
|
||||||
try {
|
|
||||||
await client.recreateCollection(COLLECTION_NAME, {
|
|
||||||
vectors: {
|
|
||||||
size: VECTOR_SIZE,
|
|
||||||
distance: 'Cosine'
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
console.log(`Collection '${COLLECTION_NAME}' created/recreated.`);
|
this.collectionName = "entity_embeddings";
|
||||||
} catch (err) {
|
|
||||||
console.error('Error creating collection:', err.message);
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Get embedding from OpenAI
|
async createCollection() {
|
||||||
async function getEmbedding(text) {
|
try {
|
||||||
try {
|
await this.client.createCollection(this.collectionName, {
|
||||||
const response = await axios.post(
|
vectors: { size: 1536, distance: "Cosine" },
|
||||||
'https://api.openai.com/v1/embeddings',
|
});
|
||||||
{
|
console.log(`Collection ${this.collectionName} created.`);
|
||||||
input: text,
|
} catch (e) {
|
||||||
model: 'text-embedding-ada-002'
|
if (e.message.includes("already exists")) {
|
||||||
},
|
console.log(`Collection ${this.collectionName} already exists.`);
|
||||||
{
|
} else {
|
||||||
headers: {
|
throw e;
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: `Bearer ${OPENAI_API_KEY}`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
);
|
|
||||||
if (response.data && response.data.data && response.data.data[0]) {
|
|
||||||
return response.data.data[0].embedding;
|
|
||||||
} else {
|
|
||||||
throw new Error('No embedding returned');
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
|
||||||
console.error('Error getting embedding:', err.message);
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Upsert embeddings for an entity
|
async upsertEntity(id, vector, payload) {
|
||||||
async function upsertEmbeddings(entity, text) {
|
await this.client.upsert(this.collectionName, {
|
||||||
if (!client) initClient();
|
points: [
|
||||||
try {
|
{
|
||||||
const vector = await getEmbedding(text);
|
id,
|
||||||
const point = {
|
vector,
|
||||||
id: entity,
|
payload,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async searchNearest(vector, limit = 3) {
|
||||||
|
const result = await this.client.search(this.collectionName, {
|
||||||
vector,
|
vector,
|
||||||
payload: {
|
|
||||||
entity,
|
|
||||||
text
|
|
||||||
}
|
|
||||||
};
|
|
||||||
await client.upsertPoints(COLLECTION_NAME, {
|
|
||||||
points: [point]
|
|
||||||
});
|
|
||||||
console.log(`Upserted embeddings for entity '${entity}'.`);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error upserting embeddings:', err.message);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search embeddings
|
|
||||||
async function searchEmbeddings(query, limit = 3) {
|
|
||||||
if (!client) initClient();
|
|
||||||
try {
|
|
||||||
const queryVector = await getEmbedding(query);
|
|
||||||
const result = await client.search(COLLECTION_NAME, {
|
|
||||||
vector: queryVector,
|
|
||||||
limit,
|
limit,
|
||||||
with_payload: true,
|
withPayload: true,
|
||||||
with_vector: false
|
|
||||||
});
|
});
|
||||||
return result.hits.map((hit) => ({
|
return result;
|
||||||
id: hit.id,
|
|
||||||
score: hit.score,
|
|
||||||
payload: hit.payload
|
|
||||||
}));
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error searching embeddings:', err.message);
|
|
||||||
throw err;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
initClient,
|
|
||||||
createCollection,
|
|
||||||
upsertEmbeddings,
|
|
||||||
searchEmbeddings
|
|
||||||
};
|
|
||||||
+22
-27
@@ -1,41 +1,36 @@
|
|||||||
const axios = require('axios');
|
import axios from "axios";
|
||||||
require('dotenv').config();
|
import dotenv from "dotenv";
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
const TAVILY_API_KEY = process.env.TAVILY_API_KEY;
|
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 {
|
try {
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
TAVILY_ENDPOINT,
|
TAVILY_ENDPOINT,
|
||||||
{
|
{
|
||||||
|
api_key: TAVILY_API_KEY,
|
||||||
query: entity,
|
query: entity,
|
||||||
search_depth: 2,
|
search_depth: 2,
|
||||||
include_raw_content: true,
|
include_raw: true,
|
||||||
max_results: 5
|
|
||||||
},
|
},
|
||||||
{
|
{ headers: { "Content-Type": "application/json" } }
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'accept': 'application/json',
|
|
||||||
'Authorization': `Bearer ${TAVILY_API_KEY}`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
if (response.data && response.data.results) {
|
const results = response.data.results;
|
||||||
// Concatenate all raw content into a single string
|
if (!results || results.length === 0) {
|
||||||
const texts = response.data.results
|
return `No summary available for ${entity}.`;
|
||||||
.map((r) => r.raw_content || '')
|
|
||||||
.filter(Boolean);
|
|
||||||
return texts.join('\n\n');
|
|
||||||
} else {
|
|
||||||
throw new Error('No results returned from Tavily');
|
|
||||||
}
|
}
|
||||||
} 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}.`;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user