File size: 14,986 Bytes
10ac46e c79a43d 10ac46e f36d1f9 10ac46e f36d1f9 10ac46e c79a43d 10ac46e c79a43d 10ac46e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
import fs from 'fs';
import path from 'path';
import { modalClient } from './modal-client';
import { nebiusClient } from './nebius-client';
import { FileProcessor } from './file-upload';
import { storage } from './storage';
import { type Document, type InsertDocument } from '@shared/schema';
export interface ProcessingResult {
success: boolean;
extractedText?: string;
embeddings?: number[];
modalTaskId?: string;
error?: string;
processingTime: number;
}
export interface BatchProcessingResult {
success: boolean;
processedCount: number;
failedCount: number;
results: Array<{
documentId: number;
success: boolean;
extractedText?: string;
embeddings?: number[];
error?: string;
}>;
totalProcessingTime: number;
}
export class DocumentProcessor {
private static instance: DocumentProcessor;
static getInstance(): DocumentProcessor {
if (!DocumentProcessor.instance) {
DocumentProcessor.instance = new DocumentProcessor();
}
return DocumentProcessor.instance;
}
/**
* Process a single document using Modal for heavy workloads
*/
async processDocument(
document: Document,
operations: Array<'extract_text' | 'generate_embedding' | 'build_index'> = ['extract_text']
): Promise<ProcessingResult> {
const startTime = Date.now();
try {
let extractedText = document.content;
let embeddings: number[] | undefined;
let modalTaskId: string | undefined;
// Step 1: Extract text if needed (for PDFs and images)
if (operations.includes('extract_text') && document.filePath) {
const textResult = await this.extractText(document);
if (textResult.success) {
extractedText = textResult.extractedText || document.content;
modalTaskId = textResult.modalTaskId;
} else {
console.warn(`Text extraction failed for document ${document.id}: ${textResult.error}`);
}
}
// Step 2: Generate embeddings if requested
if (operations.includes('generate_embedding') && extractedText) {
const embeddingResult = await this.generateEmbeddings(extractedText);
if (embeddingResult.success) {
embeddings = embeddingResult.embeddings;
} else {
console.warn(`Embedding generation failed for document ${document.id}: ${embeddingResult.error}`);
}
}
const processingTime = Date.now() - startTime;
return {
success: true,
extractedText,
embeddings,
modalTaskId,
processingTime
};
} catch (error) {
const processingTime = Date.now() - startTime;
return {
success: false,
error: error instanceof Error ? error.message : String(error),
processingTime
};
}
}
/**
* Process multiple documents in batch using Modal's distributed computing
*/
async batchProcessDocuments(
documents: Document[],
operations: Array<'extract_text' | 'generate_embedding' | 'build_index'> = ['extract_text']
): Promise<BatchProcessingResult> {
const startTime = Date.now();
const results: BatchProcessingResult['results'] = [];
try {
// Separate documents by processing requirements
const documentsForModal = documents.filter(doc =>
doc.filePath && FileProcessor.requiresOCR(doc.mimeType || '')
);
const documentsForLocal = documents.filter(doc =>
!doc.filePath || !FileProcessor.requiresOCR(doc.mimeType || '')
);
// Process Modal-required documents in batch
if (documentsForModal.length > 0 && operations.includes('extract_text')) {
try {
const modalResults = await this.batchExtractTextModal(documentsForModal);
results.push(...modalResults);
} catch (error) {
console.error('Modal batch processing failed:', error);
// Fall back to individual processing
for (const doc of documentsForModal) {
const result = await this.processDocument(doc, operations);
results.push({
documentId: doc.id,
success: result.success,
extractedText: result.extractedText,
embeddings: result.embeddings,
error: result.error
});
}
}
}
// Process local documents
for (const doc of documentsForLocal) {
const result = await this.processDocument(doc, operations);
results.push({
documentId: doc.id,
success: result.success,
extractedText: result.extractedText,
embeddings: result.embeddings,
error: result.error
});
}
const totalProcessingTime = Date.now() - startTime;
const successCount = results.filter(r => r.success).length;
const failedCount = results.length - successCount;
return {
success: true,
processedCount: successCount,
failedCount,
results,
totalProcessingTime
};
} catch (error) {
const totalProcessingTime = Date.now() - startTime;
return {
success: false,
processedCount: 0,
failedCount: documents.length,
results: documents.map(doc => ({
documentId: doc.id,
success: false,
error: error instanceof Error ? error.message : String(error)
})),
totalProcessingTime
};
}
}
/**
* Extract text from a document using Modal for PDFs/images or direct reading for text files
*/
private async extractText(document: Document): Promise<{
success: boolean;
extractedText?: string;
modalTaskId?: string;
error?: string;
}> {
if (!document.filePath) {
return { success: true, extractedText: document.content };
}
const mimeType = document.mimeType || '';
try {
// For text files, read directly
if (FileProcessor.isTextFile(mimeType)) {
const content = await FileProcessor.readTextFile(document.filePath);
return { success: true, extractedText: content };
}
// For PDFs and images, use Modal
if (FileProcessor.requiresOCR(mimeType)) {
return await this.extractTextModal(document);
}
// Fallback: return existing content
return { success: true, extractedText: document.content };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : String(error)
};
}
}
/**
* Extract text using Modal for OCR-required files
*/
private async extractTextModal(document: Document): Promise<{
success: boolean;
extractedText?: string;
modalTaskId?: string;
error?: string;
}> {
try {
if (!document.filePath) {
throw new Error('No file path provided for Modal processing');
}
// Read file and convert to base64
const fileBuffer = await fs.promises.readFile(document.filePath);
const base64Content = fileBuffer.toString('base64');
// Prepare document for Modal
const modalDocument = {
id: document.id.toString(),
content: base64Content,
contentType: document.mimeType || 'application/octet-stream'
};
// Call Modal extract-text endpoint
const result = await modalClient.extractTextFromDocuments([modalDocument]);
if (result.status === 'completed' && result.results?.length > 0) {
const extractionResult = result.results[0];
if (extractionResult.status === 'completed') {
return {
success: true,
extractedText: extractionResult.extracted_text,
modalTaskId: result.task_id
};
} else {
return {
success: false,
error: extractionResult.error || 'Modal extraction failed'
};
}
} else {
return {
success: false,
error: result.error || 'Modal processing failed'
};
}
} catch (error) {
console.error('Modal text extraction failed:', error);
return {
success: false,
error: error instanceof Error ? error.message : String(error)
};
}
}
/**
* Batch extract text using Modal
*/
private async batchExtractTextModal(documents: Document[]): Promise<Array<{
documentId: number;
success: boolean;
extractedText?: string;
error?: string;
}>> {
const modalDocuments = await Promise.all(
documents.map(async (doc) => {
if (!doc.filePath) return null;
try {
const fileBuffer = await fs.promises.readFile(doc.filePath);
return {
id: doc.id.toString(),
content: fileBuffer.toString('base64'),
contentType: doc.mimeType || 'application/octet-stream'
};
} catch (error) {
console.error(`Failed to read file for document ${doc.id}:`, error);
return null;
}
})
);
const validDocuments = modalDocuments.filter(doc => doc !== null) as any[];
if (validDocuments.length === 0) {
return documents.map(doc => ({
documentId: doc.id,
success: false,
error: 'No valid documents for processing'
}));
}
try {
const batchResult = await modalClient.batchProcessDocuments({
documents: validDocuments,
modelName: 'text-embedding-3-small',
batchSize: Math.min(validDocuments.length, 10)
});
if (batchResult.status === 'completed' && batchResult.extraction_results) {
return batchResult.extraction_results.map((result: any) => ({
documentId: parseInt(result.id),
success: result.status === 'completed',
extractedText: result.extracted_text,
error: result.error
}));
} else {
throw new Error(batchResult.error || 'Batch processing failed');
}
} catch (error) {
console.error('Modal batch processing failed:', error);
return documents.map(doc => ({
documentId: doc.id,
success: false,
error: error instanceof Error ? error.message : String(error)
}));
}
}
/**
* Generate embeddings using Nebius AI
*/
private async generateEmbeddings(text: string): Promise<{
success: boolean;
embeddings?: number[];
error?: string;
}> {
try {
// Truncate text if too long (most embedding models have token limits)
const maxLength = 8000; // Conservative limit
const truncatedText = text.length > maxLength ? text.substring(0, maxLength) : text;
const result = await nebiusClient.generateEmbeddings(truncatedText);
if (result.success && result.embeddings) {
return {
success: true,
embeddings: result.embeddings
};
} else {
return {
success: false,
error: result.error || 'Embedding generation failed'
};
}
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : String(error)
};
}
}
/**
* Build vector index using Modal
*/
async buildVectorIndex(
documents: Document[],
indexName = 'research_papers_clean_v2'
): Promise<{
success: boolean;
indexName?: string;
documentCount?: number;
error?: string;
}> {
try {
const modalDocuments = documents.map(doc => ({
id: doc.id.toString(),
content: doc.content,
title: doc.title,
source: doc.source
}));
const result = await modalClient.buildVectorIndex(modalDocuments, {
indexName,
dimension: 1536, // Standard OpenAI embedding dimension
indexType: 'IVF',
nlist: Math.min(100, Math.max(10, Math.floor(documents.length / 10)))
});
if (result.status === 'completed') {
return {
success: true,
indexName: result.index_name,
documentCount: result.document_count
};
} else {
return {
success: false,
error: result.error || 'Index building failed'
};
}
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : String(error)
};
}
}
/**
* Search vector index using Modal
*/
async searchVectorIndex(
query: string,
indexName = 'research_papers_clean_v2',
maxResults = 10
): Promise<{
success: boolean;
results?: Array<{
id: string;
title: string;
content: string;
source: string;
relevanceScore: number;
rank: number;
snippet: string;
}>;
error?: string;
}> {
try {
const result = await modalClient.vectorSearch(query, indexName, maxResults);
if (result.status === 'completed') {
// Enrich vector search results with complete document data from database
const enrichedResults = await Promise.all(
result.results.map(async (vectorResult: any) => {
try {
// Get complete document data from database using the ID
const dbDocument = await storage.getDocument(parseInt(vectorResult.id));
if (dbDocument) {
// Merge vector search metadata with database document
// Ensure the URL field is preserved from the database
const enriched = {
id: dbDocument.id,
title: dbDocument.title,
content: dbDocument.content,
source: dbDocument.source,
sourceType: dbDocument.sourceType,
url: dbDocument.url, // Explicitly preserve URL
metadata: dbDocument.metadata,
createdAt: dbDocument.createdAt,
// Add vector search specific fields
relevanceScore: vectorResult.relevanceScore,
rank: vectorResult.rank,
snippet: vectorResult.snippet || dbDocument.content.substring(0, 200) + '...'
};
return enriched;
} else {
// Fallback to vector result if database document not found
return vectorResult;
}
} catch (error) {
console.warn(`Failed to enrich vector result for ID ${vectorResult.id}:`, error);
return vectorResult;
}
})
);
return {
success: true,
results: enrichedResults
};
} else {
return {
success: false,
error: result.error || 'Vector search failed'
};
}
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : String(error)
};
}
}
}
export const documentProcessor = DocumentProcessor.getInstance(); |