Skip to content

RAG

The RAG class is the single entry point for all ragwise functionality. It is an async context manager.

Usage

from ragwise import RAG

async with RAG(llm="openai/gpt-4o-mini") as rag:
    await rag.ingest("./docs/")
    answer = await rag.query("What is the refund policy?")

Reference

ragwise.pipeline.RAG

Async context manager for the full RAG pipeline.

Basic usage::

async with RAG(llm="openai/gpt-4o-mini") as rag:
    await rag.ingest("./docs/")
    answer = await rag.query("What is the refund policy?")

Streaming::

async for token in rag.stream_query("What changed in v2?"):
    print(token, end="", flush=True)

Agent tool (no generation)::

results = await rag.search("error code 0x80004005", top_k=5)
Source code in src/ragwise/pipeline.py
 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
class RAG:
    """Async context manager for the full RAG pipeline.

    Basic usage::

        async with RAG(llm="openai/gpt-4o-mini") as rag:
            await rag.ingest("./docs/")
            answer = await rag.query("What is the refund policy?")

    Streaming::

        async for token in rag.stream_query("What changed in v2?"):
            print(token, end="", flush=True)

    Agent tool (no generation)::

        results = await rag.search("error code 0x80004005", top_k=5)
    """

    def __init__(
        self,
        config: RAGConfig | None = None,
        *,
        embedder: str | Any = _UNSET,
        store: str | Any = _UNSET,
        llm: str | Any = _UNSET,
        chunker: str | Any = _UNSET,
        chunk_size: Any = _UNSET,
        chunk_overlap: Any = _UNSET,
        reranker: Any = _UNSET,
        cache: Any = _UNSET,
        batch_size: Any = _UNSET,
    ) -> None:
        if config is not None:
            self._config = config
        else:
            self._config = RAGConfig(
                embedder="openai/text-embedding-3-small" if embedder is _UNSET else embedder,
                store="memory" if store is _UNSET else store,
                llm="openai/gpt-4o-mini" if llm is _UNSET else llm,
                chunker="recursive" if chunker is _UNSET else chunker,
                chunk_size=512 if chunk_size is _UNSET else chunk_size,
                chunk_overlap=64 if chunk_overlap is _UNSET else chunk_overlap,
                reranker=None if reranker is _UNSET else reranker,
                cache=True if cache is _UNSET else cache,
                batch_size=32 if batch_size is _UNSET else batch_size,
            )

        # Explicit object overrides — used in __aenter__ when config= and kwargs are mixed
        self._embedder_kw: Any = None if embedder is _UNSET else embedder
        self._llm_kw: Any = None if llm is _UNSET else llm
        self._store_kw: Any = None if store is _UNSET else store
        self._reranker_kw: Any = _UNSET if reranker is _UNSET else reranker
        self._cache_kw: Any = _UNSET if cache is _UNSET else cache

        self._store: VectorStore | None = None
        self._embedder: Any = None
        self._llm: LLMProtocol | None = None
        self._reranker: Any = None
        self._chunker: Any = None
        self._searcher: HybridSearcher | None = None
        self._assembler: Assembler | None = None
        self._sufficiency: SufficiencyChecker = SufficiencyChecker()
        self._tracer: Any = None  # LangfuseTracer | None
        self._semantic_cache: Any = None  # MemorySemanticCache | None

    async def __aenter__(self) -> RAG:
        from ragwise.embedding.base import resolve_embedder

        cfg = self._config

        # Use explicit kwarg object if passed, otherwise resolve from config spec
        if self._embedder_kw is not None and not isinstance(self._embedder_kw, str):
            self._embedder = self._embedder_kw
        else:
            self._embedder = resolve_embedder(cfg.embedder)

        if self._store_kw is not None and not isinstance(self._store_kw, str):
            self._store = self._store_kw
        else:
            self._store = _resolve_store(cfg.store)

        if self._llm_kw is not None and not isinstance(self._llm_kw, str):
            base_llm = self._llm_kw
        else:
            base_llm = resolve_llm(cfg.llm)

        # cache: explicit False overrides config's True
        use_cache = cfg.cache if self._cache_kw is _UNSET else self._cache_kw
        if use_cache:
            from ragwise.generation.semantic_cache import MemorySemanticCache
            backend = use_cache if isinstance(use_cache, str) else "memory"
            # Semantic cache at query level (replaces SHA-256 prompt cache)
            if isinstance(backend, str) and backend.startswith("redis"):
                from ragwise.generation.semantic_cache import RedisSemanticCache
                self._semantic_cache = RedisSemanticCache(url=backend)
            else:
                self._semantic_cache = MemorySemanticCache()
            # Keep LLM-level cache for streaming paths
            cache_obj = LLMCache(backend=backend)
            self._llm = CachedLLM(llm=base_llm, cache=cache_obj)
        else:
            self._llm = base_llm

        reranker_spec = cfg.reranker if self._reranker_kw is _UNSET else self._reranker_kw
        self._reranker = resolve_reranker(reranker_spec)

        self._chunker = _resolve_chunker(
            cfg.chunker, cfg.chunk_size, cfg.chunk_overlap,
            model="gpt-4o",
            llm=base_llm,
        )

        self._searcher = HybridSearcher(
            store=self._store, embedder=self._embedder
        )
        self._assembler = Assembler()
        return self

    async def __aexit__(self, *args: Any) -> None:
        pass  # InMemoryStore needs no teardown; connection cleanup deferred to v0.2

    async def ingest(
        self,
        path: str,
        *,
        glob: str = "**/*",
        force: bool = False,
        tenant_id: str | None = None,
        metadata: dict[str, Any] | None = None,
        on_progress: Callable[[str, int, int], None] | None = None,
        show_progress: bool = False,
    ) -> IngestResult:
        assert self._store is not None, "RAG must be used as a context manager"
        assert self._embedder is not None
        assert self._chunker is not None

        loader = AutoLoader()
        p = Path(path)

        indexed: dict[str, str] = {}
        if not force:
            indexed = await self._store.get_indexed_sources()

        files = [p] if p.is_file() else [f for f in p.glob(glob) if f.is_file()]
        total_files = len(files)

        _progress_bar: Any = None
        if show_progress:
            with contextlib.suppress(ImportError):
                from tqdm import tqdm
                _progress_bar = tqdm(total=total_files, desc="Indexing", unit="file")

        succeeded = 0
        failed = 0
        errors: list[str] = []
        failed_files: list[str] = []
        cfg = self._config

        for files_done, file_path in enumerate(files, start=1):
            try:
                current_hash = hash_source(file_path)
                if not force and indexed.get(str(file_path)) == current_hash:
                    if on_progress:
                        on_progress(str(file_path), files_done, total_files)
                    if _progress_bar is not None:
                        _progress_bar.update(1)
                    continue

                docs: list[Document] = loader.load(file_path)

                chunks: list[Document] = []
                for doc in docs:
                    chunks.extend(self._chunker.chunk(doc))

                if not chunks:
                    succeeded += 1
                    if on_progress:
                        on_progress(str(file_path), files_done, total_files)
                    if _progress_bar is not None:
                        _progress_bar.update(1)
                    continue

                texts = [c.text for c in chunks]
                all_embeddings: list[list[float]] = []
                for i in range(0, len(texts), cfg.batch_size):
                    batch = texts[i : i + cfg.batch_size]
                    batch_vecs = await self._embedder.embed(batch)
                    all_embeddings.extend(batch_vecs)

                # Delete stale chunks before upserting — prevents duplicates on re-ingest
                await self._store.delete(str(file_path))

                chunk_meta = metadata or {}
                embedded_docs = [
                    EmbeddedDoc(
                        id=chunk.id,
                        text=chunk.text,
                        source=chunk.source,
                        embedding=vec,
                        metadata={
                            **chunk.metadata,
                            **chunk_meta,
                            "content_hash": current_hash,
                            "tenant_id": tenant_id or "",
                        },
                    )
                    for chunk, vec in zip(chunks, all_embeddings, strict=True)
                ]
                await self._store.upsert(embedded_docs)
                succeeded += 1

            except Exception as exc:
                failed += 1
                errors.append(f"{file_path}: {exc}")
                failed_files.append(str(file_path))

            if on_progress:
                on_progress(str(file_path), files_done, total_files)
            if _progress_bar is not None:
                _progress_bar.update(1)

        if _progress_bar is not None:
            _progress_bar.close()

        return IngestResult(
            succeeded=succeeded,
            failed=failed,
            errors=errors,
            failed_files=failed_files,
        )

    async def query(
        self,
        question: str,
        *,
        config: QueryConfig | None = None,
    ) -> Answer:
        assert self._searcher is not None, "RAG must be used as a context manager"
        assert self._assembler is not None
        assert self._llm is not None

        qc = config or QueryConfig()
        cfg = self._config
        trace = QueryTrace()

        # --- Embedding ---
        t0 = time.perf_counter()
        query_vec = (await self._embedder.embed([question]))[0]
        trace.query_embedding_ms = int((time.perf_counter() - t0) * 1000)

        # --- Semantic cache lookup (skip for temporal/versioned queries) ---
        _temporal_active = bool(qc.as_of or qc.version)
        if self._semantic_cache is not None and not _temporal_active:
            cached_answer, sim = self._semantic_cache.lookup(query_vec, qc.cache_threshold)
            if cached_answer is not None:
                trace.cache_hit = True
                trace.cache_similarity = sim
                return Answer(
                    text=cached_answer.text,
                    citations=cached_answer.citations,
                    chunks_used=cached_answer.chunks_used,
                    sufficient=cached_answer.sufficient,
                    has_sufficient_context=cached_answer.has_sufficient_context,
                    trace=trace,
                )

        # --- Retrieval (with optional expansion) ---
        if (qc.n_queries > 1 or qc.query_variants) and not _temporal_active:
            t0 = time.perf_counter()
            results, expanded = await self._expand_and_search(question, qc, query_vec)
            trace.expanded_queries = expanded
            trace.expansion_ms = int((time.perf_counter() - t0) * 1000)
            trace.retrieval_ms = trace.expansion_ms
        else:
            t0 = time.perf_counter()
            results = await self._searcher.search(
                question,
                top_k=qc.top_k,
                alpha=qc.alpha,
                tenant_id=qc.tenant_id,
                allowed_sources=qc.allowed_sources if qc.allowed_sources else None,
                as_of=qc.as_of,
                version=qc.version,
            )
            trace.retrieval_ms = int((time.perf_counter() - t0) * 1000)
            if _temporal_active:
                trace.temporal_filter_applied = True
                trace.as_of_used = qc.as_of

        # --- Reranking ---
        rerank_score_map: dict[str, float] = {}
        if self._reranker is not None and results:
            t0 = time.perf_counter()
            results = await self._reranker.rerank(question, results, top_k=qc.top_k)
            trace.rerank_ms = int((time.perf_counter() - t0) * 1000)
            rerank_score_map = {r.id: r.score for r in results}

        # --- Sufficiency / confidence gate ---
        has_sufficient_context = True
        if cfg.confidence_threshold > 0.0 and results:
            has_sufficient_context = await self._sufficiency.check(
                query_vec, results, cfg.confidence_threshold
            )

        # Also honour legacy per-query sufficiency check
        sufficient = True
        if qc.check_sufficiency and results:
            sufficient = await self._sufficiency.check(
                query_vec, results, qc.sufficiency_threshold
            )

        # Build RetrievedChunks for trace
        def _to_chunk(r: SearchResult) -> RetrievedChunk:
            rs = rerank_score_map.get(r.id)
            return RetrievedChunk(
                text=r.text,
                source=r.source,
                chunk_id=r.id,
                final_score=r.score,
                bm25_score=r.bm25_score,
                dense_score=r.dense_score,
                rerank_score=rs,
                page=r.metadata.get("page"),
            )

        # --- Context assembly ---
        assembler = (
            Assembler(max_context_tokens=qc.max_context_tokens)
            if qc.max_context_tokens
            else self._assembler
        )
        prompt, citations, dropped_results = assembler.assemble(question, results)

        trace.retrieved_chunks = [_to_chunk(r) for r in results]
        trace.dropped_chunks = [_to_chunk(r) for r in dropped_results]
        trace.context_tokens = count_tokens(prompt)

        # --- Confidence gate: skip LLM if insufficient ---
        if not has_sufficient_context:
            top3 = [
                Citation(
                    text=r.text,
                    source=r.source,
                    chunk_id=r.id,
                    final_score=r.score,
                    bm25_score=r.bm25_score,
                    dense_score=r.dense_score,
                    page=r.metadata.get("page"),
                )
                for r in results[:3]
            ]
            return Answer(
                text=cfg.insufficient_response,
                citations=[],
                chunks_used=len(results),
                sufficient=False,
                has_sufficient_context=False,
                trace=trace,
                top_retrieved=top3,
            )

        # --- Generation ---
        t0 = time.perf_counter()
        text = await self._llm.complete(prompt)
        trace.generation_ms = int((time.perf_counter() - t0) * 1000)

        # Estimate token counts + cost
        trace.prompt_tokens = count_tokens(prompt)
        trace.completion_tokens = count_tokens(text)
        llm_spec = cfg.llm if isinstance(cfg.llm, str) else ""
        trace.cost_usd = _estimate_cost(llm_spec, trace.prompt_tokens, trace.completion_tokens)

        answer = Answer(
            text=text,
            citations=citations if qc.include_citations else [],
            chunks_used=len(results),
            sufficient=sufficient,
            has_sufficient_context=True,
            trace=trace,
        )

        # Store in semantic cache (skip for temporal/versioned queries)
        if self._semantic_cache is not None and not _temporal_active:
            self._semantic_cache.store(query_vec, answer)

        if self._tracer is not None:
            with contextlib.suppress(Exception):
                await self._tracer.trace(question, answer, EvalSchema(query=question))

        return answer

    async def stream_query(
        self,
        question: str,
        *,
        config: QueryConfig | None = None,
    ) -> AsyncGenerator[str, None]:
        """Stream generated tokens as they arrive from the LLM.

        Usage::

            async for token in rag.stream_query("What changed in v2?"):
                print(token, end="", flush=True)

        Falls back to yielding the complete response as a single token if the
        underlying LLM does not implement ``StreamingLLMProtocol``.
        """
        assert self._searcher is not None, "RAG must be used as a context manager"
        assert self._assembler is not None
        assert self._llm is not None

        qc = config or QueryConfig()
        cfg = self._config

        query_vec = (await self._embedder.embed([question]))[0]
        results = await self._searcher.search(
            question,
            top_k=qc.top_k,
            alpha=qc.alpha,
            tenant_id=qc.tenant_id,
            allowed_sources=qc.allowed_sources if qc.allowed_sources else None,
        )

        if self._reranker is not None and results:
            results = await self._reranker.rerank(question, results, top_k=qc.top_k)

        # Confidence gate
        if cfg.confidence_threshold > 0.0 and results:
            has_ctx = await self._sufficiency.check(query_vec, results, cfg.confidence_threshold)
            if not has_ctx:
                yield cfg.insufficient_response
                return

        assembler = (
            Assembler(max_context_tokens=qc.max_context_tokens)
            if qc.max_context_tokens
            else self._assembler
        )
        prompt, _, _ = assembler.assemble(question, results)

        if isinstance(self._llm, StreamingLLMProtocol):
            async for token in self._llm.stream_complete(prompt):
                yield token
        else:
            yield await self._llm.complete(prompt)

    async def delete(self, source: str) -> None:
        """Remove all indexed chunks whose source matches *source*.

        Use for GDPR right-to-erasure or removing a specific document::

            await rag.delete("docs/old-policy.md")
        """
        assert self._store is not None, "RAG must be used as a context manager"
        await self._store.delete(source)

    async def list_sources(self) -> list[str]:
        """Return all distinct source paths currently indexed.

        Useful for building admin UIs or audit reports::

            sources = await rag.list_sources()
        """
        assert self._store is not None, "RAG must be used as a context manager"
        return await self._store.list_sources()

    async def update(self, source: str, path: str | None = None) -> IngestResult:
        """Re-ingest a source, replacing all its existing chunks.

        If *path* is omitted, *source* is used as the file path::

            await rag.update("docs/policy.md")
            await rag.update("docs/policy.md", path="/new/location/policy.md")
        """
        assert self._store is not None, "RAG must be used as a context manager"
        await self._store.delete(source)
        return await self.ingest(path or source, force=True)

    async def search(
        self,
        query: str,
        *,
        top_k: int = 5,
        config: QueryConfig | None = None,
    ) -> list[SearchResult]:
        """Return raw hybrid search results — no generation.

        Use this to build agent tools, custom pipelines, or debug retrieval::

            results = await rag.search("error code 0x80004005", top_k=5)
            for r in results:
                print(r.score, r.source, r.text[:120])
        """
        assert self._searcher is not None, "RAG must be used as a context manager"
        qc = config or QueryConfig()
        return await self._searcher.search(
            query,
            top_k=top_k,
            alpha=qc.alpha,
            tenant_id=qc.tenant_id,
            allowed_sources=qc.allowed_sources if qc.allowed_sources else None,
            as_of=qc.as_of,
            version=qc.version,
        )

    async def _expand_and_search(
        self,
        question: str,
        qc: QueryConfig,
        query_vec: list[float],
    ) -> tuple[list[SearchResult], list[str]]:
        """Generate query variants and merge results via RRF."""
        import asyncio
        import json as _json
        import logging

        _log = logging.getLogger(__name__)

        if qc.query_variants:
            variants = list(qc.query_variants)
        else:
            prompt = (
                f"Generate {qc.n_queries} search query variants for the following question. "
                f"Each variant should rephrase the question to improve document retrieval coverage. "
                f"Return as a JSON array of strings.\n\nOriginal: {question}"
            )
            try:
                assert self._llm is not None
                raw = await self._llm.complete(prompt)
                parsed = _json.loads(raw)
                variants = [str(v) for v in parsed[: qc.n_queries]]
            except Exception:
                _log.warning("[RAG] Query expansion parse failure — falling back to single query")
                variants = [question]

        assert self._searcher is not None
        searches = await asyncio.gather(
            *[
                self._searcher.search(
                    v,
                    top_k=qc.top_k,
                    alpha=qc.alpha,
                    tenant_id=qc.tenant_id,
                    allowed_sources=qc.allowed_sources if qc.allowed_sources else None,
                )
                for v in variants
            ],
            return_exceptions=True,
        )

        from ragwise.utils.rrf import rrf

        all_rankings: list[list[str]] = []
        lookup: dict[str, SearchResult] = {}
        for res in searches:
            if isinstance(res, Exception):
                continue
            all_rankings.append([r.id for r in res])  # type: ignore[union-attr]
            for r in res:  # type: ignore[union-attr]
                lookup[r.id] = r

        fused = rrf(all_rankings)
        results = [lookup[doc_id] for doc_id, _ in fused[: qc.top_k] if doc_id in lookup]
        return results, variants

    async def list_stale(
        self,
        as_of: str | None = "now",
        expiring_soon_days: int = 0,
    ) -> tuple[list[Any], list[Any]]:
        """Return (expired, expiring_soon) StaleSource lists.

        Sources with no valid_until metadata are never returned.
        """
        assert self._store is not None, "RAG must be used as a context manager"
        from ragwise.lifecycle import StalenessChecker
        checker = StalenessChecker(self._store)
        return await checker.list_stale(as_of=as_of, expiring_soon_days=expiring_soon_days)

    async def purge_stale(self, as_of: str | None = "now") -> Any:
        """Delete all chunks from sources whose valid_until < as_of."""
        assert self._store is not None, "RAG must be used as a context manager"
        from ragwise.lifecycle import StalenessChecker
        checker = StalenessChecker(self._store)
        return await checker.purge_stale(as_of=as_of)

    async def staleness_report(
        self,
        as_of: str | None = "now",
        expiring_soon_days: int = 30,
    ) -> str:
        """Return a human-readable staleness report string."""
        assert self._store is not None, "RAG must be used as a context manager"
        from ragwise.lifecycle import StalenessChecker
        checker = StalenessChecker(self._store)
        return await checker.staleness_report(as_of=as_of, expiring_soon_days=expiring_soon_days)

    @property
    def cache(self) -> Any:
        """Access the semantic cache for stats/clear/invalidate."""
        return self._semantic_cache

    def set_tracer(self, tracer: Any) -> RAG:
        """Set a tracer for production observability (e.g. LangfuseTracer).

        Returns self for fluent chaining::

            rag.set_tracer(LangfuseTracer(...))
        """
        self._tracer = tracer
        return self

    async def eval_chunks(self, docs: list[Document]) -> ChunkEvalSchema:
        """Score a list of Document chunks for quality metrics."""
        assert self._embedder is not None, "RAG must be used as a context manager"
        from ragwise.eval.chunks import score_chunks

        return await score_chunks(docs, self._embedder, chunk_size=self._config.chunk_size)

    async def eval(self, dataset: list[dict[str, Any]]) -> EvalSchema:
        """Evaluate the RAG pipeline on a labeled dataset using RAGAS."""
        from ragwise.eval.metrics import evaluate_dataset

        return await evaluate_dataset(dataset)

