Spaces:
Runtime error
Runtime error
clean code
Browse filesFiles changed (5)
8 unmodified lines91011121314151 unmodified line1718192021222324252627282918 unmodified lines484950515253548 unmodified linesexcept Exception:passdef _is_hf_persistent_storage_available() -> bool:data_dir = Path('/data')return data_dir.exists() and os.access(data_dir, os.W_OK)1 unmodified line_USE_HF_PERSISTENT_STORAGE = _is_hf_persistent_storage_available()def _default_documents_db_url() -> str:if _USE_HF_PERSISTENT_STORAGE:return 'sqlite:////data/rag_metadata.db'return 'sqlite:///./rag_metadata.db'def _bounded_int_from_env(name: str, default: int, minimum: int, maximum: int) -> int:raw_value = os.getenv(name, str(default))try:18 unmodified linesTOP_K_RESULTS = int(os.getenv('TOP_K_RESULTS', '10'))FINAL_TOP_K = int(os.getenv('FINAL_TOP_K', '5'))QDRANT_COLLECTION = os.getenv('QDRANT_COLLECTION', 'rag_docs')DOCUMENTS_DATABASE_URL = os.getenv('DOCUMENTS_DATABASE_URL', _default_documents_db_url())# External service configs8 unmodified lines91011121314151 unmodified line1718192021222324252627282918 unmodified lines4849505152538 unmodified linesexcept Exception:pass#Kiểm tra xem có thư mục /data không và có quyền ghi hay không để quyết định sử dụng persistent storage của Hugging Face hay khôngdef _is_hf_persistent_storage_available() -> bool:data_dir = Path('/data')return data_dir.exists() and os.access(data_dir, os.W_OK)1 unmodified line_USE_HF_PERSISTENT_STORAGE = _is_hf_persistent_storage_available()# Nếu sử dụng persistent storage của Hugging Face, lưu trữ metadata vào /data/rag_metadata.db, ngược lại lưu vào thư mục hiện tạidef _default_documents_db_url() -> str:if _USE_HF_PERSISTENT_STORAGE:return 'sqlite:////data/rag_metadata.db'return 'sqlite:///./rag_metadata.db'# Hàm tiện ích để lấy giá trị int từ biến môi trường với giới hạn min và max, trả về default nếu không hợp lệdef _bounded_int_from_env(name: str, default: int, minimum: int, maximum: int) -> int:raw_value = os.getenv(name, str(default))try:18 unmodified linesTOP_K_RESULTS = int(os.getenv('TOP_K_RESULTS', '10'))FINAL_TOP_K = int(os.getenv('FINAL_TOP_K', '5'))DOCUMENTS_DATABASE_URL = os.getenv('DOCUMENTS_DATABASE_URL', _default_documents_db_url())# External service configs
12345630 unmodified lines3738394041424325 unmodified lines6970717273747576777879808182838485862 unmodified lines8990919293949596979848 unmodified lines147148149150151152153154155156157import hashlibimport loggingfrom typing import Listfrom langchain_core.documents import Document as LangChainDocumentfrom rank_bm25 import BM25Okapi30 unmodified linesself.embeddings_model = embeddings_modelself.top_n_collections = max(1, int(top_n_collections or 3))# Cache giờ đây lưu một dict: { 'bm25': obj, 'corpus_docs': list, 'count': int }self._bm25_cache = {}@staticmethoddef _doc_key(doc) -> str:25 unmodified linesnormalized_cohort = (cohort_key or "").strip()if normalized_cohort:return [collection_namefor collection_name in active_collectionsif collection_matches_cohort(collection_name, normalized_cohort)]return active_collections[: self.top_n_collections]def _ensure_bm25_loaded(self, collection_name: str) -> tuple[BM25Okapi, List[LangChainDocument]] | None:"""Lazy load and cache BM25 index and corpus for a collection (với cơ chế tự động làm mới Cache)"""# 1. Lấy tổng số chunks hiện tại trong Qdrant (Rất nhanh, tốn < 10ms)try:collection_info = self.qdrant_client.get_collection(collection_name)current_count = collection_info.points_count2 unmodified linesreturn None# 2. Kiểm tra Cache: Nếu chưa có hoặc số lượng thay đổi -> Xóa cache build lạicached_data = self._bm25_cache.get(collection_name)if cached_data and cached_data.get('count') == current_count:# Tái sử dụng (Phải trả về cả bm25 VÀ corpus_docs để map điểm)return cached_data['bm25'], cached_data['corpus_docs']logger.info(f"Phát hiện dữ liệu mới hoặc chưa có cache cho {collection_name} (Count: {current_count}). Đang build lại BM25...")48 unmodified linesbm25 = BM25Okapi(tokenized_docs, k1=1.5, b=0.5)# 3. Lưu lại Cache kèm the con số count và corpus_docs để đối chiếu lần sauself._bm25_cache[collection_name] = {'bm25': bm25,'corpus_docs': corpus_docs,'count': current_count}logger.info("BM25 index built and cached for collection=%s (docs=%d)", collection_name, len(corpus_docs))return bm25, corpus_docs123456730 unmodified lines383940414243444525 unmodified lines717273747576777879808182838485868788892 unmodified lines929394959697989910010110248 unmodified lines151152153154155156157158159160161162import hashlibimport loggingfrom typing import Listfrom threading import Lockfrom langchain_core.documents import Document as LangChainDocumentfrom rank_bm25 import BM25Okapi30 unmodified linesself.embeddings_model = embeddings_modelself.top_n_collections = max(1, int(top_n_collections or 3))# Cache giờ đây lưu một dict: { 'bm25': obj, 'corpus_docs': list, 'count': int }self._bm25_cache = {}self._bm25_lock = Lock() # Thread-safe lock cho BM25 cache@staticmethoddef _doc_key(doc) -> str:25 unmodified linesnormalized_cohort = (cohort_key or "").strip()if normalized_cohort:matches = [collection_namefor collection_name in active_collectionsif collection_matches_cohort(collection_name, normalized_cohort)]return matches[:1] if matches else []return active_collections[: self.top_n_collections]def _ensure_bm25_loaded(self, collection_name: str) -> tuple[BM25Okapi, List[LangChainDocument]] | None:"""Lazy load and cache BM25 index and corpus for a collection (với cơ chế tự động làm mới Cache)"""# 1. Lấy tổng số chunks hiện tại trong Qdranttry:collection_info = self.qdrant_client.get_collection(collection_name)current_count = collection_info.points_count2 unmodified linesreturn None# 2. Kiểm tra Cache: Nếu chưa có hoặc số lượng thay đổi -> Xóa cache build lạiwith self._bm25_lock:cached_data = self._bm25_cache.get(collection_name)if cached_data and cached_data.get('count') == current_count:# Tái sử dụng (Phải trả về cả bm25 VÀ corpus_docs để map điểm)return cached_data['bm25'], cached_data['corpus_docs']logger.info(f"Phát hiện dữ liệu mới hoặc chưa có cache cho {collection_name} (Count: {current_count}). Đang build lại BM25...")48 unmodified linesbm25 = BM25Okapi(tokenized_docs, k1=1.5, b=0.5)# 3. Lưu lại Cache kèm the con số count và corpus_docs để đối chiếu lần sauwith self._bm25_lock:self._bm25_cache[collection_name] = {'bm25': bm25,'corpus_docs': corpus_docs,'count': current_count}logger.info("BM25 index built and cached for collection=%s (docs=%d)", collection_name, len(corpus_docs))return bm25, corpus_docs
1 unmodified line2345678910 unmodified lines2021222324252627282923 unmodified lines535455565758596061626364656667686970711 unmodified linefrom typing import Set_COLLECTION_SAFE_RE = re.compile(r"[^a-z0-9_]+")_YEAR_PATTERN = re.compile(r"(20\d{2})")def normalize_folder_key(folder_key: str) -> str:value = (folder_key or "").strip().lower()10 unmodified linesreturn base[:63]def extract_year_tokens(value: str) -> Set[str]:return {token for token in _YEAR_PATTERN.findall(value or "")}def extract_folder_key_from_collection_name(collection_name: str, prefix: str = "rag") -> str | None:"""Extract folder_key from collection name.23 unmodified linesreturn Falsereturn extracted.lower() == cohort_key.lower()def collection_matches_year(collection_name: str, year_scope: str) -> bool:if not year_scope:return Falsecollection_years = extract_year_tokens(collection_name)target_years = extract_year_tokens(year_scope)if not target_years:return False# For explicit ranges (e.g. 2022-2023), require all years to match.if len(target_years) >= 2:return target_years.issubset(collection_years)return bool(collection_years.intersection(target_years))1 unmodified line23456710 unmodified lines18192021222323 unmodified lines4748491 unmodified linefrom typing import Set_COLLECTION_SAFE_RE = re.compile(r"[^a-z0-9_]+")def normalize_folder_key(folder_key: str) -> str:value = (folder_key or "").strip().lower()10 unmodified linesreturn base[:63]def extract_folder_key_from_collection_name(collection_name: str, prefix: str = "rag") -> str | None:"""Extract folder_key from collection name.23 unmodified linesreturn Falsereturn extracted.lower() == cohort_key.lower()
16 unmodified lines171819202122232425262728293031323316 unmodified linesMAX_DOC_CHARS = 1800MAX_OUT_CHARS = 3000# Quản lý API Keys cho Groq và Gemini với xoay tua tự động khi gặp lỗi hoặc hết hạndef sanitize_for_prompt(text: str) -> str:"""Lọc bỏ prompt injection và PII """text = re.sub(r"(?i)(ignore previous instructions|system prompt|developer message|jailbreak)", "[FILTERED_INJECTION]", text)text = re.sub(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", "[EMAIL]", text)text = re.sub(r"\b(0\d{9}|\+84\d{9,10})\b", "[PHONE]", text)text = re.sub(r"\b\d{8,12}\b", "[ID]", text)return text.strip()def generate_standalone_query(message: str, history: List) -> str:"""Tái tạo câu hỏi từ lịch sử """if not history:16 unmodified lines17181920212216 unmodified linesMAX_DOC_CHARS = 1800MAX_OUT_CHARS = 3000def generate_standalone_query(message: str, history: List) -> str:"""Tái tạo câu hỏi từ lịch sử """if not history:
222 unmodified lines223224225226227228229230222 unmodified linesif not vectors or not vectors[0]:raise ValueError("Failed to create embeddings for chunks.")target_collection = (collection_name or document.collection_name or QDRANT_COLLECTION or "").strip()if not target_collection:raise ValueError("Target collection is empty.")222 unmodified lines223224225226227228229222 unmodified linesif not vectors or not vectors[0]:raise ValueError("Failed to create embeddings for chunks.")target_collection = (collection_name or document.collection_name or "rag_docs" or "").strip()if not target_collection:raise ValueError("Target collection is empty.")
core/config.py+3-4
rag/collection_router_retriever.py+17-12
rag/collection_utils.py-22
rag/qa_pipeline.py-11
services/document_ingest_service.py+1-2