LeomordKaly commited on
Commit
f7d7b5d
·
verified ·
1 Parent(s): a8ef23e

deploy: phase 3 BYOK backend (Dockerfile.hf, FastAPI on 7860)

Browse files
Dockerfile.hf CHANGED
@@ -123,8 +123,11 @@ ENV SAR_BM25_INDEX_PATH=/tmp/secureagentrag/bm25_index.pkl
123
  # Multi-tenant collections route BYOK session -> documents_sess_<sid>.
124
  ENV SAR_MULTI_TENANT_COLLECTIONS=true
125
 
126
- # Pipeline safety
127
- ENV SAR_REQUEST_TIMEOUT_S=120
 
 
 
128
  ENV SAR_FAITHFULNESS_GATE_ENABLED=true
129
  # `flag` keeps the answer text intact and lets the UI render the
130
  # faithfulness badge. `drop` would rewrite sentences out of the answer
 
123
  # Multi-tenant collections route BYOK session -> documents_sess_<sid>.
124
  ENV SAR_MULTI_TENANT_COLLECTIONS=true
125
 
126
+ # Pipeline safety -- BYOK uploads can push the candidate set up to 20
127
+ # chunks (top_k=10 from base + top_k=10 from session) which the grader
128
+ # scores one Groq call at a time. 180 s leaves headroom for the
129
+ # reranker cold load + Groq free-tier rate-limit waits on a fresh boot.
130
+ ENV SAR_REQUEST_TIMEOUT_S=180
131
  ENV SAR_FAITHFULNESS_GATE_ENABLED=true
132
  # `flag` keeps the answer text intact and lets the UI render the
133
  # faithfulness badge. `drop` would rewrite sentences out of the answer
config/settings.py CHANGED
@@ -247,6 +247,12 @@ class Settings(BaseSettings):
247
  # raise without a Qdrant tier upgrade.
248
  byok_upload_max_bytes: int = 5 * 1024 * 1024 # 5 MB per file
249
  byok_upload_max_files: int = 5 # per session
 
 
 
 
 
 
250
  # Extensions allowed on the BYOK upload endpoint. .pdf parsed via PyPDF2;
251
  # .txt / .md pass through the text loader. OCR / docx / csv stay off to
252
  # avoid pulling Paddle (~700 MB) into the image.
 
247
  # raise without a Qdrant tier upgrade.
248
  byok_upload_max_bytes: int = 5 * 1024 * 1024 # 5 MB per file
249
  byok_upload_max_files: int = 5 # per session
250
+ # Hard chunk-count cap per uploaded file. A 50-page PDF can chunk to
251
+ # 100+ pieces -- on the HF Space CPU Basic each Groq call adds ~2 s
252
+ # so a single 135-chunk doc can blow past SAR_REQUEST_TIMEOUT_S.
253
+ # If the parsed file exceeds this cap, the ingest endpoint cleans up
254
+ # the partial points and returns 413 with a clear message.
255
+ byok_upload_max_chunks_per_file: int = 60
256
  # Extensions allowed on the BYOK upload endpoint. .pdf parsed via PyPDF2;
257
  # .txt / .md pass through the text loader. OCR / docx / csv stay off to
258
  # avoid pulling Paddle (~700 MB) into the image.
interfaces/api.py CHANGED
@@ -619,6 +619,7 @@ if _FASTAPI_AVAILABLE:
619
  "count": len(uploads),
620
  "max_files": settings.byok_upload_max_files,
621
  "max_bytes": settings.byok_upload_max_bytes,
 
622
  "allowed_extensions": list(settings.byok_upload_allowed_extensions),
623
  "items": uploads,
624
  }
@@ -725,6 +726,38 @@ if _FASTAPI_AVAILABLE:
725
  ],
726
  )
727
  result = await pipeline.ingest_document(req)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728
  # Tag every newly-upserted chunk with the file_id + ingested_at
729
  # so the list and delete endpoints can group by file.
