Spaces:
Runtime error

File size: 10,618 Bytes
64fb1e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93117c2
 
 
 
 
 
 
 
 
 
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
"""
/api/kb β€” Knowledge Base REST endpoints.

POST   /api/kb/upload               β€” single multipart upload
POST   /api/kb/upload-batch         β€” multiple files, one BG task each
GET    /api/kb/documents            β€” list all documents
DELETE /api/kb/documents/{doc_id}   β€” remove document + all chunks
GET    /api/kb/search?q=&n=5        β€” hybrid search with retrieval badges + RRF score
GET    /api/kb/status/{doc_id}      β€” chunk count + status
GET    /api/kb/chunks/{doc_id}      β€” sorted chunk list with token counts
GET    /api/kb/similarity/{doc_id}  β€” NΓ—N cosine similarity matrix
GET    /api/kb/questions/{doc_id}   β€” suggested questions from doc headings
"""
import os
import uuid
import logging
import tempfile
from pathlib import Path

from fastapi import APIRouter, UploadFile, File, HTTPException, BackgroundTasks, Form
from pydantic import BaseModel
from sqlalchemy import text

from database import AsyncSessionLocal
from knowledge import kb_manager, embeddings as embeddings_module

logger = logging.getLogger(__name__)
router = APIRouter(prefix="/api/kb", tags=["knowledge"])

ALLOWED_EXTENSIONS = {".pdf", ".docx", ".doc", ".txt", ".md"}
MAX_FILE_SIZE = 20 * 1024 * 1024  # 20 MB


# ── Pydantic models ───────────────────────────────────────────────────────────

class UploadResponse(BaseModel):
    doc_id: str
    filename: str
    status: str
    message: str


class DocumentInfo(BaseModel):
    doc_id: str
    filename: str
    file_type: str
    category: str
    chunk_count: int
    status: str


class SearchResult(BaseModel):
    content: str
    source: str
    relevance: float
    category: str
    retrieval: str = "dense"   # "dense" | "bm25" | "graph" | "hybrid"
    rrf_score: float = 0.0


# ── Shared ingest helper ──────────────────────────────────────────────────────

def _validate_file(filename: str, size: int) -> str:
    suffix = Path(filename).suffix.lower()
    if suffix not in ALLOWED_EXTENSIONS:
        raise HTTPException(
            status_code=400,
            detail=f"Unsupported file type '{suffix}'. Allowed: {', '.join(ALLOWED_EXTENSIONS)}",
        )
    if size > MAX_FILE_SIZE:
        raise HTTPException(status_code=413, detail="File too large (max 20 MB)")
    return suffix


async def _accept_file(
    file: UploadFile,
    category: str,
    background_tasks: BackgroundTasks,
) -> UploadResponse:
    filename = file.filename or "unknown"
    contents = await file.read()
    suffix = _validate_file(filename, len(contents))
    doc_id = str(uuid.uuid4())

    with tempfile.NamedTemporaryFile(suffix=suffix, delete=False) as tmp:
        tmp.write(contents)
        tmp_path = tmp.name

    try:
        async with AsyncSessionLocal() as db:
            await db.execute(
                text("""INSERT INTO kb_documents (id, filename, file_type, category, status)
                         VALUES (:id, :fn, :ft, :cat, 'processing')"""),
                {"id": uuid.UUID(doc_id), "fn": filename, "ft": suffix.lstrip("."), "cat": category},
            )
            await db.commit()
    except Exception as exc:
        os.unlink(tmp_path)
        logger.error(f"KB DB insert failed: {exc}")
        raise HTTPException(status_code=500, detail="Database error")

    background_tasks.add_task(
        _ingest_background, tmp_path, doc_id, filename, suffix.lstrip("."), category
    )
    return UploadResponse(
        doc_id=doc_id,
        filename=filename,
        status="processing",
        message="Document accepted. Indexing in background.",
    )


async def _ingest_background(
    tmp_path: str,
    doc_id: str,
    filename: str,
    file_type: str,
    category: str,
) -> None:
    try:
        _, chunk_count = await kb_manager.ingest_document(
            file_path=tmp_path,
            filename=filename,
            file_type=file_type,
            category=category,
            doc_id=doc_id,
        )
        async with AsyncSessionLocal() as db:
            await db.execute(
                text("UPDATE kb_documents SET status='ready', chunk_count=:cc WHERE id=:id"),
                {"cc": chunk_count, "id": uuid.UUID(doc_id)},
            )
            await db.commit()
        logger.info(f"KB doc {doc_id} ready with {chunk_count} chunks")
    except Exception as exc:
        logger.error(f"Ingestion failed for {doc_id}: {exc}")
        try:
            async with AsyncSessionLocal() as db:
                await db.execute(
                    text("UPDATE kb_documents SET status='error' WHERE id=:id"),
                    {"id": uuid.UUID(doc_id)},
                )
                await db.commit()
        except Exception:
            pass
    finally:
        try:
            os.unlink(tmp_path)
        except Exception:
            pass


