Spaces:
Sleeping
Sleeping
Update src/rag_pipeline.py
Browse files328 unmodified lines3293303313323323333343353363373383393403413423433443453465 unmodified lines352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423328 unmodified lines if not sources: return ""
source_max_length = self.config.get("rag.source_max_length", 150) source_max_length = self.config.get("rag.source_max_length", 280) formatted_sources = []
for i, doc in enumerate(sources, 1): source_name = doc.metadata.get("source", "Unknown") # Prefer basename for cleaner UI try: from pathlib import Path
source_name = Path(str(source_name)).name or source_name except Exception: # noqa: BLE001 pass page = doc.metadata.get("page", "") page_info = f" (Page {page + 1})" if page != "" else ""
5 unmodified lines
return "\n\n".join(formatted_sources)
def format_retrieval_panel(self, question: str, sources: list[Document] | None = None) -> str: """Build a markdown panel describing the retrieval query and hits.
Args: question: User query used for retrieval sources: Optional pre-fetched docs; retrieved again if omitted
Returns: Markdown string for UI panels """ if sources is None: sources = self.get_source_documents(question)
strategy = self.retrieval_strategy_name lines = [ "### 🔍 Retrieval debug", f"**Query:** `{question}`", f"**Strategy:** `{strategy}`", f"**Hits:** {len(sources)}", "", ] if not sources: lines.append("_No documents retrieved._") return "\n".join(lines)
source_max_length = self.config.get("rag.source_max_length", 280) for i, doc in enumerate(sources, 1): raw_source = doc.metadata.get("source", "Unknown") try: from pathlib import Path
source_name = Path(str(raw_source)).name or str(raw_source) except Exception: # noqa: BLE001 source_name = str(raw_source) page = doc.metadata.get("page", "") page_info = f", page {int(page) + 1}" if page != "" else "" preview = doc.page_content[:source_max_length].replace("\n", " ") if len(doc.page_content) > source_max_length: preview += "..." lines.append(f"**[{i}]** `{source_name}`{page_info}") lines.append(f"> {preview}") lines.append("")
return "\n".join(lines)
def get_answer_with_retrieval( self, question: str, chat_history: list[dict] | None = None ) -> dict[str, Any]: """Return answer plus retrieval panel markdown and source docs.
Args: question: User question chat_history: Optional prior turns
Returns: Dict with keys: answer, retrieval_panel, sources, source_documents """ sources = self.get_source_documents(question, chat_history=chat_history) answer = self.get_answer(question, chat_history=chat_history) return { "answer": answer, "retrieval_panel": self.format_retrieval_panel(question, sources=sources), "sources": self.format_sources(sources), "source_documents": sources, }
def get_main_document_info(self) -> dict[str, Any]: """Get information about loaded main document.
src/rag_pipeline.py+74-1