File size: 32,458 Bytes
7376e73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
"""ProTeGi: Prompt Optimization with Textual Gradients.

Reference paper:
    "Automatic Prompt Optimization with Gradient Descent and Beam Search"
    (Pryzant et al., 2023) — https://arxiv.org/abs/2305.03495

What is ProTeGi?
----------------
ProTeGi treats prompt optimization like gradient descent in ML:
    - "loss"      = low ROUGE / faithfulness score on examples
    - "gradient"  = an LLM-generated text that says *why* the prompt fails
    - "step"      = a new prompt that the LLM proposes to fix the gradient

Instead of numeric gradients (like backprop), the gradients are in *natural
language* — hence "textual gradients". This makes the method model-agnostic
and interpretable.

Algorithm (one iteration):
    1. EVALUATE  : run Summarizer with current_prompt on train set
                   → compute composite metric per example
    2. FIND WORST: select K examples with lowest scores
                   → these are the "signal" (where the prompt fails)
    3. GRADIENT  : LLM analyzes the failures → writes a textual gradient
                   "The prompt fails because it does not ask for X..."
    4. CANDIDATES: LLM proposes `beam_size` improved prompts
                   inspired by the gradient
    5. VALIDATE  : evaluate each candidate on a held-out validation set
    6. SELECT    : keep the candidate with the highest average score

Repeat for n_iterations. Return the prompt with the best validation score.

Integration in this project:
    - Task: Article → ArticleSummary (Summarizer agent)
    - Metric: ROUGE-L + LLM Judge composite (see summary_metrics.py)
    - Dataset: PubMed article-abstract pairs (from Kaggle)
    - Optimizes: SYSTEM_PROMPT of src/agents/summarizer.py
"""

from __future__ import annotations

import json
import logging
import time
from dataclasses import dataclass, field
from datetime import datetime
from pathlib import Path
from typing import TYPE_CHECKING, Optional

from pydantic import BaseModel, Field

if TYPE_CHECKING:
    from src.llm.base import LLMClient

logger = logging.getLogger(__name__)


# ============================================================
# Data structures
# ============================================================


@dataclass
class Example:
    """One training / validation example.

    Attributes:
        article_text : input fed to the Summarizer (truncated article body)
        reference    : gold summary (e.g. PubMed author-written abstract)
        article_id   : optional unique ID for tracing
    """

    article_text: str
    reference: str
    article_id: str = ""


@dataclass
class EvalResult:
    """Per-example evaluation result under one prompt."""

    example_id: str
    composite: float           # primary optimization target
    rouge1: float
    rouge2: float
    rougeL: float
    faithfulness: float = 0.5  # default = neutral (when Judge disabled)
    coverage: float = 0.5
    generated: str = ""        # first 300 chars of generated text (for debug)
    error: Optional[str] = None


@dataclass
class IterationResult:
    """Summary of one ProTeGi iteration (for the history log)."""

    iteration: int
    current_prompt: str
    train_composite: float
    val_composite: float        # best candidate score on validation
    improved: bool
    gradient_text: str = ""
    n_candidates_tried: int = 0
    best_candidate_preview: str = ""  # first 200 chars of winning candidate


class ProTeGiResult(BaseModel):
    """Final output of a ProTeGi optimization run — Pydantic for JSON export."""

    best_prompt: str = Field(..., description="Optimized system prompt")
    initial_prompt: str = Field(..., description="Starting system prompt")
    n_iterations: int
    initial_composite: float = Field(..., ge=0.0, le=1.0)
    final_composite: float = Field(..., ge=0.0, le=1.0)
    improvement_pct: float = Field(
        ..., description="Relative improvement vs initial, in percent"
    )
    iterations: list[dict] = Field(
        default_factory=list, description="Per-iteration detail log"
    )
    duration_seconds: float
    optimized_at: datetime = Field(default_factory=datetime.now)
    prompt_saved_to: Optional[str] = None


