Hiyerarsik ozet modelin YAPMADIGI atiflari donduruyordu
Browse files at this commitaf70856dddcce88231179a5a97dbc83cd7b062c9Parent: 00225a4- fix
- hiyerarsik ozet modelin YAPMADIGI atiflari donduruyordu
2 changed files +130 -8
- src/summarize/summarizer.py +30 -5
- tests/unit/test_summarize.py +100 -3
src/summarize/summarizer.pymodified+30 -5
| @@ -219,15 +219,37 @@ class Summarizer: | |||
| 219 | system, user = build_summary_prompt(merge_blocks, scope_label) | 219 | system, user = build_summary_prompt(merge_blocks, scope_label) |
| 220 | result = self.deepseek.chat(user, system=system, temperature=temperature, | 220 | result = self.deepseek.chat(user, system=system, temperature=temperature, |
| 221 | max_tokens=max_tokens) | 221 | max_tokens=max_tokens) |
| 222 | - citations = [{"n": i, "span_ids": src["span_ids"], "pages": src["pages"]} | 222 | + # M2-3 (#55, EXP-010/ACC-01) -- KOSULARAK KANITLANMIS HATA: |
| 223 | - for i, src in merge_lookup.items() if src["span_ids"] or src["pages"]] | 223 | + # burada `_parse_citation_ns(result.text)` CAGRILMIYORDU; `citations` |
| 224 | + # dogrudan `merge_lookup.items()`'tan, yani KANITI OLAN TUM ara-ozetlerden | ||
| 225 | + # uretiliyordu. Kosulan kanit: 9 birim / mupg=3 -> 3 ara-ozet; model nihai | ||
| 226 | + # metinde YALNIZ [1] atifladi, cikti 3 atif dondu (s.1, s.4, s.7). | ||
| 227 | + # s.4 ve s.7 MODELIN YAPMADIGI atiflardi. Docstring "merge yeni span/sayfa | ||
| 228 | + # uydurmaz" diyordu -- dogru, ama KOD uyduruyordu. Ozet yuzeyinde atif | ||
| 229 | + # precision'i yapisal olarak 1/grup_sayisi'na dusuyordu. | ||
| 230 | + # Cozum tek-gecis yolundaki desenin AYNISI: yalniz atiflanan N'ler. | ||
| 231 | + cited_ns = sorted(set(_parse_citation_ns(result.text))) | ||
| 232 | + citations = [] | ||
| 233 | + for n in cited_ns: | ||
| 234 | + src = merge_lookup.get(n) | ||
| 235 | + if src is None: # hayalet atif -> sessiz eleme | ||
| 236 | + continue | ||
| 237 | + if not (src["span_ids"] or src["pages"]): | ||
| 238 | + continue # kaniti olmayan ara-ozet | ||
| 239 | + citations.append({"n": n, "span_ids": src["span_ids"], "pages": src["pages"]}) | ||
| 224 | usd = pricing_cost_usd(result.model, result.usage) | 240 | usd = pricing_cost_usd(result.model, result.usage) |
| 225 | self._record_call(usage=result.usage, model=result.model, n_items=len(summaries), | 241 | self._record_call(usage=result.usage, model=result.model, n_items=len(summaries), |
| 226 | - note=f"özet hiyerarşik-birleştirme: {len(summaries)} ara-özet") | 242 | + note=f"özet hiyerarşik-birleştirme: {len(summaries)} ara-özet, " |
| 243 | + f"{len(citations)} atıflandı") | ||
| 227 | scope_pages = sorted({p for gs in summaries for p in gs.scope_pages}) | 244 | scope_pages = sorted({p for gs in summaries for p in gs.scope_pages}) |
| 245 | + # #55 (ikinci yari): `abstained=False` SABITTI -> nihai merge | ||
| 246 | + # NO_CONTENT_SENTENCE donse bile "gercek ozet" isaretleniyordu. | ||
| 247 | + llm_abstained = result.text.strip() == NO_CONTENT_SENTENCE | ||
| 228 | return GroundedSummary(text=result.text, citations=citations, scope_pages=scope_pages, | 248 | return GroundedSummary(text=result.text, citations=citations, scope_pages=scope_pages, |
| 229 | n_source_units=sum(gs.n_source_units for gs in summaries), | 249 | n_source_units=sum(gs.n_source_units for gs in summaries), |
| 230 | - abstained=False, reason="", usage=result.usage, | 250 | + abstained=llm_abstained, |
| 251 | + reason="llm_no_content" if llm_abstained else "", | ||
| 252 | + usage=result.usage, | ||
| 231 | cost_usd=usd, latency_s=result.latency_s, hierarchical=True) | 253 | cost_usd=usd, latency_s=result.latency_s, hierarchical=True) |
| 232 | 254 | ||
| 233 | def _summarize_hierarchical(self, units: list[CanonicalUnit], *, scope_label: str, | 255 | def _summarize_hierarchical(self, units: list[CanonicalUnit], *, scope_label: str, |
| @@ -263,8 +285,11 @@ class Summarizer: | |||
| 263 | level = new_level | 285 | level = new_level |
| 264 | 286 | ||
| 265 | final = level[0] | 287 | final = level[0] |
| 288 | + # #55: burada da `abstained=False` SABITTI -- son seviyenin cekimserligi | ||
| 289 | + # (tek grup varsa tek-gecisin, coksa merge'in) yutuluyordu. | ||
| 266 | return GroundedSummary(text=final.text, citations=final.citations, | 290 | return GroundedSummary(text=final.text, citations=final.citations, |
| 267 | scope_pages=sorted({u.page for u in units}), | 291 | scope_pages=sorted({u.page for u in units}), |
| 268 | - n_source_units=len(units), abstained=False, reason="", | 292 | + n_source_units=len(units), |
| 293 | + abstained=final.abstained, reason=final.reason, | ||
| 269 | usage=_sum_usage(all_usages), cost_usd=total_cost, | 294 | usage=_sum_usage(all_usages), cost_usd=total_cost, |
| 270 | latency_s=total_latency, hierarchical=True) | 295 | latency_s=total_latency, hierarchical=True) |
tests/unit/test_summarize.pymodified+100 -3
| @@ -290,9 +290,14 @@ class HierarchicalTests(unittest.TestCase): | |||
| 290 | def test_hierarchical_drops_group_with_no_valid_citation(self): | 290 | def test_hierarchical_drops_group_with_no_valid_citation(self): |
| 291 | # KANITLI ÖZET bütünlüğü: bir grup kendi biriminde HİÇ atıf yapmazsa nihai | 291 | # KANITLI ÖZET bütünlüğü: bir grup kendi biriminde HİÇ atıf yapmazsa nihai |
| 292 | # atıf listesine EKLENMEZ (boş atıf = sahte/anlamsız kanıt; v1 testinde 143 | 292 | # atıf listesine EKLENMEZ (boş atıf = sahte/anlamsız kanıt; v1 testinde 143 |
| 293 | - # birim için 2 atıftan biri boş çıkmıştı). Kanıt üreten gruplar kalır; sahte | 293 | + # birim için 2 atıftan biri boş çıkmıştı). |
| 294 | - # doldurma YOK (pass-bias yasak). Atıf listesi = özetin dayandığı TÜM kanıt | 294 | + # |
| 295 | - # grupları (yalnız merge-LLM'in [N]'lediği değil). | 295 | + # DÜZELTME (#55, 2026-09-11): bu testin eski açıklaması "atıf listesi = |
| 296 | + # özetin dayandığı TÜM kanıt grupları (yalnız merge-LLM'in [N]'lediği | ||
| 297 | + # değil)" diyordu — o cümle HATANIN KENDİSİNİ sözleşme olarak kaydediyordu. | ||
| 298 | + # Artık kural tek-geçiş yolundakiyle aynı: yalnız modelin GERÇEKTEN | ||
| 299 | + # atıfladığı [N]'ler çıktıya girer. Bu test o kuralla da geçer çünkü | ||
| 300 | + # merge yalnız [2]'yi atıflıyor. | ||
| 296 | units = [_Unit("s1", 1, "m1"), _Unit("s2", 2, "m2"), _Unit("s3", 3, "m3")] | 301 | units = [_Unit("s1", 1, "m1"), _Unit("s2", 2, "m2"), _Unit("s3", 3, "m3")] |
| 297 | texts = [ | 302 | texts = [ |
| 298 | NO_CONTENT_SENTENCE, # grup1 (s1,s2): atıf yok -> ATLANIR | 303 | NO_CONTENT_SENTENCE, # grup1 (s1,s2): atıf yok -> ATLANIR |
| @@ -311,6 +316,98 @@ class HierarchicalTests(unittest.TestCase): | |||
| 311 | self.assertEqual(c_by_n[2]["pages"], [3]) | 316 | self.assertEqual(c_by_n[2]["pages"], [3]) |
| 312 | 317 | ||
| 313 | 318 | ||
| 319 | +class HierarchicalCitationFabricationTests(unittest.TestCase): | ||
| 320 | + """#55 (EXP-010/ACC-01) — hiyerarşik özet MODELİN YAPMADIĞI atıfları döndürüyordu. | ||
| 321 | + | ||
| 322 | + `_merge_summaries` nihai metni alıyor ama `_parse_citation_ns(result.text)` | ||
| 323 | + ÇAĞIRMIYORDU; `citations` doğrudan `merge_lookup.items()`'tan, yani *kanıtı | ||
| 324 | + olan TÜM ara-özetlerden* üretiliyordu. Docstring "merge yeni span/sayfa | ||
| 325 | + uydurmaz" diyordu — doğru, ama **kod uyduruyordu**. Özet yüzeyinde atıf | ||
| 326 | + precision'ı yapısal olarak `1/grup_sayısı`'na düşüyordu. | ||
| 327 | + | ||
| 328 | + Kapı **A-08** (%100): dönen her atıf modelin metinde yazdığı bir `[N]` | ||
| 329 | + olmalı. | ||
| 330 | + """ | ||
| 331 | + | ||
| 332 | + def _kos(self, texts, units, mupg=3): | ||
| 333 | + ds = _StubDeepSeek(texts=texts) | ||
| 334 | + s = Summarizer(ds, cost_recorder=_noop_recorder, max_units_per_group=mupg) | ||
| 335 | + return s.summarize(units), ds | ||
| 336 | + | ||
| 337 | + def _dokuz(self): | ||
| 338 | + return [_Unit(f"s{i}", i, f"metin{i}") for i in range(1, 10)] | ||
| 339 | + | ||
| 340 | + def test_denetimde_kosulan_senaryo_tek_atif_tek_citation(self): | ||
| 341 | + """Denetimin kanıtı: 9 birim / mupg=3 → 3 ara-özet; model nihai metinde | ||
| 342 | + YALNIZ [1] atıfladı ama çıktı 3 atıf (s.1, s.4, s.7) döndürüyordu.""" | ||
| 343 | + texts = ["Grup1 [1].", "Grup2 [1].", "Grup3 [1].", "Nihai özet [1]."] | ||
| 344 | + res, _ = self._kos(texts, self._dokuz()) | ||
| 345 | + self.assertEqual([c["n"] for c in res.citations], [1]) | ||
| 346 | + self.assertEqual(res.citations[0]["pages"], [1]) | ||
| 347 | + sayfalar = {p for c in res.citations for p in c["pages"]} | ||
| 348 | + self.assertNotIn(4, sayfalar) # modelin YAPMADIĞI atıflar | ||
| 349 | + self.assertNotIn(7, sayfalar) | ||
| 350 | + | ||
| 351 | + def test_iki_atif_iki_citation(self): | ||
| 352 | + texts = ["Grup1 [1].", "Grup2 [1].", "Grup3 [1].", "Nihai [1][3]."] | ||
| 353 | + res, _ = self._kos(texts, self._dokuz()) | ||
| 354 | + self.assertEqual([c["n"] for c in res.citations], [1, 3]) | ||
| 355 | + self.assertEqual({p for c in res.citations for p in c["pages"]}, {1, 7}) | ||
| 356 | + | ||
| 357 | + def test_hic_atif_yoksa_citation_da_yok(self): | ||
| 358 | + """Dayanaksız özet sessizce kanıtlı görünmemeli.""" | ||
| 359 | + texts = ["Grup1 [1].", "Grup2 [1].", "Grup3 [1].", "Nihai özet, atıf yok."] | ||
| 360 | + res, _ = self._kos(texts, self._dokuz()) | ||
| 361 | + self.assertEqual(res.citations, []) | ||
| 362 | + | ||
| 363 | + def test_hayalet_atif_sessizce_elenir(self): | ||
| 364 | + """Kaynak sayısını aşan [N] patlamaya değil sessiz elemeye yol açar | ||
| 365 | + (generator.py ile TUTARLI).""" | ||
| 366 | + texts = ["Grup1 [1].", "Grup2 [1].", "Grup3 [1].", "Nihai [1][9]."] | ||
| 367 | + res, _ = self._kos(texts, self._dokuz()) | ||
| 368 | + self.assertEqual([c["n"] for c in res.citations], [1]) | ||
| 369 | + | ||
| 370 | + def test_her_donen_atif_metinde_gercekten_yaziyor(self): | ||
| 371 | + """Kapı A-08 değişmezi: citations ⊆ metindeki [N]'ler.""" | ||
| 372 | + import re | ||
| 373 | + texts = ["G1 [1].", "G2 [1].", "G3 [1].", "Nihai [2] ve ayrıca [3]."] | ||
| 374 | + res, _ = self._kos(texts, self._dokuz()) | ||
| 375 | + metindeki = {int(n) for n in re.findall(r"\[(\d+)\]", res.text)} | ||
| 376 | + self.assertTrue({c["n"] for c in res.citations} <= metindeki) | ||
| 377 | + self.assertEqual({c["n"] for c in res.citations}, {2, 3}) | ||
| 378 | + | ||
| 379 | + def test_merge_cekimser_olursa_abstained_true(self): | ||
| 380 | + """İkinci yarı: `abstained=False` SABİTTİ → nihai merge 'içerik yok' | ||
| 381 | + dönse bile 'gerçek özet' işaretleniyordu.""" | ||
| 382 | + texts = ["G1 [1].", "G2 [1].", "G3 [1].", NO_CONTENT_SENTENCE] | ||
| 383 | + res, _ = self._kos(texts, self._dokuz()) | ||
| 384 | + self.assertTrue(res.abstained) | ||
| 385 | + self.assertEqual(res.reason, "llm_no_content") | ||
| 386 | + self.assertEqual(res.citations, []) | ||
| 387 | + | ||
| 388 | + def test_cekimser_olmayan_merge_abstained_false_kalir(self): | ||
| 389 | + texts = ["G1 [1].", "G2 [1].", "G3 [1].", "Nihai özet [1]."] | ||
| 390 | + res, _ = self._kos(texts, self._dokuz()) | ||
| 391 | + self.assertFalse(res.abstained) | ||
| 392 | + self.assertEqual(res.reason, "") | ||
| 393 | + | ||
| 394 | + def test_ozyinelemeli_seviyede_de_gecerli(self): | ||
| 395 | + """mupg=2 ile 9 birim → 5 ara-özet → 3 merge → 2 merge → 1: ara | ||
| 396 | + seviyelerde de yalnız atıflanan N taşınmalı.""" | ||
| 397 | + texts = ["G [1]." for _ in range(5)] + ["M [1]." for _ in range(10)] | ||
| 398 | + res, ds = self._kos(texts, self._dokuz(), mupg=2) | ||
| 399 | + self.assertGreater(ds.calls, 6) | ||
| 400 | + self.assertEqual(len(res.citations), 1) | ||
| 401 | + self.assertEqual(res.citations[0]["pages"], [1]) | ||
| 402 | + | ||
| 403 | + def test_maliyet_hala_tum_seviyelerin_toplami(self): | ||
| 404 | + """Atıf kırpma maliyeti SIFIRLAMAMALI — LLM gerçekten çağrıldı.""" | ||
| 405 | + texts = ["G1 [1].", "G2 [1].", "G3 [1].", "Nihai, atıf yok."] | ||
| 406 | + res, ds = self._kos(texts, self._dokuz()) | ||
| 407 | + self.assertGreater(res.cost_usd, 0.0) | ||
| 408 | + self.assertEqual(res.usage.output, ds.calls * 5) | ||
| 409 | + | ||
| 410 | + | ||
| 314 | # --------------------------------------------------------------------------- cost_recorder spy | 411 | # --------------------------------------------------------------------------- cost_recorder spy |
| 315 | 412 | ||
| 316 | class CostRecorderSpyTests(unittest.TestCase): | 413 | class CostRecorderSpyTests(unittest.TestCase): |