cache property

Access the semantic cache for stats/clear/invalidate.

stream_query(question, *, config=None) async

Stream generated tokens as they arrive from the LLM.

Usage::

async for token in rag.stream_query("What changed in v2?"):
    print(token, end="", flush=True)

Falls back to yielding the complete response as a single token if the underlying LLM does not implement StreamingLLMProtocol.

Source code in src/ragwise/pipeline.py
async def stream_query(
    self,
    question: str,
    *,
    config: QueryConfig | None = None,
) -> AsyncGenerator[str, None]:
    """Stream generated tokens as they arrive from the LLM.

    Usage::

        async for token in rag.stream_query("What changed in v2?"):
            print(token, end="", flush=True)

    Falls back to yielding the complete response as a single token if the
    underlying LLM does not implement ``StreamingLLMProtocol``.
    """
    assert self._searcher is not None, "RAG must be used as a context manager"
    assert self._assembler is not None
    assert self._llm is not None

    qc = config or QueryConfig()
    cfg = self._config

    query_vec = (await self._embedder.embed([question]))[0]
    results = await self._searcher.search(
        question,
        top_k=qc.top_k,
        alpha=qc.alpha,
        tenant_id=qc.tenant_id,
        allowed_sources=qc.allowed_sources if qc.allowed_sources else None,
    )

    if self._reranker is not None and results:
        results = await self._reranker.rerank(question, results, top_k=qc.top_k)

    # Confidence gate
    if cfg.confidence_threshold > 0.0 and results:
        has_ctx = await self._sufficiency.check(query_vec, results, cfg.confidence_threshold)
        if not has_ctx:
            yield cfg.insufficient_response
            return

    assembler = (
        Assembler(max_context_tokens=qc.max_context_tokens)
        if qc.max_context_tokens
        else self._assembler
    )
    prompt, _, _ = assembler.assemble(question, results)

    if isinstance(self._llm, StreamingLLMProtocol):
        async for token in self._llm.stream_complete(prompt):
            yield token
    else:
        yield await self._llm.complete(prompt)