730
  if result.point_ids:
 
619
  "count": len(uploads),
620
  "max_files": settings.byok_upload_max_files,
621
  "max_bytes": settings.byok_upload_max_bytes,
622
+ "max_chunks_per_file": settings.byok_upload_max_chunks_per_file,
623
  "allowed_extensions": list(settings.byok_upload_allowed_extensions),
624
  "items": uploads,
625
  }
 
726
  ],
727
  )
728
  result = await pipeline.ingest_document(req)
729
+ # Reject documents that chunk too aggressively. Without this
730
+ # cap a 100-page PDF dominates the dual-collection retrieval
731
+ # and the grader/faithfulness loop times out under the 180 s
732
+ # SLO. Delete what we just upserted and return 413 so the
733
+ # visitor knows exactly what failed.
734
+ max_chunks = int(settings.byok_upload_max_chunks_per_file)
735
+ if max_chunks > 0 and result.num_chunks > max_chunks:
736
+ try:
737
+ if result.point_ids:
738
+ sess_qdrant.client.delete(
739
+ collection_name=sess_qdrant.collection_name,
740
+ points_selector=list(result.point_ids),
741
+ )
742
+ except Exception as exc:
743
+ logger.warning(
744
+ "byok_upload_oversize_cleanup_failed",
745
+ error=str(exc),
746
+ chunks=result.num_chunks,
747
+ )
748
+ raise HTTPException(
749
+ status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
750
+ detail={
751
+ "reason": "too_many_chunks",
752
+ "chunk_count": result.num_chunks,
753
+ "max_chunks": max_chunks,
754
+ "hint": (
755
+ f"This file chunks to {result.num_chunks} pieces "
756
+ f"but the BYOK demo caps single uploads at {max_chunks}. "
757
+ "Try a shorter document or a section instead."
758
+ ),
759
+ },
760
+ )
761
  # Tag every newly-upserted chunk with the file_id + ingested_at
762
  # so the list and delete endpoints can group by file.
763
  if result.point_ids:
retrieval/hybrid_search.py CHANGED
@@ -161,11 +161,16 @@ class HybridSearcher:
161
  extra_filter=extra_filter,
162
  )
163
  if session_qdrant is not None:
 
 
 
 
 
164
  try:
165
  sess_dense = session_qdrant.search_with_rbac(
166
  query_embedding=query_embedding,
167
  user_context=user_context,
168
- top_k=top_k * 2,
169
  extra_filter=extra_filter,
170
  )
171
  dense_results = list(dense_results) + list(sess_dense)
@@ -208,7 +213,7 @@ class HybridSearcher:
208
  sess_sparse = session_qdrant.search_sparse_with_rbac(
209
  sparse_vector=sparse_vector,
210
  user_context=user_context,
211
- top_k=top_k * 2,
212
  extra_filter=extra_filter,
213
  )
214
  sparse_results.extend(sess_sparse)
 
161
  extra_filter=extra_filter,
162
  )
163
  if session_qdrant is not None:
164
+ # Session collections can be much smaller than the base, but
165
+ # an aggressive PDF upload can balloon to 100+ chunks; cap
166
+ # session pulls at top_k (not top_k * 2) so the candidate
167
+ # explosion in the dual-collection RRF stays bounded and the
168
+ # grader / faithfulness loop completes within the SLO.
169
  try:
170
  sess_dense = session_qdrant.search_with_rbac(
171
  query_embedding=query_embedding,
172
  user_context=user_context,
173
+ top_k=top_k,
174
  extra_filter=extra_filter,
175
  )
176
  dense_results = list(dense_results) + list(sess_dense)
 
213
  sess_sparse = session_qdrant.search_sparse_with_rbac(
214
  sparse_vector=sparse_vector,
215
  user_context=user_context,
216
+ top_k=top_k,
217
  extra_filter=extra_filter,
218
  )
219
  sparse_results.extend(sess_sparse)