raglab.llm#
LLM-backed roles — the Formulator and Evaluator (ir_09 §8 steps 1 & 3).
raglab’s no-LLM thin slice (raglab.agent) runs offline: an identity
formulator and a pass-through evaluator. This module supplies the two LLM roles
that turn that slice into a real agent — query understanding and the back-edge:
make_llm_formulator()— query rewrite / expand / HyDE. A thin adapter over ir’sir.make_llm_formulator()(theformulate=seam, ir_09 §3): ir owns the lazy-oarewriter and the identity fallback; raglab only adapts the shapestr -> str | [str]to the role contract(SubTask, source) -> [LowLevelQuery].make_llm_evaluator()— sufficiency + refinement.ir.selectowns the relevance decision (the calibrated committed subset, ir_09 §3); the LLM owns sufficiency — informed by ir’s model-freeir.Selection.sufficienthint, it judges whether the committed subset actually satisfies the goal and, when it does not, emits arefinementSubTask. That refinement is the back-edge that makes the loop an agent rather than a DAG.
Two load-bearing boundaries (ir ↔ raglab), guarded here:
A Formulator returns queries, never SubTasks — decomposition is the Planner’s job.
make_llm_formulator()only ever yieldsLowLevelQuerys.The back-edge lives in raglab, never in ir: ir derives a
sufficientsignal from its own selection; raglab’s Evaluator is what reads it, decides, and re-queries.
Both builders mirror ir.select.make_llm_selector(): an injectable callable
(a test double, or your own router), built lazily on oa only when omitted
(so import raglab stays offline), with a safe fallback — a formulator must
never make retrieval worse than the raw query, and an evaluator must never
fabricate an endless loop (on any failure it returns no refinement, which is the
loop’s break condition).
Module Attributes
Default prompt for |
Functions
|
An LLM-backed raglab |
|
An LLM-backed raglab |
- raglab.llm.EVALUATION_PROMPT = 'A search agent is pursuing this goal:\n\n{goal}\n\nA calibrated selector reviewed the retrieved candidates and committed to the\nresults below (it abstained if this list is empty):\n\n{results}\n\nDecide whether these results are SUFFICIENT to satisfy the goal.\n- If they are, reply with exactly: SUFFICIENT\n- If they are not, reply with: INSUFFICIENT\n then, on the next line, a single improved search query that would retrieve what\n is still missing. Keep it a terse search phrase — no prose, no numbering.\n'#
Default prompt for
make_llm_evaluator()— judge sufficiency, else emit a single improved query (the refinement that drives the back-edge).
- raglab.llm.make_llm_evaluator(*, judge=None, select_strategy='conservative', select_kwargs=None, prerank=None, prompt='A search agent is pursuing this goal:\n\n{goal}\n\nA calibrated selector reviewed the retrieved candidates and committed to the\nresults below (it abstained if this list is empty):\n\n{results}\n\nDecide whether these results are SUFFICIENT to satisfy the goal.\n- If they are, reply with exactly: SUFFICIENT\n- If they are not, reply with: INSUFFICIENT\n then, on the next line, a single improved search query that would retrieve what\n is still missing. Keep it a terse search phrase — no prose, no numbering.\n', max_result_chars=500, **prompt_function_kwargs)[source]#
An LLM-backed raglab
Evaluator(turns on the back-edge).Relevance is ir’s: each round the accumulated results are passed through
ir.select()and the committed subset becomes theJudgement.relevant(so the LLM stays in its lane — LLM relevance is known-fragile, ir_01 §3). Sufficiency is the LLM’s: it reads the committed subset (informed by ir’s model-freesufficienthint via abstention) and decides whether the goal is satisfied. When it is not, the judge’s improved query becomes arefinementSubTaskover the same sources — the back-edge.- Parameters:
judge (
Optional[Callable[...,tuple[bool,str|None] |str]]) – an injectable(goal, results) -> (sufficient, refinement)callable (a test double, or your own router); a raw text reply is also accepted and parsed. When omitted it is built lazily onoa.select_strategy (
str) – ir selection strategy for the relevance decision (default"conservative"— distractor-robust).select_kwargs (
Mapping[str,Any] |None) – extra args forwarded toir.select()(e.g.max_k,rel).min_scoreis allowed only while the round’s pool keeps raw scores (single-source, orprerank=score_reranker): an absolute floor is per-(corpus, mode, embedder), so a round whose pool was rank-fused across sources raises rather than silently mass-abstaining on ordinal RRF scores.prerank (
Reranker|None) – the per-round merge that puts the accumulated pool best-first (and deduped) beforeir.select. Defaults torrf_reranker()— a round’s pool can already mix sources (a SubTask spans several), so the same rank-based, scale-safe merge as the fan-in applies; single-source rounds keep raw scores. Injectscore_reranker()for sources known to share one score scale.max_result_chars (
int) – per-result text truncation in the judge prompt.
- Return type:
Safe fallback: any judge error returns
refinement=None— the control loop’s break condition — so a failing judge can never fabricate an endless loop. Sufficiency without a refinement query is likewise treated as a stop.
- raglab.llm.make_llm_formulator(*, formulate=None, params=None, **make_kwargs)[source]#
An LLM-backed raglab
Formulator.Adapts an ir-style query formulator (
str -> str | [str]) to the role contract(SubTask, source) -> [LowLevelQuery]: the sub-goal text is rewritten / expanded into one or more search queries, each wrapped as aLowLevelQueryagainstsource.- Parameters:
formulate (
Optional[Callable[[str],str|Sequence[str]]]) – an injectable ir-style formulator (a test double, or one you built). When omitted it is built once viair.make_llm_formulator()(**make_kwargsare forwarded — e.g.n,prompt,rewriter); that call is offline (oais imported lazily only when the formulator is invoked), soimport raglabstays offline.params (
Mapping[str,Any] |None) – per-call retriever overrides (e.g.{"mode": "hybrid", "k": 5}) attached to every emittedLowLevelQuery.
- Return type:
The boundary: this returns queries, never SubTasks. ir’s formulator already falls back to identity on any failure, so the emitted list is never empty — a formulator must never make retrieval worse than the raw sub-goal.