delete(source) async

Remove all indexed chunks whose source matches source.

Use for GDPR right-to-erasure or removing a specific document::

await rag.delete("docs/old-policy.md")
Source code in src/ragwise/pipeline.py
async def delete(self, source: str) -> None:
    """Remove all indexed chunks whose source matches *source*.

    Use for GDPR right-to-erasure or removing a specific document::

        await rag.delete("docs/old-policy.md")
    """
    assert self._store is not None, "RAG must be used as a context manager"
    await self._store.delete(source)

list_sources() async

Return all distinct source paths currently indexed.

Useful for building admin UIs or audit reports::

sources = await rag.list_sources()
Source code in src/ragwise/pipeline.py
async def list_sources(self) -> list[str]:
    """Return all distinct source paths currently indexed.

    Useful for building admin UIs or audit reports::

        sources = await rag.list_sources()
    """
    assert self._store is not None, "RAG must be used as a context manager"
    return await self._store.list_sources()

update(source, path=None) async

Re-ingest a source, replacing all its existing chunks.

If path is omitted, source is used as the file path::

await rag.update("docs/policy.md")
await rag.update("docs/policy.md", path="/new/location/policy.md")
Source code in src/ragwise/pipeline.py
async def update(self, source: str, path: str | None = None) -> IngestResult:
    """Re-ingest a source, replacing all its existing chunks.

    If *path* is omitted, *source* is used as the file path::

        await rag.update("docs/policy.md")
        await rag.update("docs/policy.md", path="/new/location/policy.md")
    """
    assert self._store is not None, "RAG must be used as a context manager"
    await self._store.delete(source)
    return await self.ingest(path or source, force=True)

