From 044f34951322cba913375490bcbfb420395ee235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=BCneyt=20=C5=9Eahin?= Date: Thu, 19 Feb 2026 10:29:29 +0300 Subject: [PATCH] . --- backend/src/config/env.ts | 2 - backend/src/services/document_rag.service.ts | 8 +-- backend/src/services/llm_backend.service.ts | 55 +++++++------------ .../auth/data/api_auth_repository.dart | 8 +-- .../presentation/widgets/procedural_tree.dart | 1 - lib/shared/data/api_document_repository.dart | 9 ++- lib/shared/models/models.dart | 12 +++- lib/shared/services/api_service.dart | 5 +- llm_backend/app/api/chat.py | 4 -- scripts/dev_start.sh | 2 - 10 files changed, 47 insertions(+), 59 deletions(-) diff --git a/backend/src/config/env.ts b/backend/src/config/env.ts index 35be8ac..27dfe5a 100644 --- a/backend/src/config/env.ts +++ b/backend/src/config/env.ts @@ -8,7 +8,6 @@ interface EnvConfig { PORT: number; DATABASE_URL: string; LLM_BACKEND_URL: string; - OLLAMA_EMBEDDINGS_URL: string | null; EMBEDDING_MODEL: string; } @@ -34,7 +33,6 @@ function validateEnv(): EnvConfig { PORT: port, DATABASE_URL: databaseUrl, LLM_BACKEND_URL: process.env.LLM_BACKEND_URL || 'http://localhost:8000', - OLLAMA_EMBEDDINGS_URL: process.env.OLLAMA_EMBEDDINGS_URL || null, EMBEDDING_MODEL: process.env.EMBEDDING_MODEL || 'nomic-embed-text', }; } diff --git a/backend/src/services/document_rag.service.ts b/backend/src/services/document_rag.service.ts index 7640c64..56edc69 100644 --- a/backend/src/services/document_rag.service.ts +++ b/backend/src/services/document_rag.service.ts @@ -64,9 +64,9 @@ export async function indexDocument(params: { await pool.query('DELETE FROM document_chunks WHERE document_id = $1', [params.documentId]); await pool.query( `UPDATE documents - SET status = 'processing', total_chunks = $2, processing_progress = 0.00, error_message = NULL, summary = $3, content_text = $4 + SET status = 'processing', total_chunks = $2, processing_progress = 0.00, error_message = NULL, summary = $3 WHERE id = $1`, - [params.documentId, totalChunks, summary, text] + [params.documentId, totalChunks, summary] ); let processed = 0; @@ -136,9 +136,9 @@ export async function indexDocument(params: { await pool.query( `UPDATE documents - SET status = 'ready', total_chunks = $2, indexed_at = NOW(), error_message = NULL, processing_progress = 1.00, summary = $3, content_text = $4 + SET status = 'ready', total_chunks = $2, indexed_at = NOW(), error_message = NULL, processing_progress = 1.00, summary = $3 WHERE id = $1`, - [params.documentId, totalChunks, summary, text] + [params.documentId, totalChunks, summary] ); } catch (e) { await markDocumentFailed(params.documentId, 'Doküman işlenirken hata oluştu.'); diff --git a/backend/src/services/llm_backend.service.ts b/backend/src/services/llm_backend.service.ts index c55357d..b288e2f 100644 --- a/backend/src/services/llm_backend.service.ts +++ b/backend/src/services/llm_backend.service.ts @@ -5,10 +5,6 @@ interface EmbeddingResponse { embeddings: number[][]; } -interface OllamaEmbeddingResponse { - embedding: number[]; -} - interface RagAnswerResponse { answer: string; } @@ -18,41 +14,28 @@ interface ChatResponse { } export async function embedTexts(texts: string[]): Promise { - if (!config.OLLAMA_EMBEDDINGS_URL) { - throw new Error('Ollama embeddings endpoint is not configured'); - } - - const embeddings: number[][] = []; - for (const text of texts) { - let lastError: Error | null = null; - for (let attempt = 1; attempt <= 3; attempt += 1) { - try { - const response = await fetch(config.OLLAMA_EMBEDDINGS_URL, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ model: config.EMBEDDING_MODEL, prompt: text }), - }); - if (!response.ok) { - const body = await response.text(); - throw new Error(`Ollama embedding error: ${response.status} ${body}`); - } - const data = (await response.json()) as OllamaEmbeddingResponse; - embeddings.push(data.embedding); - lastError = null; - break; - } catch (error) { - lastError = error instanceof Error ? error : new Error('Ollama embedding failed'); - if (attempt < 3) { - await new Promise((resolve) => setTimeout(resolve, 500 * attempt)); - } + let lastError: Error | null = null; + for (let attempt = 1; attempt <= 3; attempt += 1) { + try { + const response = await fetch(`${config.LLM_BACKEND_URL}/rag/embeddings`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ texts }), + }); + if (!response.ok) { + const body = await response.text(); + throw new Error(`Embedding error: ${response.status} ${body}`); + } + const data = (await response.json()) as EmbeddingResponse; + return data.embeddings; + } catch (error) { + lastError = error instanceof Error ? error : new Error('Embedding request failed'); + if (attempt < 3) { + await new Promise((resolve) => setTimeout(resolve, 500 * attempt)); } - } - if (lastError) { - throw lastError; } } - - return embeddings; + throw lastError!; } export async function answerWithContext(question: string, context: string): Promise { diff --git a/lib/features/auth/data/api_auth_repository.dart b/lib/features/auth/data/api_auth_repository.dart index 119dce6..e18ae9c 100644 --- a/lib/features/auth/data/api_auth_repository.dart +++ b/lib/features/auth/data/api_auth_repository.dart @@ -100,10 +100,10 @@ class ApiAuthRepository implements AuthRepository { print('✅ Current user: ${user['email']}'); return AuthUser( - id: user['id'] as String, - email: user['email'] as String, - displayName: user['displayName'] as String? ?? 'User', - isGuest: user['isGuest'] as bool? ?? false, + id: (user['id'] as String?) ?? '', + email: (user['email'] as String?) ?? '', + displayName: (user['displayName'] as String?) ?? 'User', + isGuest: (user['isGuest'] as bool?) ?? false, ); } catch (e) { print('❌ Get current user failed: $e'); diff --git a/lib/features/home/presentation/widgets/procedural_tree.dart b/lib/features/home/presentation/widgets/procedural_tree.dart index efc4e68..fe666b1 100644 --- a/lib/features/home/presentation/widgets/procedural_tree.dart +++ b/lib/features/home/presentation/widgets/procedural_tree.dart @@ -140,7 +140,6 @@ class _TreePainter extends CustomPainter { // Recursive branches if (currentDepth > 0) { - const int branches = 2; // Split into 2 final double spreadAngle = 0.6 + (currentLevel * 0.04).clamp(0.0, 0.4); // Wider spread diff --git a/lib/shared/data/api_document_repository.dart b/lib/shared/data/api_document_repository.dart index 723a4e7..32affb5 100644 --- a/lib/shared/data/api_document_repository.dart +++ b/lib/shared/data/api_document_repository.dart @@ -15,12 +15,13 @@ class ApiDocumentRepository { Future> getDocuments() async { try { final response = await _dio.get>('/documents'); + print('📄 getDocuments response: ${response.data}'); final data = response.data!['documents'] as List; return data .map((json) => Document.fromJson(json as Map)) .toList(); } catch (e) { - // Return empty list on error for now or rethrow + print('❌ getDocuments error: $e'); rethrow; } } @@ -39,10 +40,13 @@ class ApiDocumentRepository { data: formData, ); + print('📄 Upload response: ${response.data}'); + return Document.fromJson( response.data!['document'] as Map, ); } catch (e) { + print('❌ Upload error: $e'); rethrow; } } @@ -63,10 +67,13 @@ class ApiDocumentRepository { data: formData, ); + print('📄 Upload bytes response: ${response.data}'); + return Document.fromJson( response.data!['document'] as Map, ); } catch (e) { + print('❌ Upload bytes error: $e'); rethrow; } } diff --git a/lib/shared/models/models.dart b/lib/shared/models/models.dart index 67caa53..eff82a6 100644 --- a/lib/shared/models/models.dart +++ b/lib/shared/models/models.dart @@ -150,12 +150,18 @@ class Document extends Equatable { final progressRaw = json['processing_progress']; final progress = progressRaw is num ? progressRaw.toDouble() : 0.0; + DateTime? uploadedAt; + final uploadedAtRaw = json['uploaded_at']; + if (uploadedAtRaw is String) { + uploadedAt = DateTime.tryParse(uploadedAtRaw); + } + return Document( id: json['id'] as String?, - title: json['title'] as String, - summary: json['summary'] as String? ?? '', + title: (json['title'] as String?) ?? 'Untitled', + summary: (json['summary'] as String?) ?? '', status: status, - uploadedAt: DateTime.parse(json['uploaded_at'] as String), + uploadedAt: uploadedAt, processingProgress: progress, ); } diff --git a/lib/shared/services/api_service.dart b/lib/shared/services/api_service.dart index 9a72ba7..7931dc9 100644 --- a/lib/shared/services/api_service.dart +++ b/lib/shared/services/api_service.dart @@ -39,8 +39,9 @@ class ApiService { _dio = Dio( BaseOptions( baseUrl: baseUrl, - connectTimeout: const Duration(seconds: 15), - receiveTimeout: const Duration(seconds: 15), + connectTimeout: const Duration(seconds: 30), + receiveTimeout: const Duration(seconds: 60), + sendTimeout: const Duration(seconds: 60), ), ); print('🔌 ApiService Initialized'); diff --git a/llm_backend/app/api/chat.py b/llm_backend/app/api/chat.py index f044d49..93a1b85 100644 --- a/llm_backend/app/api/chat.py +++ b/llm_backend/app/api/chat.py @@ -27,7 +27,3 @@ def chat(req: ChatRequest): except Exception as e: logger.error(f"Chat error: {str(e)}") raise HTTPException(status_code=500, detail="LLM error") - - except Exception as e: - logger.error(str(e)) - raise HTTPException(status_code=500, detail="LLM error") diff --git a/scripts/dev_start.sh b/scripts/dev_start.sh index b40bcc5..b7f173b 100755 --- a/scripts/dev_start.sh +++ b/scripts/dev_start.sh @@ -57,8 +57,6 @@ start_process "llm_backend" "cd '$ROOT/llm_backend' && \ uvicorn app.main:app --host 0.0.0.0 --port 8000" start_process "backend" "cd '$ROOT/backend' && \ export NODE_ENV='development' && \ - export OLLAMA_EMBEDDINGS_URL='http://127.0.0.1:11434/api/embeddings' && \ - export EMBEDDING_MODEL='nomic-embed-text' && \ npm run start" echo "All services started. Logs: $LOG_DIR"