forge
Explore
Log in

Don't you dare go Hollow

Finalize once when both abstain samples are plan-only

Browse files at this commit

d40a041bba079f43026237f3fe98c88a7d873d55Ahmet Kaan GümüşParent: f1d5ed2
  • fix
    • finalize once when both abstain samples are plan-only
Show full message
fix: finalize once when both abstain samples are plan-only

A second identical abstain was still returned to the user. After two plan-only or abstain-shaped samples, issue one stricter finalize call (same chunks, temperature 0, cite [N], no plan). Return model_abstained only if that third call abstains too. insufficient_data is not retried.
Local-Gate: f6961a141b20239d76b47e46ff1ea1df1ecf01b6 (1411 passed, 32s)
src/generate/generator.pymodified+33 -8
@@ -568,13 +568,30 @@ class Generator:
568 "doc_id": getattr(ctx, "doc_id", "")}568 "doc_id": getattr(ctx, "doc_id", "")}
569 569
570 system, user = build_grounded_prompt(q, numbered_sources)570 system, user = build_grounded_prompt(q, numbered_sources)
571- # One bounded re-ask when the post-hoc detector says model_abstained.571+ # Two samples, then one finalize. The re-ask keeps the same grounded
572- # Prompt and params stay the same; the sampler is what varies. A second572+ # prompt and params; the sampler is what varies. If both samples are
573- # abstain is returned exactly as before. insufficient_data never reaches573+ # plan-only or abstain-shaped, one stricter finalize call asks for the
574- # this loop. ungrounded_no_citations is not retried.574+ # cited answer instead of another roll. A third abstain is returned
575- for attempt in range(2):575+ # exactly as before. insufficient_data never reaches this loop.
576- result = self.llm.chat(user, system=system, temperature=temperature,576+ # ungrounded_no_citations is not retried. Total LLM calls stay <= 3.
577- max_tokens=max_tokens)577+ for attempt in range(3):
578+ if attempt < 2:
579+ chat_user, chat_system, chat_temp = user, system, temperature
580+ else:
581+ chat_user = (
582+ user
583+ + "\n\nFINALIZE: write the final answer now, cite [N]; "
584+ "do not describe your plan."
585+ )
586+ chat_system = (
587+ "Write the final answer now. Cite every claim with [N] "
588+ "from the numbered sources. Do not describe your plan. "
589+ "If the sources contain no relevant information, reply with exactly: "
590+ + ABSTAIN_SENTENCE
591+ )
592+ chat_temp = 0.0
593+ result = self.llm.chat(chat_user, system=chat_system,
594+ temperature=chat_temp, max_tokens=max_tokens)
578 from .prompt import drop_reasoning_plan595 from .prompt import drop_reasoning_plan
579 answer_text = drop_reasoning_plan(result.text)596 answer_text = drop_reasoning_plan(result.text)
580 597
@@ -677,8 +694,16 @@ class Generator:
677 _tr("decision", stage="abstain_reask", reason="model_abstained",694 _tr("decision", stage="abstain_reask", reason="model_abstained",
678 fired=fired)695 fired=fired)
679 continue696 continue
697+ if attempt == 1:
698+ print(
699+ f"[generate] re-ask also model_abstained ({fired}); finalizing once",
700+ flush=True,
701+ )
702+ _tr("decision", stage="abstain_finalize", reason="model_abstained",
703+ fired=fired)
704+ continue
680 print(705 print(
681- f"[generate] re-ask also model_abstained ({fired}); returning abstain",706+ f"[generate] finalize also model_abstained ({fired}); returning abstain",
682 flush=True,707 flush=True,
683 )708 )
684 return GroundedAnswer(text=gosterim_metni, citations=citations,709 return GroundedAnswer(text=gosterim_metni, citations=citations,
tests/unit/test_generate.pymodified+36 -5
@@ -325,7 +325,7 @@ class PostHocAbstainTests(unittest.TestCase):
325 325
326 self.assertTrue(result.abstained)326 self.assertTrue(result.abstained)
327 self.assertEqual(result.reason, "model_abstained")327 self.assertEqual(result.reason, "model_abstained")
328- self.assertEqual(llm.calls, 2) # one re-ask, both abstain328+ self.assertEqual(llm.calls, 3) # re-ask + finalize, all abstain
329 self.assertIsNotNone(result.usage)329 self.assertIsNotNone(result.usage)
330 self.assertGreater(result.cost_usd, 0.0) # maliyet SIFIRLANMADI330 self.assertGreater(result.cost_usd, 0.0) # maliyet SIFIRLANMADI
331 331
@@ -393,19 +393,50 @@ class ModelAbstainReaskTests(unittest.TestCase):
393 self.assertEqual(again.text, result.text)393 self.assertEqual(again.text, result.text)
394 self.assertEqual(llm.calls, 2)394 self.assertEqual(llm.calls, 2)
395 395
396- def test_two_abstains_return_model_abstained(self):396+ def test_two_plans_then_finalize_cited_answer(self):
397- llm = _SequenceLLM([ABSTAIN_SENTENCE, ABSTAIN_SENTENCE])397+ llm = _SequenceLLM([self._PLAN, self._PLAN, self._CITED])
398+ gen = _make_generator(llm, scores={"c1": 0.9, "c2": 0.85})
399+ gen.response_cache = ResponseCache(SQLiteCache(":memory:"))
400+ gen.corpus_version = "v1"
401+ buf = io.StringIO()
402+ with redirect_stdout(buf):
403+ result = gen.answer("DNA nedir?")
404+ self.assertFalse(result.abstained)
405+ self.assertEqual(result.reason, "")
406+ self.assertIn("[1]", result.text)
407+ self.assertEqual(result.citations[0]["chunk_id"], "c1")
408+ self.assertEqual(llm.calls, 3)
409+ self.assertEqual(llm.prompts[0], llm.prompts[1])
410+ self.assertNotEqual(llm.prompts[2], llm.prompts[0])
411+ self.assertIn("write the final answer now, cite [N]", llm.prompts[2])
412+ self.assertIn("[Kaynak 1 |", llm.prompts[2])
413+ self.assertIn("do not describe your plan", llm.prompts[2])
414+ self.assertEqual(llm.temperatures, [0.2, 0.2, 0.0])
415+ self.assertEqual(llm.max_tokens, [700, 700, 700])
416+ log = buf.getvalue()
417+ self.assertIn("re-asking the LLM once", log)
418+ self.assertIn("finalizing once", log)
419+ self.assertNotIn("returning abstain", log)
420+ with redirect_stdout(io.StringIO()):
421+ again = gen.answer("DNA nedir?")
422+ self.assertTrue(again.cache_hit)
423+ self.assertEqual(llm.calls, 3)
424+
425+ def test_three_abstains_return_model_abstained(self):
426+ llm = _SequenceLLM([ABSTAIN_SENTENCE, ABSTAIN_SENTENCE, ABSTAIN_SENTENCE])
398 gen = _make_generator(llm, scores={"c1": 0.9, "c2": 0.85})427 gen = _make_generator(llm, scores={"c1": 0.9, "c2": 0.85})
399 buf = io.StringIO()428 buf = io.StringIO()
400 with redirect_stdout(buf):429 with redirect_stdout(buf):
401 result = gen.answer("DNA nedir?")430 result = gen.answer("DNA nedir?")
402 self.assertTrue(result.abstained)431 self.assertTrue(result.abstained)
403 self.assertEqual(result.reason, "model_abstained")432 self.assertEqual(result.reason, "model_abstained")
404- self.assertEqual(llm.calls, 2)433+ self.assertEqual(llm.calls, 3)
405 self.assertGreater(result.cost_usd, 0.0)434 self.assertGreater(result.cost_usd, 0.0)
406 log = buf.getvalue()435 log = buf.getvalue()
407 self.assertIn("re-asking the LLM once", log)436 self.assertIn("re-asking the LLM once", log)
408 self.assertIn("re-ask also model_abstained (looks_like_abstain)", log)437 self.assertIn("re-ask also model_abstained (looks_like_abstain)", log)
438+ self.assertIn("finalizing once", log)
439+ self.assertIn("finalize also model_abstained (looks_like_abstain)", log)
409 440
410 def test_insufficient_data_does_not_call_llm(self):441 def test_insufficient_data_does_not_call_llm(self):
411 llm = _SequenceLLM([self._PLAN, self._CITED])442 llm = _SequenceLLM([self._PLAN, self._CITED])
@@ -1082,7 +1113,7 @@ class ResponseCacheIntegrationTests(unittest.TestCase):
1082 gen.answer("DNA nedir?")1113 gen.answer("DNA nedir?")
1083 gen.answer("DNA nedir?")1114 gen.answer("DNA nedir?")
1084 1115
1085- self.assertEqual(llm.calls, 4, "model_abstained sonucu cache'lenmediği için her answer() bir re-ask daha yapar")1116+ self.assertEqual(llm.calls, 6, "model_abstained sonucu cache'lenmediği için her answer() re-ask ve finalize yapar")
1086 self.assertEqual(gen.cache_hits, 0)1117 self.assertEqual(gen.cache_hits, 0)
1087 1118
1088 def test_no_response_cache_behaves_exactly_as_before(self):1119 def test_no_response_cache_behaves_exactly_as_before(self):