# ============================================================
# Meta-LLM system prompts (used internally by ProTeGi)
# ============================================================

_GRADIENT_SYSTEM = """\
You are an expert at improving AI prompts for scientific paper summarization.

You will receive:
  1. CURRENT PROMPT: the system prompt being evaluated
  2. FAILURE EXAMPLES: cases where the current prompt produced poor summaries
     (low ROUGE-L overlap with the gold abstract, or low faithfulness scores)

Your job is to write a GRADIENT — a paragraph that precisely diagnoses
what the current prompt is failing to produce. Cover:
  - What patterns of error appear across the failures
    (e.g. "the prompt produces vague problem statements")
  - What specific instruction is missing or poorly worded
    (e.g. "the prompt does not ask for quantitative results")
  - What concrete change would fix the problem
    (e.g. "add 'include specific numbers such as accuracy or F1 score'")

Be concrete and reference the actual failure examples. The gradient is
used by another LLM to write improved prompts — make it actionable."""


_CANDIDATE_SYSTEM = """\
You are an expert prompt engineer specializing in scientific NLP tasks.

You will receive:
  1. ORIGINAL PROMPT: the current system prompt for a scientific summarizer
  2. GRADIENT: an analysis of what the prompt is doing wrong
  3. N: how many improved prompt variants to generate

Produce exactly N improved prompts. Each improved prompt must:
  - Be a complete, self-contained system prompt (not a patch, the full text)
  - Fix the specific issues identified in the gradient
  - Keep the core task: extract structured summary from a scientific abstract
  - Keep the same 6-field output structure:
      problem, method, dataset, results, limitations, key_contributions
  - Stay under 600 words
  - Not add new output fields (would break the Pydantic schema downstream)

Output exactly a valid JSON array of strings, nothing else:
["prompt 1 full text...", "prompt 2 full text...", ...]
No markdown, no explanation — only the JSON array."""

# ── Narrative-mode variants of the two meta-prompts ──────────────────────────
# Used when ProTeGiOptimizer is created with narrative_mode=True.
# The key difference: candidates must produce a prose paragraph, NOT a JSON
# object with named fields.  ROUGE directly compares the paragraph to the
# PubMed gold abstract — so writing style and lexical overlap matter.

_GRADIENT_SYSTEM_NARRATIVE = """\
You are an expert at improving AI prompts for scientific paper summarization.

You will receive:
  1. CURRENT PROMPT: the system prompt being evaluated
  2. FAILURE EXAMPLES: cases where the current prompt produced poor summaries
     (low ROUGE-L overlap with the PubMed gold abstract, or low faithfulness)

Your job is to write a GRADIENT — a paragraph that precisely diagnoses
what the current prompt is failing to produce. Cover:
  - What patterns of error appear across the failures
    (e.g. "the prompt omits quantitative findings")
  - What specific instruction is missing or poorly worded
    (e.g. "the prompt does not ask for specific numbers")
  - What concrete change would fix the problem
    (e.g. "add 'include statistics such as p-values and percentages'")

The summary format is a NARRATIVE PARAGRAPH (prose), not structured fields.
Be concrete and reference the actual failure examples. The gradient is
used by another LLM to write improved prompts — make it actionable."""

_CANDIDATE_SYSTEM_NARRATIVE = """\
You are an expert prompt engineer specializing in scientific NLP tasks.

You will receive:
  1. ORIGINAL PROMPT: the current system prompt for a scientific summarizer
  2. GRADIENT: an analysis of what the prompt is doing wrong
  3. N: how many improved prompt variants to generate

Produce exactly N improved prompts. Each improved prompt must:
  - Be a complete, self-contained system prompt (not a patch, the full text)
  - Fix the specific issues identified in the gradient
  - Keep the core task: write a NARRATIVE PARAGRAPH summarizing a scientific article
  - The output format is prose (150-250 words), NOT a JSON object or bullet list
  - Stay under 400 words
  - Not introduce structured fields or JSON output

Output exactly a valid JSON array of strings, nothing else:
["prompt 1 full text...", "prompt 2 full text...", ...]
No markdown, no explanation — only the JSON array."""