search(query, *, top_k=5, config=None) async

Return raw hybrid search results — no generation.

Use this to build agent tools, custom pipelines, or debug retrieval::

results = await rag.search("error code 0x80004005", top_k=5)
for r in results:
    print(r.score, r.source, r.text[:120])
Source code in src/ragwise/pipeline.py
async def search(
    self,
    query: str,
    *,
    top_k: int = 5,
    config: QueryConfig | None = None,
) -> list[SearchResult]:
    """Return raw hybrid search results — no generation.

    Use this to build agent tools, custom pipelines, or debug retrieval::

        results = await rag.search("error code 0x80004005", top_k=5)
        for r in results:
            print(r.score, r.source, r.text[:120])
    """
    assert self._searcher is not None, "RAG must be used as a context manager"
    qc = config or QueryConfig()
    return await self._searcher.search(
        query,
        top_k=top_k,
        alpha=qc.alpha,
        tenant_id=qc.tenant_id,
        allowed_sources=qc.allowed_sources if qc.allowed_sources else None,
        as_of=qc.as_of,
        version=qc.version,
    )

list_stale(as_of='now', expiring_soon_days=0) async

Return (expired, expiring_soon) StaleSource lists.

Sources with no valid_until metadata are never returned.

Source code in src/ragwise/pipeline.py
async def list_stale(
    self,
    as_of: str | None = "now",
    expiring_soon_days: int = 0,
) -> tuple[list[Any], list[Any]]:
    """Return (expired, expiring_soon) StaleSource lists.

    Sources with no valid_until metadata are never returned.
    """
    assert self._store is not None, "RAG must be used as a context manager"
    from ragwise.lifecycle import StalenessChecker
    checker = StalenessChecker(self._store)
    return await checker.list_stale(as_of=as_of, expiring_soon_days=expiring_soon_days)

