diff --git a/src/generate/generator.py b/src/generate/generator.py index 86a25e2..d322352 100644 --- a/src/generate/generator.py +++ b/src/generate/generator.py @@ -568,13 +568,30 @@ class Generator: "doc_id": getattr(ctx, "doc_id", "")} system, user = build_grounded_prompt(q, numbered_sources) - # One bounded re-ask when the post-hoc detector says model_abstained. - # Prompt and params stay the same; the sampler is what varies. A second - # abstain is returned exactly as before. insufficient_data never reaches - # this loop. ungrounded_no_citations is not retried. - for attempt in range(2): - result = self.llm.chat(user, system=system, temperature=temperature, - max_tokens=max_tokens) + # Two samples, then one finalize. The re-ask keeps the same grounded + # prompt and params; the sampler is what varies. If both samples are + # plan-only or abstain-shaped, one stricter finalize call asks for the + # cited answer instead of another roll. A third abstain is returned + # exactly as before. insufficient_data never reaches this loop. + # ungrounded_no_citations is not retried. Total LLM calls stay <= 3. + for attempt in range(3): + if attempt < 2: + chat_user, chat_system, chat_temp = user, system, temperature + else: + chat_user = ( + user + + "\n\nFINALIZE: write the final answer now, cite [N]; " + "do not describe your plan." + ) + chat_system = ( + "Write the final answer now. Cite every claim with [N] " + "from the numbered sources. Do not describe your plan. " + "If the sources contain no relevant information, reply with exactly: " + + ABSTAIN_SENTENCE + ) + chat_temp = 0.0 + result = self.llm.chat(chat_user, system=chat_system, + temperature=chat_temp, max_tokens=max_tokens) from .prompt import drop_reasoning_plan answer_text = drop_reasoning_plan(result.text) @@ -677,8 +694,16 @@ class Generator: _tr("decision", stage="abstain_reask", reason="model_abstained", fired=fired) continue + if attempt == 1: + print( + f"[generate] re-ask also model_abstained ({fired}); finalizing once", + flush=True, + ) + _tr("decision", stage="abstain_finalize", reason="model_abstained", + fired=fired) + continue print( - f"[generate] re-ask also model_abstained ({fired}); returning abstain", + f"[generate] finalize also model_abstained ({fired}); returning abstain", flush=True, ) return GroundedAnswer(text=gosterim_metni, citations=citations, diff --git a/tests/unit/test_generate.py b/tests/unit/test_generate.py index 297f9c7..d114fce 100644 --- a/tests/unit/test_generate.py +++ b/tests/unit/test_generate.py @@ -325,7 +325,7 @@ class PostHocAbstainTests(unittest.TestCase): self.assertTrue(result.abstained) self.assertEqual(result.reason, "model_abstained") - self.assertEqual(llm.calls, 2) # one re-ask, both abstain + self.assertEqual(llm.calls, 3) # re-ask + finalize, all abstain self.assertIsNotNone(result.usage) self.assertGreater(result.cost_usd, 0.0) # maliyet SIFIRLANMADI @@ -393,19 +393,50 @@ class ModelAbstainReaskTests(unittest.TestCase): self.assertEqual(again.text, result.text) self.assertEqual(llm.calls, 2) - def test_two_abstains_return_model_abstained(self): - llm = _SequenceLLM([ABSTAIN_SENTENCE, ABSTAIN_SENTENCE]) + def test_two_plans_then_finalize_cited_answer(self): + llm = _SequenceLLM([self._PLAN, self._PLAN, self._CITED]) + gen = _make_generator(llm, scores={"c1": 0.9, "c2": 0.85}) + gen.response_cache = ResponseCache(SQLiteCache(":memory:")) + gen.corpus_version = "v1" + buf = io.StringIO() + with redirect_stdout(buf): + result = gen.answer("DNA nedir?") + self.assertFalse(result.abstained) + self.assertEqual(result.reason, "") + self.assertIn("[1]", result.text) + self.assertEqual(result.citations[0]["chunk_id"], "c1") + self.assertEqual(llm.calls, 3) + self.assertEqual(llm.prompts[0], llm.prompts[1]) + self.assertNotEqual(llm.prompts[2], llm.prompts[0]) + self.assertIn("write the final answer now, cite [N]", llm.prompts[2]) + self.assertIn("[Kaynak 1 |", llm.prompts[2]) + self.assertIn("do not describe your plan", llm.prompts[2]) + self.assertEqual(llm.temperatures, [0.2, 0.2, 0.0]) + self.assertEqual(llm.max_tokens, [700, 700, 700]) + log = buf.getvalue() + self.assertIn("re-asking the LLM once", log) + self.assertIn("finalizing once", log) + self.assertNotIn("returning abstain", log) + with redirect_stdout(io.StringIO()): + again = gen.answer("DNA nedir?") + self.assertTrue(again.cache_hit) + self.assertEqual(llm.calls, 3) + + def test_three_abstains_return_model_abstained(self): + llm = _SequenceLLM([ABSTAIN_SENTENCE, ABSTAIN_SENTENCE, ABSTAIN_SENTENCE]) gen = _make_generator(llm, scores={"c1": 0.9, "c2": 0.85}) buf = io.StringIO() with redirect_stdout(buf): result = gen.answer("DNA nedir?") self.assertTrue(result.abstained) self.assertEqual(result.reason, "model_abstained") - self.assertEqual(llm.calls, 2) + self.assertEqual(llm.calls, 3) self.assertGreater(result.cost_usd, 0.0) log = buf.getvalue() self.assertIn("re-asking the LLM once", log) self.assertIn("re-ask also model_abstained (looks_like_abstain)", log) + self.assertIn("finalizing once", log) + self.assertIn("finalize also model_abstained (looks_like_abstain)", log) def test_insufficient_data_does_not_call_llm(self): llm = _SequenceLLM([self._PLAN, self._CITED]) @@ -1082,7 +1113,7 @@ class ResponseCacheIntegrationTests(unittest.TestCase): gen.answer("DNA nedir?") gen.answer("DNA nedir?") - self.assertEqual(llm.calls, 4, "model_abstained sonucu cache'lenmediği için her answer() bir re-ask daha yapar") + self.assertEqual(llm.calls, 6, "model_abstained sonucu cache'lenmediği için her answer() re-ask ve finalize yapar") self.assertEqual(gen.cache_hits, 0) def test_no_response_cache_behaves_exactly_as_before(self):