Spaces:
Sleeping
Sleeping
File size: 13,841 Bytes
49f0cfb 32c4506 49f0cfb dc1b199 732b14f dc1b199 732b14f dc1b199 0b42403 732b14f dc1b199 732b14f dc1b199 49f0cfb dc1b199 32c4506 dc1b199 49f0cfb dc1b199 49f0cfb dc1b199 49f0cfb dc1b199 49f0cfb dc1b199 49f0cfb dc1b199 49f0cfb dc1b199 eda3d74 0b42403 eda3d74 b76f199 eda3d74 b76f199 eda3d74 b76f199 eda3d74 b76f199 eda3d74 b76f199 eda3d74 b76f199 eda3d74 b76f199 eda3d74 b76f199 732b14f | 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 | """Tenant-scoped retriever backed by the LangChain vector store.
In the LangChain experiment branch, the retriever no longer computes
query embeddings manually. Instead, it delegates directly to
``VectorStore.search(query_text, tenant_id, k)`` β the LangChain store
handles embedding the query internally.
Compared to master branch:
- β
Simpler interface β no ``get_embedding_client()`` call
- β
Single responsibility β retrieval only
- β
Tenant isolation guaranteed by the vector store (FAISS post-filter by ``tenant_id``)
"""
import logging
from functools import partial
from app.async_executor import run_sync_in_executor
from app.config import settings
from app.models.schemas import SearchResult
from app.retrieval.chunk_role import role_priority
from app.retrieval.semantic_cache import get_semantic_cache, is_semantic_cache_active
from app.retrieval.vector_search import async_vs_search
from app.vectorstore.factory import get_vectorstore
logger = logging.getLogger(__name__)
def _scoped_cache_query(
query: str,
*,
hierarchy_level: str | None = None,
doc_id_in: frozenset[str] | None = None,
) -> str:
"""Distinct cache key when hierarchy or document scope differs."""
if not hierarchy_level and not doc_id_in:
return query
doc_sig = ",".join(sorted(doc_id_in)) if doc_id_in else ""
return f"{query}\n#scope:{hierarchy_level or ''}:{doc_sig}"
async def retrieve_scoped_async(
query: str,
tenant_id: str,
k: int,
*,
hierarchy_level: str | None = None,
doc_id_in: frozenset[str] | None = None,
) -> list[SearchResult]:
"""Scoped async retrieval (hierarchy / doc filter) with optional semantic cache."""
cache_query = _scoped_cache_query(
query, hierarchy_level=hierarchy_level, doc_id_in=doc_id_in
)
cache = get_semantic_cache()
if cache is not None:
cached = await cache.get(cache_query, tenant_id)
if cached is not None:
logger.debug(
"Semantic cache hit (scoped) tenant=%s k=%d",
tenant_id,
min(k, len(cached)),
)
return cached[:k]
vs = get_vectorstore()
results = await async_vs_search(
vs,
query,
tenant_id,
k=k,
hierarchy_level=hierarchy_level,
doc_id_in=doc_id_in,
)
if cache is not None and results:
await cache.put(cache_query, tenant_id, results)
return results
async def retrieve_async(
query: str,
tenant_id: str,
k: int | None = None,
) -> list[SearchResult]:
"""Async retrieval with optional semantic cache (Qdrant when enabled)."""
top_k = k if k is not None else settings.retrieval_top_k
cache = get_semantic_cache()
if cache is not None:
cached = await cache.get(query, tenant_id)
if cached is not None:
logger.debug(
"Semantic cache hit tenant=%s k=%d", tenant_id, min(top_k, len(cached))
)
return cached[:top_k]
vs = get_vectorstore()
results = await async_vs_search(vs, query, tenant_id, k=top_k)
if cache is not None and results:
await cache.put(query, tenant_id, results)
logger.debug(
"Retrieved %d results (async) for tenant=%s (k=%d)",
len(results),
tenant_id,
top_k,
)
return results
def use_async_retrieval_path() -> bool:
"""Use async retrieval (and semantic cache when Qdrant cache is configured)."""
return bool(settings.enable_async_pipeline or is_semantic_cache_active())
def _use_async_retrieval_path() -> bool:
return use_async_retrieval_path()
async def retrieve_unified(
query: str,
tenant_id: str,
k: int | None = None,
) -> list[SearchResult]:
"""Async-safe retrieval: cache/async path or executor-wrapped sync search."""
top_k = k if k is not None else settings.retrieval_top_k
if _use_async_retrieval_path():
return await retrieve_async(query=query, tenant_id=tenant_id, k=top_k)
return await run_sync_in_executor(
partial(retrieve, query=query, tenant_id=tenant_id, k=top_k)
)
async def retrieve_for_report_unified(
query: str,
tenant_id: str,
primary_document_id: str | None,
k: int | None = None,
secondary_document_ids: list[str] | None = None,
) -> list[SearchResult]:
"""Async-safe :func:`retrieve_for_report` with optional semantic cache."""
if _use_async_retrieval_path():
return await retrieve_for_report_async(
query=query,
tenant_id=tenant_id,
primary_document_id=primary_document_id,
k=k,
secondary_document_ids=secondary_document_ids,
)
return await run_sync_in_executor(
partial(
retrieve_for_report,
query=query,
tenant_id=tenant_id,
primary_document_id=primary_document_id,
k=k,
secondary_document_ids=secondary_document_ids,
)
)
def retrieve(
query: str,
tenant_id: str,
k: int | None = None,
) -> list[SearchResult]:
"""Retrieve the top-k chunks nearest to ``query`` for ``tenant_id``.
The query is embedded internally by the LangChain vector store β
no manual vector computation is required by the caller.
Tenant isolation is enforced by post-filtering FAISS hits so only the
requested ``tenant_id`` is returned.
Args:
query: Plain-text retrieval query (typically concatenated fact bullets).
tenant_id: Restrict results to this tenant's documents.
k: Number of candidates to retrieve. Defaults to
``settings.retrieval_top_k``.
Returns:
List of :class:`~app.models.schemas.SearchResult` ordered by
descending similarity score.
Example::
results = retrieve("Victorian terrace, 95 sqm", tenant_id="t-abc")
"""
top_k = k if k is not None else settings.retrieval_top_k
vs = get_vectorstore()
results = vs.search(query=query, tenant_id=tenant_id, k=top_k)
logger.debug(
"Retrieved %d results for tenant=%s (k=%d)", len(results), tenant_id, top_k
)
return results
def reorder_by_chunk_role(
results: list[SearchResult],
boost_boilerplate: bool = True,
) -> list[SearchResult]:
"""Stable-sort retrieval hits to bring ``chunk_role='boilerplate'`` first.
Used at assembly / low AI-involvement tiers so the firm's approved
standard wording is preferentially placed in the LLM context (and in the
deterministic stitcher's input). Original similarity order is preserved
within each role bucket.
"""
if not boost_boilerplate or not results:
return results
return sorted(results, key=lambda r: role_priority(r.chunk_role))
def retrieve_for_report(
query: str,
tenant_id: str,
primary_document_id: str | None,
k: int | None = None,
secondary_document_ids: list[str] | None = None,
) -> list[SearchResult]:
"""Retrieve chunks for ``tenant_id``, prioritising the report's source upload.
All chunks remain tenant-scoped (privacy). When ``primary_document_id`` is
the UUID of the document the user attached when creating this report, chunks
from that file are listed first so RAG favours the user's RICS / survey
upload over any other files they may have ingested under the same tenant.
Optional ``secondary_document_ids`` (e.g. exemplar / Beh Rang reference PDFs)
are ordered after the primary document and before the rest of the library.
Args:
query: Semantic query (e.g. expanded bullets).
tenant_id: Isolated tenant identifier.
primary_document_id: ``Report.document_id`` or ``None`` to use global ordering.
k: Result cap (defaults to ``settings.retrieval_top_k``).
secondary_document_ids: Additional document UUIDs to prioritise after primary.
Returns:
Up to ``k`` :class:`SearchResult` rows, primary-document chunks first.
"""
if k is None:
k = settings.retrieval_top_k
fetch_n = max(k * 4, min(48, k * 6))
pool = retrieve(query=query, tenant_id=tenant_id, k=fetch_n)
if not primary_document_id:
return pool[:k]
secondary = set(secondary_document_ids or [])
preferred = [r for r in pool if r.doc_id == primary_document_id]
second = [r for r in pool if r.doc_id in secondary and r.doc_id != primary_document_id]
rest = [
r
for r in pool
if r.doc_id != primary_document_id and r.doc_id not in secondary
]
merged = preferred + second + rest
logger.debug(
"retrieve_for_report: primary_doc=%s secondary=%d preferred=%d total_returned=%d",
primary_document_id[:8] if primary_document_id else None,
len(secondary),
len(preferred),
min(k, len(merged)),
)
return merged[:k]
def retrieve_document_level_context(
*,
template_id: str,
skeleton_excerpt: str,
tenant_id: str,
primary_document_id: str | None,
reference_document_ids: list[str] | None,
product_label: str | None = None,
) -> list[SearchResult]:
"""Broad semantic pull for whole-document narrative (survey + exemplar PDFs).
Uses a section-aware query over the template skeleton so hits span report-wide
phrasing, not only bullet-level detail. Results are capped per document so both
the raw context PDF and reference RICS exemplars can contribute.
"""
if not primary_document_id and not (reference_document_ids or []):
return []
sk = (skeleton_excerpt or "").strip().replace("\n", " ")[:600]
pl = (product_label or "RICS Home Survey Level 3 (Building Survey)").strip()
doc_query = (
f"{pl} report section {template_id}. "
f"Professional surveyor narrative and condition description. {sk}"
)
fetch_n = max(48, settings.rag_doc_context_max_chunks * 12)
pool = retrieve(query=doc_query, tenant_id=tenant_id, k=fetch_n)
ref_set = {str(x) for x in (reference_document_ids or []) if x}
allowed: set[str] = set()
if primary_document_id:
allowed.add(str(primary_document_id))
allowed.update(ref_set)
filtered = [r for r in pool if r.doc_id in allowed]
if not filtered:
return []
primary_max = settings.rag_doc_chunks_primary
per_ref = settings.rag_doc_chunks_per_reference
total_max = settings.rag_doc_context_max_chunks
ref_counts: dict[str, int] = {}
primary_count = 0
out: list[SearchResult] = []
for r in filtered:
if len(out) >= total_max:
break
did = r.doc_id
if did == primary_document_id:
if primary_count < primary_max:
out.append(r)
primary_count += 1
elif did in ref_set:
n = ref_counts.get(did, 0)
if n < per_ref:
out.append(r)
ref_counts[did] = n + 1
logger.debug(
"retrieve_document_level_context: section=%s chunks=%d primary=%d",
template_id,
len(out),
primary_count,
)
return out
async def retrieve_for_report_async(
query: str,
tenant_id: str,
primary_document_id: str | None,
k: int | None = None,
secondary_document_ids: list[str] | None = None,
) -> list[SearchResult]:
"""Async variant of :func:`retrieve_for_report`."""
if k is None:
k = settings.retrieval_top_k
fetch_n = max(k * 4, min(48, k * 6))
pool = await retrieve_async(query=query, tenant_id=tenant_id, k=fetch_n)
if not primary_document_id:
return pool[:k]
secondary = set(secondary_document_ids or [])
preferred = [r for r in pool if r.doc_id == primary_document_id]
second = [r for r in pool if r.doc_id in secondary and r.doc_id != primary_document_id]
rest = [
r
for r in pool
if r.doc_id != primary_document_id and r.doc_id not in secondary
]
return (preferred + second + rest)[:k]
async def retrieve_document_level_context_async(
*,
template_id: str,
skeleton_excerpt: str,
tenant_id: str,
primary_document_id: str | None,
reference_document_ids: list[str] | None,
product_label: str | None = None,
) -> list[SearchResult]:
"""Async variant of :func:`retrieve_document_level_context`."""
if not primary_document_id and not (reference_document_ids or []):
return []
sk = (skeleton_excerpt or "").strip().replace("\n", " ")[:600]
pl = (product_label or "RICS Home Survey Level 3 (Building Survey)").strip()
doc_query = (
f"{pl} report section {template_id}. "
f"Professional surveyor narrative and condition description. {sk}"
)
fetch_n = max(48, settings.rag_doc_context_max_chunks * 12)
pool = await retrieve_async(query=doc_query, tenant_id=tenant_id, k=fetch_n)
ref_set = {str(x) for x in (reference_document_ids or []) if x}
allowed: set[str] = set()
if primary_document_id:
allowed.add(str(primary_document_id))
allowed.update(ref_set)
filtered = [r for r in pool if r.doc_id in allowed]
if not filtered:
return []
primary_max = settings.rag_doc_chunks_primary
per_ref = settings.rag_doc_chunks_per_reference
total_max = settings.rag_doc_context_max_chunks
ref_counts: dict[str, int] = {}
primary_count = 0
out: list[SearchResult] = []
for r in filtered:
if len(out) >= total_max:
break
did = r.doc_id
if did == primary_document_id:
if primary_count < primary_max:
out.append(r)
primary_count += 1
elif did in ref_set:
n = ref_counts.get(did, 0)
if n < per_ref:
out.append(r)
ref_counts[did] = n + 1
return out
|