# ============================================================
# ProTeGi optimizer class
# ============================================================


class ProTeGiOptimizer:
    """Optimize a Summarizer system prompt via iterative textual gradient descent.

    This class is framework-agnostic: it only needs LLMClient instances, not
    a full LangGraph state. It can be used standalone or wired into the agent
    pipeline.

    Args:
        summarize_llm : LLMClient used to generate summaries during evaluation
                        (recommended: gpt-4o-mini for cost efficiency)
        gradient_llm  : LLMClient used for gradient + candidate generation
                        (can be the same as summarize_llm, or a stronger model)
        judge_llm     : LLMClient for LLM Judge evaluation
                        (defaults to gradient_llm if not provided)
        n_iterations  : Number of optimization cycles (3 is a good default)
        beam_size     : How many candidate prompts to generate per iteration
                        (4 gives good coverage, >6 becomes expensive)
        n_worst       : Number of worst examples to use as gradient signal
                        (5 is enough; more adds noise)
        use_llm_judge : Whether to include LLM Judge in the composite metric
                        (set False for fast/cheap runs)
        use_rouge     : Whether to include ROUGE in the composite metric.
                        Set False when the reference format does not match the
                        generated format (e.g. PubMed narrative abstract vs
                        structured 6-field summary). Requires use_llm_judge=True
                        to keep a meaningful signal.
    """

    def __init__(
        self,
        summarize_llm: "LLMClient",
        gradient_llm: "LLMClient",
        judge_llm: Optional["LLMClient"] = None,
        n_iterations: int = 3,
        beam_size: int = 4,
        n_worst: int = 5,
        use_llm_judge: bool = True,
        use_rouge: bool = True,
        narrative_mode: bool = False,
    ) -> None:
        self.summarize_llm = summarize_llm
        self.gradient_llm = gradient_llm
        self.judge_llm = judge_llm or gradient_llm
        self.n_iterations = n_iterations
        self.beam_size = beam_size
        self.n_worst = n_worst
        self.use_llm_judge = use_llm_judge
        self.use_rouge = use_rouge
        self.narrative_mode = narrative_mode

    # ------------------------------------------------------------------
    # Public API
    # ------------------------------------------------------------------

    def optimize(
        self,
        initial_prompt: str,
        train_examples: list[Example],
        val_examples: list[Example],
        save_dir: Optional[Path] = None,
        run_name: str = "",
    ) -> ProTeGiResult:
        """Run the full ProTeGi loop and return the best prompt.

        Args:
            initial_prompt  : starting SYSTEM_PROMPT (e.g. from summarizer.py)
            train_examples  : used to compute gradient (find failures)
            val_examples    : held-out set used to select best candidate
            save_dir        : if provided, saves the best prompt + results JSON
            run_name        : optional label included in saved filenames
                              e.g. "llama-haiku-sonnet" →
                              summarizer_prompt_llama-haiku-sonnet_20260526.txt

        Returns:
            ProTeGiResult with best_prompt and full iteration history.
        """
        start_time = datetime.now()
        current_prompt = initial_prompt
        history: list[dict] = []

        logger.info(
            "ProTeGi start: %d iterations, beam=%d, judge=%s",
            self.n_iterations, self.beam_size, self.use_llm_judge,
        )
        logger.info(
            "  Dataset: %d train | %d val examples",
            len(train_examples), len(val_examples),
        )

        # ── Baseline: evaluate initial prompt on validation set ───────────
        baseline_results = self._evaluate_set(initial_prompt, val_examples)
        baseline_composite = _mean(baseline_results, "composite")
        best_composite = baseline_composite
        best_prompt = initial_prompt

        logger.info("  Baseline composite (val): %.4f", baseline_composite)

        # ── Main optimization loop ────────────────────────────────────────
        for i in range(self.n_iterations):
            logger.info("=== Iteration %d / %d ===", i + 1, self.n_iterations)

            # Step 1: evaluate current prompt on TRAIN set to find failures
            train_results = self._evaluate_set(current_prompt, train_examples)
            train_composite = _mean(train_results, "composite")

            # Step 2: select K worst examples (gradient signal)
            worst = sorted(train_results, key=lambda r: r.composite)[: self.n_worst]
            logger.info(
                "  Worst %d (train) composite: %.4f ... %.4f",
                self.n_worst,
                worst[0].composite if worst else 0,
                worst[-1].composite if worst else 0,
            )

            # Map worst results back to their Example objects
            id_to_example = {ex.article_id: ex for ex in train_examples}
            worst_examples = [
                id_to_example.get(r.example_id)
                for r in worst
                if r.example_id in id_to_example
            ]
            worst_examples = [e for e in worst_examples if e is not None]

            # Step 3: generate textual gradient
            gradient = self._generate_gradient(current_prompt, worst, worst_examples)
            logger.info("  ┌─ GRADIENT (diagnosis) ─────────────────────────")
            for line in gradient.splitlines():
                logger.info("  │  %s", line)
            logger.info("  └────────────────────────────────────────────────")

            # Step 4: generate beam_size candidate prompts
            candidates = self._generate_candidates(current_prompt, gradient)
            logger.info("  Generated %d valid candidates", len(candidates))

            # Step 5: evaluate each candidate on VALIDATION set
            best_candidate = current_prompt
            best_candidate_composite = best_composite  # must beat the current best

            candidates_detail: list[dict] = []
            for j, candidate in enumerate(candidates):
                # ── Display candidate prompt ──────────────────────────────
                logger.info(
                    "  ┌─ Candidate %d/%d ─────────────────────────────────",
                    j + 1, len(candidates),
                )
                for line in candidate.splitlines():
                    logger.info("  │  %s", line)
                logger.info(
                    "  └────────────────────────────────────────────────────"
                )

                # Pause between candidates so Groq's 60-second TPM window can
                # partially reset. Without this, all N_val Groq calls from the
                # previous candidate accumulate and push us over the 6 000 TPM
                # free-tier cap the moment the next candidate evaluation starts.
                if j > 0 and self._uses_groq():
                    pause = 25
                    logger.info(
                        "  [Groq TPM pause] waiting %ds before candidate %d/%d …",
                        pause, j + 1, len(candidates),
                    )
                    time.sleep(pause)

                cand_results = self._evaluate_set(candidate, val_examples)
                cand_composite = _mean(cand_results, "composite")
                is_best = cand_composite > best_candidate_composite
                logger.info(
                    "  Candidate %d/%d score: composite=%.4f  %s",
                    j + 1, len(candidates), cand_composite,
                    "★ NEW BEST" if is_best else "",
                )
                candidates_detail.append({
                    "rank":      j + 1,
                    "prompt":    candidate,
                    "composite": round(cand_composite, 4),
                    "rouge1":    round(_mean(cand_results, "rouge1"), 4),
                    "rouge2":    round(_mean(cand_results, "rouge2"), 4),
                    "rougeL":    round(_mean(cand_results, "rougeL"), 4),
                    "faithfulness": round(_mean(cand_results, "faithfulness"), 4),
                    "coverage":     round(_mean(cand_results, "coverage"), 4),
                    "is_best":   is_best,
                })
                if is_best:
                    best_candidate_composite = cand_composite
                    best_candidate = candidate

            # Step 6: update if improved
            improved = best_candidate_composite > best_composite
            if improved:
                delta = best_candidate_composite - best_composite
                logger.info(
                    "  [IMPROVED] +%.4f -> new composite=%.4f",
                    delta, best_candidate_composite,
                )
                best_composite = best_candidate_composite
                best_prompt = best_candidate
                current_prompt = best_candidate
            else:
                logger.info("  [NO CHANGE] keeping current prompt")

            history.append({
                "iteration":               i + 1,
                "train_composite":         round(train_composite, 4),
                "best_candidate_composite": round(best_candidate_composite, 4),
                "improved":                improved,
                # Full texts saved for the detail file
                "gradient":                gradient,
                "candidates":              candidates_detail,
                # Short previews kept for the summary JSON (backward compat)
                "gradient_preview":        gradient[:300],
                "n_candidates":            len(candidates),
                "best_candidate_preview":  best_candidate[:200],
            })

        # ── Final metrics ─────────────────────────────────────────────────
        duration = (datetime.now() - start_time).total_seconds()
        improvement_pct = (
            (best_composite - baseline_composite) / max(baseline_composite, 1e-6) * 100
        )

        logger.info(
            "ProTeGi done: %.4f -> %.4f (%+.1f%%) in %.1fs",
            baseline_composite, best_composite, improvement_pct, duration,
        )

        # ── Save results ──────────────────────────────────────────────────
        saved_path: Optional[str] = None
        if save_dir is not None:
            saved_path = str(
                self._save_results(
                    save_dir=save_dir,
                    best_prompt=best_prompt,
                    run_name=run_name,
                    metadata={
                        "run_name": run_name,
                        "initial_composite": round(baseline_composite, 4),
                        "final_composite": round(best_composite, 4),
                        "improvement_pct": round(improvement_pct, 2),
                        "n_iterations": self.n_iterations,
                        "beam_size": self.beam_size,
                        "n_worst": self.n_worst,
                        "use_llm_judge": self.use_llm_judge,
                        "iterations": history,
                    },
                )
            )

        return ProTeGiResult(
            best_prompt=best_prompt,
            initial_prompt=initial_prompt,
            n_iterations=self.n_iterations,
            initial_composite=round(baseline_composite, 4),
            final_composite=round(best_composite, 4),
            improvement_pct=round(improvement_pct, 2),
            iterations=history,
            duration_seconds=round(duration, 1),
            prompt_saved_to=saved_path,
        )

    # ------------------------------------------------------------------
    # Internal: helpers
    # ------------------------------------------------------------------

    def _uses_groq(self) -> bool:
        """Return True if the summarize LLM is backed by Groq.

        Used to decide whether to insert inter-candidate TPM pause: Groq's
        free tier has a 6 000 TPM cap that requires pacing; other providers
        (Anthropic, OpenAI) have much higher limits and don't need the pause.
        """
        provider = getattr(self.summarize_llm, "provider", "")
        # GroqClient reports provider="openai" (reuses the OpenAI SDK) but its
        # base_url contains "groq" — check the class name as a fallback.
        return (
            provider == "groq"
            or "groq" in type(self.summarize_llm).__name__.lower()
        )

    # ------------------------------------------------------------------
    # Internal: evaluate
    # ------------------------------------------------------------------

    def _evaluate_set(
        self, prompt: str, examples: list[Example]
    ) -> list[EvalResult]:
        """Run the Summarizer on all examples, compute metrics for each."""
        from src.llm.base import Message
        from src.optimization.summary_metrics import (
            compute_all_metrics,
            flatten_summary,
        )
        from src.schemas import ArticleSummary

        results: list[EvalResult] = []
        for ex in examples:
            try:
                # Format input the same way summarizer.py does
                user_text = (
                    f"Title: Scientific Article\n\n"
                    f"Abstract:\n{ex.article_text}\n"
                )
                messages = [Message(role="user", content=user_text)]

                if self.narrative_mode:
                    # Narrative mode: plain text call, no schema validation
                    resp = self.summarize_llm.chat(
                        system=prompt,
                        messages=messages,
                        temperature=0.0,
                        max_tokens=512,
                    )
                    generated_text = resp.content.strip()
                else:
                    # Structured mode: force 6-field JSON output
                    parsed, _resp = self.summarize_llm.chat_structured(
                        system=prompt,
                        messages=messages,
                        schema=ArticleSummary,
                        temperature=0.0,
                        max_tokens=1024,
                    )
                    parsed.article_id = ex.article_id or "protegi_eval"
                    generated_text = flatten_summary(parsed)
                metrics = compute_all_metrics(
                    generated=generated_text,
                    reference=ex.reference,
                    article_text=ex.article_text if self.use_llm_judge else None,
                    llm=self.judge_llm if self.use_llm_judge else None,
                    use_rouge=self.use_rouge,
                )

                results.append(
                    EvalResult(
                        example_id=ex.article_id or "?",
                        composite=float(metrics.get("composite", 0.0)),
                        rouge1=float(metrics.get("rouge1", 0.0)),
                        rouge2=float(metrics.get("rouge2", 0.0)),
                        rougeL=float(metrics.get("rougeL", 0.0)),
                        faithfulness=float(metrics.get("faithfulness", 0.5)),
                        coverage=float(metrics.get("coverage", 0.5)),
                        generated=generated_text[:300],
                    )
                )

            except Exception as exc:
                logger.warning(
                    "Evaluation failed for example %s: %s",
                    ex.article_id, exc,
                )
                results.append(
                    EvalResult(
                        example_id=ex.article_id or "?",
                        composite=0.0,
                        rouge1=0.0,
                        rouge2=0.0,
                        rougeL=0.0,
                        error=str(exc),
                    )
                )

        return results

    # ------------------------------------------------------------------
    # Internal: gradient generation
    # ------------------------------------------------------------------

    def _generate_gradient(
        self,
        current_prompt: str,
        worst_results: list[EvalResult],
        worst_examples: list[Example],
    ) -> str:
        """Ask gradient_llm to diagnose what the prompt is doing wrong."""
        from src.llm.base import Message

        # Build compact failure descriptions
        failure_lines: list[str] = []
        for i, r in enumerate(worst_results[:self.n_worst]):
            # Try to find the matching example for context
            ex = next(
                (e for e in worst_examples if e.article_id == r.example_id),
                None,
            )
            article_snippet = (ex.article_text[:300] if ex else "[unavailable]")
            failure_lines.append(
                f"Example {i + 1} (id={r.example_id}):\n"
                f"  Scores: composite={r.composite:.4f}, "
                f"ROUGE-L={r.rougeL:.4f}, "
                f"faithfulness={r.faithfulness:.2f}\n"
                f"  Article excerpt: {article_snippet}...\n"
                f"  Generated: {r.generated[:250]}..."
            )

        failure_text = "\n\n".join(failure_lines)
        user_text = (
            f"CURRENT PROMPT:\n{current_prompt}\n\n"
            f"FAILURE EXAMPLES (lowest scoring):\n{failure_text}"
        )
        messages = [Message(role="user", content=user_text)]

        gradient_sys = _GRADIENT_SYSTEM_NARRATIVE if self.narrative_mode else _GRADIENT_SYSTEM
        try:
            resp = self.gradient_llm.chat(
                system=gradient_sys,
                messages=messages,
                temperature=0.3,
                max_tokens=600,
            )
            return resp.content.strip()
        except Exception as exc:
            logger.warning("Gradient generation failed: %s", exc)
            # Minimal fallback gradient
            return (
                "The prompt needs to explicitly request quantitative results "
                "(numbers, percentages, dataset names) and discourage vague "
                "or generic language."
            )

    # ------------------------------------------------------------------
    # Internal: candidate generation
    # ------------------------------------------------------------------

    def _generate_candidates(
        self, current_prompt: str, gradient: str
    ) -> list[str]:
        """Ask gradient_llm to propose beam_size improved prompts."""
        from src.llm.base import Message

        user_text = (
            f"ORIGINAL PROMPT:\n{current_prompt}\n\n"
            f"GRADIENT (what is wrong and how to fix it):\n{gradient}\n\n"
            f"N: {self.beam_size}\n\n"
            f"Generate exactly {self.beam_size} improved prompt variants "
            f"as a JSON array of strings."
        )
        messages = [Message(role="user", content=user_text)]

        candidate_sys = _CANDIDATE_SYSTEM_NARRATIVE if self.narrative_mode else _CANDIDATE_SYSTEM
        try:
            resp = self.gradient_llm.chat(
                system=candidate_sys,
                messages=messages,
                temperature=0.7,          # some diversity between candidates
                max_tokens=self.beam_size * 700,  # ~600 words per prompt
            )
            content = resp.content.strip()

            # Extract JSON array (the LLM might add explanation before/after)
            start = content.find("[")
            end = content.rfind("]") + 1
            if start != -1 and end > start:
                parsed = json.loads(content[start:end])
                if isinstance(parsed, list):
                    valid = [p for p in parsed if isinstance(p, str) and len(p) > 50]
                    if valid:
                        return valid[: self.beam_size]

        except json.JSONDecodeError as exc:
            logger.warning("Candidate JSON parsing failed: %s", exc)
        except Exception as exc:
            logger.warning("Candidate generation failed: %s", exc)

        # Fallback: return the current prompt as the only "candidate"
        logger.warning("Falling back to current prompt (no valid candidates)")
        return [current_prompt]

    # ------------------------------------------------------------------
    # Internal: persistence
    # ------------------------------------------------------------------

    @staticmethod
    def _save_results(
        save_dir: Path, best_prompt: str, metadata: dict, run_name: str = ""
    ) -> Path:
        """Write the best prompt and run metadata to disk.

        Creates three files (all prefixed with run_name when provided):
            {save_dir}/summarizer_prompt_{run_name}_{timestamp}.txt
            {save_dir}/protegi_results_{run_name}_{timestamp}.json
            {save_dir}/protegi_detail_{run_name}_{timestamp}.json
        """
        import re
        save_dir.mkdir(parents=True, exist_ok=True)
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")

        # Sanitize run_name for use in filenames (replace unsafe chars with _)
        safe_name = re.sub(r"[^\w\-]", "_", run_name).strip("_") if run_name else ""
        prefix = f"{safe_name}_" if safe_name else ""

        # 1. Best prompt plain text
        prompt_path = save_dir / f"summarizer_prompt_{prefix}{timestamp}.txt"
        prompt_path.write_text(best_prompt, encoding="utf-8")
        logger.info("Saved optimized prompt : %s", prompt_path)

        # 2. Summary metadata (compact — no full prompt/gradient texts)
        summary = {k: v for k, v in metadata.items() if k != "iterations"}
        summary["iterations"] = [
            {k: v for k, v in it.items() if k not in ("gradient", "candidates")}
            for it in metadata.get("iterations", [])
        ]
        meta_path = save_dir / f"protegi_results_{prefix}{timestamp}.json"
        meta_path.write_text(json.dumps(summary, indent=2), encoding="utf-8")
        logger.info("Saved ProTeGi summary   : %s", meta_path)

        # 3. Full detail file — gradient + all candidate prompts + scores
        detail_path = save_dir / f"protegi_detail_{prefix}{timestamp}.json"
        detail_path.write_text(
            json.dumps(metadata, indent=2, ensure_ascii=False), encoding="utf-8"
        )
        logger.info("Saved ProTeGi detail    : %s", detail_path)

        return prompt_path


# ============================================================
# Helpers
# ============================================================


def _mean(results: list[EvalResult], attr: str) -> float:
    """Compute the mean of a numeric attribute across EvalResult list."""
    if not results:
        return 0.0
    values = [getattr(r, attr, 0.0) for r in results]
    return sum(values) / len(values)