-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
574 lines (459 loc) · 18.7 KB
/
server.py
File metadata and controls
574 lines (459 loc) · 18.7 KB
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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#!/usr/bin/env python3
"""Ragscallion — Multi-corpus RAG HTTP server with async job queue."""
import asyncio
import json
import logging
import re
import sqlite3
import sys
import uuid
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional
from fastapi import FastAPI, HTTPException, UploadFile, File, Form, BackgroundTasks
from fastapi.responses import JSONResponse
import lancedb
from lancedb.rerankers import RRFReranker
from sentence_transformers import SentenceTransformer
import uvicorn
from ingest import process_job
# ─── Configuration ──────────────────────────────────────────────────────
DB_PATH = Path(__file__).parent / "vectordb"
METADATA_DB_PATH = Path(__file__).parent / "metadata.db"
DOCS_PATH = Path(__file__).parent / "docs"
EMBED_MODEL = "BAAI/bge-base-en-v1.5"
LEGACY_CORPUS_ID = "legacy" # Default corpus for backwards compatibility
# Logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def corpus_table_name(corpus_id: str) -> str:
"""Map corpus_id to LanceDB table name (alphanumeric safe)."""
# Replace hyphens and underscores with underscores, ensure alphanumeric
return f"corpus_{corpus_id.replace('-', '_')}"
# ─── Global State ───────────────────────────────────────────────────────
app = FastAPI(title="Ragscallion", version="0.2.0")
embedding_model: SentenceTransformer = None
reranker = RRFReranker()
# Job queue locks
MARKER_LOCK = asyncio.Lock() # Enforce 1 concurrent Marker process (16GB GPU limit)
INGEST_LOCK = asyncio.Lock() # Enforce 1 concurrent ingest process
MARKER_TIMEOUT_SECONDS = 600 # 10 minutes max per PDF
# Background job processor state
job_processor_task: Optional[asyncio.Task] = None
@app.on_event("startup")
async def startup():
"""Load embedding model at startup."""
global embedding_model, job_processor_task
logger.info("Loading embedding model...")
try:
embedding_model = SentenceTransformer(EMBED_MODEL, device="cuda")
logger.info("Embedding model loaded on GPU.")
except RuntimeError as e:
logger.warning(f"GPU not available ({e}), falling back to CPU...")
embedding_model = SentenceTransformer(EMBED_MODEL, device="cpu")
logger.info("Embedding model loaded on CPU.")
# Initialize metadata database
_init_metadata_db()
# Start background job processor
job_processor_task = asyncio.create_task(_run_job_processor())
logger.info("Job processor started")
@app.on_event("shutdown")
async def shutdown():
"""Cleanup on shutdown."""
global job_processor_task
if job_processor_task:
job_processor_task.cancel()
try:
await job_processor_task
except asyncio.CancelledError:
pass
# ─── Database Initialization ────────────────────────────────────────────
def _init_metadata_db():
"""Create metadata.db schema if not exists."""
conn = sqlite3.connect(str(METADATA_DB_PATH))
conn.execute("PRAGMA journal_mode=WAL") # Write-Ahead Logging for concurrent access
cursor = conn.cursor()
# Jobs table: tracks ingest jobs (queued → converting → ready/failed)
cursor.execute("""
CREATE TABLE IF NOT EXISTS jobs (
job_id TEXT PRIMARY KEY,
corpus_id TEXT NOT NULL,
source_label TEXT NOT NULL,
status TEXT DEFAULT 'queued',
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
started_at TEXT,
completed_at TEXT,
error_message TEXT,
chunks_indexed INTEGER DEFAULT 0,
UNIQUE(corpus_id, source_label)
)
""")
# Corpora table: tracks multi-corpus inventory
cursor.execute("""
CREATE TABLE IF NOT EXISTS corpora (
corpus_id TEXT PRIMARY KEY,
created_at TEXT NOT NULL,
source_count INTEGER DEFAULT 0,
chunk_count INTEGER DEFAULT 0
)
""")
# Indexes for fast polling and queries
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_jobs_status_updated
ON jobs(status, updated_at DESC)
""")
cursor.execute("""
CREATE INDEX IF NOT EXISTS idx_jobs_corpus
ON jobs(corpus_id)
""")
conn.commit()
conn.close()
logger.info("Metadata database initialized")
def _get_db():
"""Get LanceDB connection."""
return lancedb.connect(str(DB_PATH))
def _update_job_status(job_id: str, status: str, error_message: Optional[str] = None, chunks_indexed: int = 0):
"""Update job status in database."""
conn = sqlite3.connect(str(METADATA_DB_PATH))
cursor = conn.cursor()
now = datetime.now(timezone.utc).isoformat()
cursor.execute("""
UPDATE jobs
SET status = ?, updated_at = ?, error_message = ?, chunks_indexed = ?
WHERE job_id = ?
""", (status, now, error_message, chunks_indexed, job_id))
if status == "converting":
cursor.execute("UPDATE jobs SET started_at = ? WHERE job_id = ?", (now, job_id))
elif status in ("ready", "failed"):
cursor.execute("UPDATE jobs SET completed_at = ? WHERE job_id = ?", (now, job_id))
conn.commit()
conn.close()
async def _run_job_processor():
"""Background task: continuously process queued jobs."""
logger.info("Job processor loop started")
while True:
try:
# Poll for queued jobs
conn = sqlite3.connect(str(METADATA_DB_PATH))
cursor = conn.cursor()
cursor.execute("SELECT job_id FROM jobs WHERE status = 'queued' LIMIT 1")
row = cursor.fetchone()
conn.close()
if row:
job_id = row[0]
logger.info(f"Processing job {job_id}")
await process_job(
job_id,
metadata_db_path=METADATA_DB_PATH,
embedding_model=embedding_model,
get_db=_get_db,
corpus_table_name=corpus_table_name,
marker_lock=MARKER_LOCK,
ingest_lock=INGEST_LOCK,
)
else:
# No queued jobs, sleep briefly
await asyncio.sleep(1)
except Exception as e:
logger.error(f"Job processor error: {e}")
await asyncio.sleep(5) # Back off on errors
# ─── Health & Metadata Endpoints ────────────────────────────────────────
@app.get("/health")
async def health():
"""Health check endpoint."""
return {"status": "ok"}
@app.get("/stats")
async def stats():
"""Return index statistics (multi-corpus aware)."""
db = _get_db()
all_tables = db.list_tables().tables
if not all_tables:
return {"status": "no_index", "message": "Run ingestion first"}
stats_data = {}
total_chunks = 0
total_corpora = 0
for table_name in all_tables:
if not table_name.startswith("corpus_"):
continue
corpus_id = table_name.replace("corpus_", "").replace("_", "-")
try:
table = db.open_table(table_name)
df = table.to_pandas()
sources = df["source"].unique()
chunks = len(df)
stats_data[corpus_id] = {
"chunks": chunks,
"sources": len(sources),
"source_list": sorted(sources.tolist())
}
total_chunks += chunks
total_corpora += 1
except Exception as e:
logger.error(f"Error reading corpus {corpus_id}: {e}")
return {
"status": "ok",
"total_chunks": total_chunks,
"total_corpora": total_corpora,
"corpora": stats_data
}
@app.get("/sources")
async def sources(corpus: Optional[str] = None):
"""List all sources across corpora or within a specific corpus."""
db = _get_db()
all_tables = db.list_tables().tables
sources_by_corpus = {}
if corpus:
# Single corpus
table_name = corpus_table_name(corpus)
if table_name not in all_tables:
raise HTTPException(status_code=404, detail=f"Corpus '{corpus}' not found")
try:
table = db.open_table(table_name)
df = table.to_pandas()
sources_by_corpus[corpus] = sorted(df["source"].unique().tolist())
except Exception as e:
logger.error(f"Error reading corpus {corpus}: {e}")
sources_by_corpus[corpus] = []
else:
# All corpora
for table_name in all_tables:
if not table_name.startswith("corpus_"):
continue
corpus_id = table_name.replace("corpus_", "").replace("_", "-")
try:
table = db.open_table(table_name)
df = table.to_pandas()
sources_by_corpus[corpus_id] = sorted(df["source"].unique().tolist())
except Exception as e:
logger.error(f"Error reading corpus {corpus_id}: {e}")
sources_by_corpus[corpus_id] = []
return {"corpora": sources_by_corpus}
# ─── Search Endpoint ────────────────────────────────────────────────────
@app.get("/search")
async def search(q: str, n: int = 5, mode: str = "hybrid", corpus: Optional[str] = None):
"""
Search across corpus or specific corpus.
Parameters:
- q: search query
- n: number of results (default 5)
- mode: hybrid, vector, or fts (default hybrid)
- corpus: specific corpus to search (optional, search all if omitted)
"""
if not q:
raise HTTPException(status_code=400, detail="Missing ?q= parameter")
db = _get_db()
all_tables = db.list_tables().tables
if not all_tables:
raise HTTPException(status_code=500, detail="No index found. Run ingestion first.")
# Determine which corpus/tables to search
tables_to_search = {}
if corpus:
# Search specific corpus
table_name = corpus_table_name(corpus)
if table_name not in all_tables:
raise HTTPException(status_code=404, detail=f"Corpus '{corpus}' not found")
tables_to_search[corpus] = table_name
else:
# Search all corpora — map table names back to corpus IDs
for table_name in all_tables:
if table_name.startswith("corpus_"):
corpus_id = table_name.replace("corpus_", "").replace("_", "-")
tables_to_search[corpus_id] = table_name
results_by_corpus = {}
for corpus_id, table_name in tables_to_search.items():
try:
table = db.open_table(table_name)
if mode == "hybrid":
query_embedding = embedding_model.encode([q])[0].tolist()
results = (
table.search(query_type="hybrid")
.vector(query_embedding)
.text(q)
.rerank(reranker)
.limit(n)
)
elif mode == "vector":
query_embedding = embedding_model.encode([q])[0].tolist()
results = table.search(query_embedding).limit(n)
elif mode == "fts":
results = table.search(q, query_type="fts").limit(n)
else:
raise HTTPException(status_code=400, detail="mode must be hybrid, vector, or fts")
results_df = results.to_pandas()
results_list = []
for _, row in results_df.iterrows():
score_col = "_relevance_score" if "_relevance_score" in results_df.columns else "_distance"
score = row.get(score_col, 0)
page_info = f" p.{row.get('page', '')}" if row.get("page") else ""
results_list.append({
"source": row["source"],
"section": row.get("section", ""),
"page": row.get("page", ""),
"text": row["text"],
"score": float(score),
"score_type": score_col
})
results_by_corpus[corpus_id] = results_list
except Exception as e:
logger.error(f"Error searching corpus {corpus_id}: {e}")
results_by_corpus[corpus_id] = []
return {"query": q, "mode": mode, "results": results_by_corpus}
# ─── Job Submission Endpoint (Task 3) ───────────────────────────────────
@app.post("/ingest")
async def submit_ingest(
file: UploadFile = File(...),
corpus_id: str = Form(...),
source_label: str = Form(...),
on_conflict: str = Form("error")
):
"""
Submit PDF for async conversion and indexing.
Parameters:
- file: PDF file to ingest
- corpus_id: target corpus identifier (regex: ^[a-z0-9][a-z0-9_-]{0,63}$)
- source_label: human-readable label for this source
- on_conflict: error (reject if source exists) | append | replace
Returns:
- job_id: unique job identifier
- corpus_id: target corpus
- status: queued
- server_now: server's current time (RFC3339)
"""
# Validate file type
if not file.filename or not file.filename.lower().endswith(".pdf"):
raise HTTPException(status_code=400, detail="File must be a PDF")
# Validate corpus_id format
if not re.match(r"^[a-z0-9][a-z0-9_-]{0,63}$", corpus_id):
raise HTTPException(
status_code=400,
detail="corpus_id must match regex ^[a-z0-9][a-z0-9_-]{0,63}$"
)
# Validate on_conflict parameter
if on_conflict not in ("error", "append", "replace"):
raise HTTPException(
status_code=400,
detail="on_conflict must be error, append, or replace"
)
# Generate job ID
job_id = str(uuid.uuid4())
now = datetime.now(timezone.utc).isoformat()
# Save PDF to staging directory
staging_dir = Path(__file__).parent / "staging"
staging_dir.mkdir(exist_ok=True)
pdf_path = staging_dir / f"{job_id}.pdf"
try:
# Write uploaded file to disk
contents = await file.read()
pdf_path.write_bytes(contents)
logger.info(f"Saved PDF to {pdf_path} ({len(contents)} bytes)")
except Exception as e:
logger.error(f"Failed to save PDF: {e}")
raise HTTPException(status_code=500, detail="Failed to save uploaded file")
# Store job metadata
conn = sqlite3.connect(str(METADATA_DB_PATH))
cursor = conn.cursor()
try:
# Check for existing source_label based on on_conflict policy
cursor.execute(
"SELECT job_id, status FROM jobs WHERE corpus_id = ? AND source_label = ?",
(corpus_id, source_label)
)
existing = cursor.fetchone()
if existing and on_conflict == "error":
pdf_path.unlink() # Clean up the uploaded file
conn.close()
raise HTTPException(
status_code=409,
detail={
"error": "collision",
"message": f"source_label '{source_label}' already exists in corpus '{corpus_id}'"
}
)
# Insert new job
cursor.execute("""
INSERT INTO jobs
(job_id, corpus_id, source_label, status, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
""", (job_id, corpus_id, source_label, "queued", now, now))
# Ensure corpus exists
cursor.execute("""
INSERT OR IGNORE INTO corpora (corpus_id, created_at)
VALUES (?, ?)
""", (corpus_id, now))
conn.commit()
except sqlite3.IntegrityError as e:
pdf_path.unlink()
logger.error(f"Database error: {e}")
raise HTTPException(status_code=500, detail="Failed to queue job")
finally:
conn.close()
logger.info(f"Job {job_id} queued for corpus {corpus_id}, source {source_label}")
# Queue job for processing (background task — implemented in Task 5)
# For now, just return immediately
return JSONResponse(
status_code=202, # Accepted
content={
"job_id": job_id,
"corpus_id": corpus_id,
"status": "queued",
"server_now": datetime.now(timezone.utc).isoformat() + "Z"
}
)
# ─── Job Polling Endpoint (Task 4) ──────────────────────────────────────
@app.get("/jobs")
async def poll_jobs(
since: Optional[str] = None,
status: Optional[str] = None,
limit: int = 100
):
"""
Poll for completed jobs.
Parameters:
- since: RFC3339 timestamp — return jobs updated after this time
- status: comma-separated (ready,failed)
- limit: max results (default 100)
Returns:
- jobs: list of job objects
- server_now: server's current time (RFC3339) — use this for next poll
"""
conn = sqlite3.connect(str(METADATA_DB_PATH))
cursor = conn.cursor()
query = "SELECT job_id, corpus_id, source_label, status, created_at, updated_at, started_at, completed_at, error_message, chunks_indexed FROM jobs WHERE 1=1"
params = []
if since:
query += " AND updated_at > ?"
params.append(since)
if status:
statuses = [s.strip() for s in status.split(",")]
placeholders = ",".join(["?" for _ in statuses])
query += f" AND status IN ({placeholders})"
params.extend(statuses)
query += " ORDER BY updated_at DESC LIMIT ?"
params.append(limit)
cursor.execute(query, params)
rows = cursor.fetchall()
conn.close()
jobs = []
for row in rows:
jobs.append({
"job_id": row[0],
"corpus_id": row[1],
"source_label": row[2],
"status": row[3],
"created_at": row[4],
"updated_at": row[5],
"started_at": row[6],
"completed_at": row[7],
"error_message": row[8],
"chunks_indexed": row[9]
})
return {
"jobs": jobs,
"server_now": datetime.now(timezone.utc).isoformat() + "Z"
}
# ─── Main ───────────────────────────────────────────────────────────────
if __name__ == "__main__":
port = int(sys.argv[1]) if len(sys.argv) > 1 else 8080
logger.info(f"Ragscallion listening on 0.0.0.0:{port}")
uvicorn.run(app, host="0.0.0.0", port=port)