Repository navigation
Expand file tree
/
Copy pathanswer.py
More file actions
431 lines (362 loc) · 18.3 KB
/
Copy pathanswer.py
File metadata and controls
431 lines (362 loc) · 18.3 KB
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
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
"""Answer -- neighbor expansion (opt.) -> char budget -> context synthesis
(opt.) -> generation.
Takes the fragments ``Retrieve`` already ranked and threshold-filtered,
expands them with neighboring chunks, trims to the character budget,
optionally synthesizes a facts briefing via RECOMP instead of feeding raw
chunks, and streams the final generation.
``run()`` consumes ``ChatModel.stream()``'s ``GenerationChunk`` items directly
rather than plain text: it appends ``chunk.text`` to the answer (and forwards
it to ``on_token``, for a caller streaming to a web client) while it goes,
and once the final chunk arrives (``chunk.done``) it copies the generation
metadata into ``AnswerResult.metrics["generation"]``, with the
``tokens_per_second`` derivation, reachable from a plain ``ChatModel`` caller.
Persisting the debug dump itself stays a caller concern
(``guardar_debug_rag``).
RECOMP synthesis lives here because ``Answer``'s use-case description
explicitly includes "sintetizar contexto (opcional)" and no equivalent exists
anywhere else -- skipping it would leave the use case unable to reproduce the
pipeline with ``USAR_RECOMP_SYNTHESIS`` on (off by default since issue #571). Its silent
fallback-to-raw-context on failure is kept as an explicit, visible decision
in ``_synthesize_recomp`` (three separate conditions -- LLM call failure,
empty synthesis, missing header -- each falls back to
``build_context_for_model``), matching the original's behavior; per the
``ChatModel`` port's failure policy this is an allowed "explicit fallback
built by the caller out of two ports", not a silently degrading adapter.
"""
import dataclasses
from typing import Any, Callable, Dict, Iterator, List, Optional, Sequence
from monkeygrab.application.context_assembly import (
RECOMP_FACTS_HEADER,
marcar_fragmento_incompleto,
texto_fuente_fragmento,
build_context_for_model,
normalize_recomp_output,
optimize_context_text,
strip_citation_markers,
strip_ollama_think_blocks,
)
from monkeygrab.application.text_chunking import adjacent_chunk_ids
from monkeygrab.config.app_config import AppConfig
from monkeygrab.config.flags import PipelineFlagsConfig
from monkeygrab.domain.fragment import Fragment
from monkeygrab.domain.generation_chunk import GenerationChunk
from monkeygrab.ports.chat_model import ChatModel
from monkeygrab.ports.vector_store import VectorStore
@dataclasses.dataclass
class AnswerResult:
"""Output of ``Answer.run``.
Attributes:
text: Complete generated answer.
metrics: Observability data (fragments expanded/discarded, whether
RECOMP synthesis ran, context char counts).
"""
text: str
metrics: Dict[str, Any]
def _is_expandable(fragment: Fragment) -> bool:
"""Return True when neighbor expansion makes sense for this fragment.
Moved from ``rag.engine.generation._fragmento_expandible``. The
original also checks ``"chunk" in metadata`` (legacy dict metadata could
omit the key); ``ChunkMetadata.chunk`` always has a default of 0, so
that check is trivially true here and is dropped.
"""
if fragment.metadata.format == "image":
return False
return fragment.metadata.total_chunks_in_page is not None
def _expand_with_neighbors(
fragments: Sequence[Fragment],
vector_store: VectorStore,
n_top_for_expansion: int,
) -> "tuple[List[Fragment], int]":
"""Append adjacent chunks for the top selected text fragments.
Moved from ``rag.engine.generation._expandir_fragmentos_contexto``.
Unlike the original's ``try/except Exception: pass`` around
``collection.get``, a ``VectorStore`` failure here propagates -- the
port's failure policy is hard-fail, and unlike ``ChatModel``/``Reranker``
its docstring does not sanction a caller-level silent-skip fallback for
this call.
Args:
fragments: Fragments to consider for expansion (assumed already
ranked best-first).
vector_store: Used to fetch neighbor chunks by id.
n_top_for_expansion: Only the first this-many fragments are
considered as expansion anchors.
Returns:
Tuple of (fragments with neighbor additions appended, number added).
"""
if not fragments:
return list(fragments), 0
expanded = list(fragments)
used_ids = {f.id for f in expanded}
additions: List[Fragment] = []
for frag in expanded[:n_top_for_expansion]:
if not _is_expandable(frag):
continue
neighbor_ids = adjacent_chunk_ids(
frag.metadata, n_neighbors=1,
previous_page_total=_previous_page_total(frag, vector_store),
)
if not neighbor_ids:
continue
for neighbor in vector_store.get_by_ids(neighbor_ids):
if neighbor.id not in used_ids:
additions.append(neighbor)
used_ids.add(neighbor.id)
if additions:
expanded.extend(additions)
return expanded, len(additions)
def _previous_page_total(frag: Fragment, vector_store: VectorStore) -> Optional[int]:
"""Chunk count of the page before ``frag``'s, read from that page's chunk 0.
Only asked for the first chunk of a page, whose contiguous neighbours are
the previous page's last chunks (issue #397). Every chunk of a page
carries the page's count, so one lookup by id is enough; a page that
produced no chunks (dropped as too short) answers None and the expansion
skips that side.
"""
meta = frag.metadata
if meta.chunk != 0 or meta.page <= 0:
return None
probe = vector_store.get_by_ids([f"{meta.source}_pag{meta.page - 1}_chunk0"])
return probe[0].metadata.total_chunks_in_page if probe else None
def _limit_by_char_budget(
fragments: Sequence[Fragment], max_context_chars: int
) -> "tuple[List[Fragment], int]":
"""Apply the retrieved-evidence character budget before context synthesis.
Moved from ``rag.engine.generation._limitar_fragmentos_por_chars``.
"""
contexto_total = sum(len(f.doc) for f in fragments)
if contexto_total <= max_context_chars:
return list(fragments), 0
fragmentos_truncados = []
chars_acum = 0
for f in fragments:
if chars_acum + len(f.doc) > max_context_chars:
break
fragmentos_truncados.append(f)
chars_acum += len(f.doc)
return fragmentos_truncados, len(fragments) - len(fragmentos_truncados)
# Field names to copy from a done GenerationChunk into AnswerResult's
# "generation" metrics -- literal match of _GEN_STATS_KEYS in
# rag/engine/generation.py, applied to the typed chunk instead of a raw dict.
_GEN_STATS_FIELDS = (
"model", "done_reason", "total_duration", "load_duration",
"prompt_eval_count", "prompt_eval_duration", "eval_count", "eval_duration",
)
def _generation_stats(chunk: GenerationChunk) -> Dict[str, Any]:
"""Build the "generation" metrics dict from the final streamed chunk.
Moved from the stats-copying half of ``generar_tokens_respuesta`` (only
keys actually present in the Ollama ``done`` line are copied -- here,
fields the adapter left ``None`` because Ollama's line did not report
them) plus the tokens-per-second derivation: both used to be unreachable from any
``ChatModel``-only caller once ``stream()`` returned plain text, which is
exactly the gap this port change closes.
"""
stats: Dict[str, Any] = {
field: value
for field, value in ((f, getattr(chunk, f)) for f in _GEN_STATS_FIELDS)
if value is not None
}
if chunk.eval_count and chunk.eval_duration:
stats["tokens_per_second"] = chunk.eval_count / (chunk.eval_duration / 1e9)
return stats
class Answer:
"""Neighbor expansion (opt.) -> char budget -> context synthesis (opt.) -> generation.
``recomp_chat_model`` is ``Optional``: when not wired in, RECOMP
synthesis is skipped regardless of ``flags.usar_recomp_synthesis`` and
the raw formatted context is used instead -- there is nothing to invoke
otherwise, same pattern as ``Retrieve``'s optional ports.
"""
def __init__(
self,
vector_store: VectorStore,
rag_chat_model: ChatModel,
config: AppConfig,
recomp_chat_model: Optional[ChatModel] = None,
system_prompt: Optional[str] = None,
):
"""Args:
vector_store: Used for neighbor-chunk expansion.
rag_chat_model: ``ChatModel`` wired to the "rag" role; its
``stream`` produces the final answer.
config: Root config; ``flags``, ``context`` and
``retrieval.n_top_for_expansion`` are read fresh on every
``run()``.
recomp_chat_model: ``ChatModel`` wired to the "recomp" role, or
``None`` to disable RECOMP synthesis.
system_prompt: System prompt for the final generation
(``SYSTEM_PROMPT_RAG`` in ``rag/chat_pdfs.py``). Not sourced
from ``AppConfig`` -- system prompts are not part of it (see
``AppConfig``'s module docstring); the caller wiring this use
case supplies it explicitly.
"""
self._vector_store = vector_store
self._rag_chat_model = rag_chat_model
self._recomp_chat_model = recomp_chat_model
self._system_prompt = system_prompt
self._config = config
def select_evidence(
self, fragments: Sequence[Fragment]
) -> "tuple[List[Fragment], Dict[str, Any]]":
"""Turn ranked fragments into the evidence the generator will see.
Expands the top fragments with their neighbours, recovering sentences
the chunk boundary cut in half, then trims the result to the character
budget so the prompt cannot outgrow the model's context window.
Args:
fragments: Ranked, threshold-filtered fragments from ``Retrieve``.
Returns:
Tuple of (evidence fragments, metrics).
"""
config = self._config
expanded, n_expanded = _expand_with_neighbors(
fragments, self._vector_store, config.retrieval.n_top_for_expansion
) if config.flags.expandir_contexto else (list(fragments), 0)
limited, n_discarded = _limit_by_char_budget(expanded, config.context.max_context_chars)
metrics = {
"fragments_expanded": n_expanded,
"fragments_discarded_by_chars": n_discarded,
"fragments_final": len(limited),
}
return limited, metrics
def build_user_message(
self, question: str, fragments: Sequence[Fragment]
) -> "tuple[str, Dict[str, Any]]":
"""Build the message the generator answers, question plus context.
The context is either the formatted chunks or, when RECOMP synthesis
is enabled, a compressed briefing of the facts they contain.
Args:
question: User question, also the focus RECOMP synthesizes toward.
fragments: Evidence from ``select_evidence``.
Returns:
Tuple of (user message, context metrics).
"""
context_str, metrics = self._build_context(fragments, question, self._config.flags)
return f"{question}\n\n<context>{context_str}</context>", metrics
def stream(self, user_message: str) -> "Iterator[GenerationChunk]":
"""Stream the generator's answer to an already-built user message.
Kept separate from ``build_user_message`` because a caller streaming to
a web client needs to send the cited sources before the first token
arrives, and cannot do that if preparing the prompt and generating are
one call.
Args:
user_message: Output of ``build_user_message``.
Yields:
Generation chunks; the final one carries the timing and token
counts.
"""
return self._rag_chat_model.stream(user_message, system=self._system_prompt)
def run(
self,
question: str,
fragments: Sequence[Fragment],
on_token: Optional[Callable[[str], None]] = None,
) -> AnswerResult:
"""Run neighbor expansion, char budgeting, context synthesis and generation.
The whole path in one call, for callers that do not need to do anything
between preparing the evidence and generating from it.
Args:
question: User question (also used as the RECOMP synthesis focus).
fragments: Ranked, threshold-filtered fragments from ``Retrieve``.
on_token: Optional callback invoked with each streamed token.
Returns:
``AnswerResult`` with the complete answer text and metrics.
"""
limited, evidence_metrics = self.select_evidence(fragments)
user_message, context_metrics = self.build_user_message(question, limited)
text = ""
generation_stats: Dict[str, Any] = {}
for chunk in self.stream(user_message):
if chunk.text:
text += chunk.text
if on_token is not None:
on_token(chunk.text)
if chunk.done:
generation_stats = _generation_stats(chunk)
metrics: Dict[str, Any] = {
**evidence_metrics,
"generation": generation_stats,
**context_metrics,
}
return AnswerResult(text=text, metrics=metrics)
def _build_context(
self, fragments: Sequence[Fragment], question: str, flags: PipelineFlagsConfig
) -> "tuple[str, Dict[str, Any]]":
if not flags.usar_recomp_synthesis or not fragments or self._recomp_chat_model is None:
context_str, metrics = build_context_for_model(fragments, flags.usar_optimizacion_contexto)
return context_str, {**metrics, "recomp_used": False}
return self._synthesize_recomp(fragments, question, flags)
def _synthesize_recomp(
self, fragments: Sequence[Fragment], question: str, flags: PipelineFlagsConfig
) -> "tuple[str, Dict[str, Any]]":
"""Synthesize context using the RECOMP model instead of raw chunks.
Falls back to ``build_context_for_model`` in three conditions: no
usable evidence text, the LLM call fails, or the
synthesized output is too short / missing the expected header.
"""
textos_preparados = []
for f in fragments:
cuerpo = texto_fuente_fragmento(f.doc or "")
if flags.usar_optimizacion_contexto:
cuerpo = optimize_context_text(cuerpo)
# The truncation marker describes the excerpt, not the cleanup:
# it must survive with optimization disabled too (issue #397).
cuerpo = marcar_fragmento_incompleto(cuerpo)
content = cuerpo.replace("\n", " ").strip()
content = strip_citation_markers(content)
if content:
n = len(textos_preparados) + 1
textos_preparados.append(f"Fragment {n}:\n{content}")
contexto_raw = "\n\n".join(textos_preparados)
if not contexto_raw.strip():
context_str, metrics = build_context_for_model(fragments, flags.usar_optimizacion_contexto)
return context_str, {**metrics, "recomp_used": False}
q = (question or "").strip()
bloque_pregunta = (
f"## User question\n{q}\n"
if q
else "## User question\n(No question provided; extract the main technical facts from the excerpts.)\n"
)
system_prompt = (
"You compress retrieved document excerpts into a brief briefing for a downstream "
"answer model.\n"
"GROUNDING:\n"
"- Use ONLY information stated in the evidence excerpts. No outside knowledge.\n"
"- Preserve technical terms, notation, formulas, and numbers exactly as written.\n"
"ENUMERATION (critical):\n"
"- If the question asks for a list or a count (e.g. 'three types', 'two ways'), "
"search ALL fragments and enumerate EVERY item you find, even if items are spread "
"across different fragments. Never say an item 'is not mentioned' if it appears "
"anywhere in the excerpts.\n"
"- When an excerpt ends with [excerpt ends mid-sentence], the list may continue in "
"another fragment — collect items from ALL fragments before writing your bullets.\n"
"STYLE:\n"
"- Write ONLY facts that help answer the user question. Do NOT describe the documents, "
"the paper, or the excerpts (forbidden openers: \"This paper\", \"The excerpt\", "
"\"This section\", \"The document\", \"The text\", \"The fragment\").\n"
"- Do NOT restate meta-summaries; every bullet must be substantive content from the excerpts.\n"
"- Do NOT cite fragment numbers, sources, or page numbers.\n"
"OUTPUT FORMAT (exactly this structure, markdown):\n"
"## Facts relevant to the question\n"
"- (first grounded fact)\n"
"- (second grounded fact)\n"
"Use one bullet per distinct fact; merge duplicates. If nothing in the excerpts bears "
"on the question, output exactly one bullet: "
"\"Insufficient evidence in the excerpts to answer the question.\"\n"
"Language: same as the evidence excerpts (or the user question if excerpts mix languages)."
)
user_prompt = (
f"{bloque_pregunta}"
"## Evidence excerpts (verbatim from retrieval; may be partial)\n"
f"{contexto_raw}\n\n"
"Produce the briefing using the required OUTPUT FORMAT. No text before "
"## Facts relevant to the question."
)
try:
raw = self._recomp_chat_model.generate(user_prompt, system=system_prompt)
except Exception:
# Explicit use-case-level fallback -- see the module docstring.
context_str, metrics = build_context_for_model(fragments, flags.usar_optimizacion_contexto)
return context_str, {**metrics, "recomp_used": False, "recomp_error": True}
sintesis = normalize_recomp_output(strip_ollama_think_blocks(raw.strip()))
if len(sintesis) < 20 or RECOMP_FACTS_HEADER.lower() not in sintesis.lower():
context_str, metrics = build_context_for_model(fragments, flags.usar_optimizacion_contexto)
return context_str, {**metrics, "recomp_used": False}
return sintesis, {"recomp_used": True, "chars_original": len(contexto_raw), "chars_optimized": len(sintesis)}