purge_stale(as_of='now') async

Delete all chunks from sources whose valid_until < as_of.

Source code in src/ragwise/pipeline.py
async def purge_stale(self, as_of: str | None = "now") -> Any:
    """Delete all chunks from sources whose valid_until < as_of."""
    assert self._store is not None, "RAG must be used as a context manager"
    from ragwise.lifecycle import StalenessChecker
    checker = StalenessChecker(self._store)
    return await checker.purge_stale(as_of=as_of)

staleness_report(as_of='now', expiring_soon_days=30) async

Return a human-readable staleness report string.

Source code in src/ragwise/pipeline.py
async def staleness_report(
    self,
    as_of: str | None = "now",
    expiring_soon_days: int = 30,
) -> str:
    """Return a human-readable staleness report string."""
    assert self._store is not None, "RAG must be used as a context manager"
    from ragwise.lifecycle import StalenessChecker
    checker = StalenessChecker(self._store)
    return await checker.staleness_report(as_of=as_of, expiring_soon_days=expiring_soon_days)

set_tracer(tracer)

Set a tracer for production observability (e.g. LangfuseTracer).

Returns self for fluent chaining::

rag.set_tracer(LangfuseTracer(...))
Source code in src/ragwise/pipeline.py
def set_tracer(self, tracer: Any) -> RAG:
    """Set a tracer for production observability (e.g. LangfuseTracer).

    Returns self for fluent chaining::

        rag.set_tracer(LangfuseTracer(...))
    """
    self._tracer = tracer
    return self

eval_chunks(docs) async

Score a list of Document chunks for quality metrics.

Source code in src/ragwise/pipeline.py
async def eval_chunks(self, docs: list[Document]) -> ChunkEvalSchema:
    """Score a list of Document chunks for quality metrics."""
    assert self._embedder is not None, "RAG must be used as a context manager"
    from ragwise.eval.chunks import score_chunks

    return await score_chunks(docs, self._embedder, chunk_size=self._config.chunk_size)

eval(dataset) async

Evaluate the RAG pipeline on a labeled dataset using RAGAS.

Source code in src/ragwise/pipeline.py
async def eval(self, dataset: list[dict[str, Any]]) -> EvalSchema:
    """Evaluate the RAG pipeline on a labeled dataset using RAGAS."""
    from ragwise.eval.metrics import evaluate_dataset

    return await evaluate_dataset(dataset)