# ── Upload (single) ───────────────────────────────────────────────────────────

@router.post("/upload", response_model=UploadResponse)
async def upload_document(
    background_tasks: BackgroundTasks,
    file: UploadFile = File(...),
    category: str = Form(default="general"),
):
    return await _accept_file(file, category, background_tasks)


# ── Upload (batch) ────────────────────────────────────────────────────────────

@router.post("/upload-batch", response_model=list[UploadResponse])
async def upload_batch(
    background_tasks: BackgroundTasks,
    files: list[UploadFile] = File(...),
    category: str = Form(default="general"),
):
    responses = []
    for file in files:
        resp = await _accept_file(file, category, background_tasks)
        responses.append(resp)
    return responses


# ── List documents ────────────────────────────────────────────────────────────

@router.get("/documents", response_model=list[DocumentInfo])
async def list_documents():
    async with AsyncSessionLocal() as db:
        result = await db.execute(
            text("SELECT id, filename, file_type, category, chunk_count, status "
                 "FROM kb_documents ORDER BY uploaded_at DESC")
        )
        rows = result.mappings().all()

    return [
        DocumentInfo(
            doc_id=str(row["id"]),
            filename=row["filename"],
            file_type=row["file_type"] or "",
            category=row["category"] or "general",
            chunk_count=row["chunk_count"] or 0,
            status=row["status"] or "unknown",
        )
        for row in rows
    ]


# ── Delete ────────────────────────────────────────────────────────────────────

@router.delete("/documents/{doc_id}")
async def delete_document(doc_id: str):
    deleted = await kb_manager.delete_document(doc_id)
    async with AsyncSessionLocal() as db:
        await db.execute(
            text("DELETE FROM kb_documents WHERE id=:id"),
            {"id": uuid.UUID(doc_id)},
        )
        await db.commit()
    return {"deleted": True, "chunks_removed": deleted, "doc_id": doc_id}


# ── Search (hybrid) ───────────────────────────────────────────────────────────

@router.get("/search", response_model=list[SearchResult])
async def search_kb(q: str, n: int = 3):
    if not q.strip():
        raise HTTPException(status_code=400, detail="Query cannot be empty")

    hits = await kb_manager.search_kb(q.strip(), n_results=min(n, 10))
    return [
        SearchResult(
            content=h["content"],
            source=h["source"],
            relevance=h.get("relevance", h.get("rrf_score", 0.0)),
            category=h.get("category", "general"),
            retrieval=h.get("retrieval", "dense"),
            rrf_score=h.get("rrf_score", 0.0),
        )
        for h in hits
    ]


# ── Status ────────────────────────────────────────────────────────────────────

@router.get("/status/{doc_id}")
async def document_status(doc_id: str):
    async with AsyncSessionLocal() as db:
        result = await db.execute(
            text("SELECT filename, status, chunk_count FROM kb_documents WHERE id=:id"),
            {"id": uuid.UUID(doc_id)},
        )
        row = result.mappings().first()

    if not row:
        raise HTTPException(status_code=404, detail="Document not found")

    return {
        "doc_id": doc_id,
        "filename": row["filename"],
        "status": row["status"],
        "chunk_count": row["chunk_count"] or 0,
    }


# ── Inspector endpoints ───────────────────────────────────────────────────────

@router.get("/chunks/{doc_id}")
async def get_chunks(doc_id: str):
    chunks = kb_manager.get_doc_chunks(doc_id)
    if not chunks:
        # Verify the doc exists in DB before returning 404
        async with AsyncSessionLocal() as db:
            result = await db.execute(
                text("SELECT id FROM kb_documents WHERE id=:id"),
                {"id": uuid.UUID(doc_id)},
            )
            if not result.first():
                raise HTTPException(status_code=404, detail="Document not found")
    return chunks


@router.get("/similarity/{doc_id}")
async def get_similarity(doc_id: str):
    return kb_manager.get_chunk_similarity(doc_id)


@router.get("/questions/{doc_id}")
async def get_questions(doc_id: str):
    return kb_manager.get_suggested_questions(doc_id)


@router.post("/rebuild-index")
async def rebuild_index():
    """Rebuild BM25 + entity indexes in-process without restarting the server."""
    kb_manager._rebuild_indexes()
    ei = len(kb_manager._entity_index)
    ep = len(kb_manager._entity_pair_index)
    chunks = len(kb_manager._bm25_corpus)
    return {"chunks": chunks, "entities": ei, "entity_pairs": ep}