File size: 11,948 Bytes
cacd4d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Candidate and Feedback Collector for Presentation

This module collects all candidates generated during optimization along with
their feedback, scores, and metadata for presentation purposes.
"""

import json
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Any, Optional
from dataclasses import dataclass, asdict, field


@dataclass
class CandidateInfo:
    """Information about a single candidate prompt"""
    iteration: int
    candidate_id: str
    source: str  # "GEPA_Reflection", "LLEGO_Crossover", "LLEGO_Mutation", "Seed"
    prompt: str
    score: Optional[float] = None
    feedback: Optional[str] = None
    feedback_details: Optional[Dict[str, Any]] = None
    timestamp: str = field(default_factory=lambda: datetime.now().isoformat())


@dataclass
class IterationInfo:
    """Information about a single optimization iteration"""
    iteration: int
    candidates: List[CandidateInfo] = field(default_factory=list)
    best_candidate: Optional[CandidateInfo] = None
    best_score: Optional[float] = None
    timestamp: str = field(default_factory=lambda: datetime.now().isoformat())


class CandidateCollector:
    """
    Collects all candidates and feedback during optimization for presentation.
    """
    
    def __init__(self, output_dir: str = "presentation_data"):
        """
        Initialize the collector.
        
        Args:
            output_dir: Directory to save collected data
        """
        self.output_dir = Path(output_dir)
        self.output_dir.mkdir(exist_ok=True)
        
        self.iterations: List[IterationInfo] = []
        self.current_iteration: Optional[IterationInfo] = None
        self.all_candidates: List[CandidateInfo] = []
        
        # Track seed prompt
        self.seed_prompt: Optional[str] = None
        
    def set_seed_prompt(self, seed_prompt: str):
        """Set the seed prompt for reference"""
        self.seed_prompt = seed_prompt
        
    def start_iteration(self, iteration: int):
        """Start tracking a new iteration"""
        self.current_iteration = IterationInfo(iteration=iteration)
        self.iterations.append(self.current_iteration)
        
    def add_candidate(
        self,
        iteration: int,
        candidate_id: str,
        source: str,
        prompt: str,
        score: Optional[float] = None,
        feedback: Optional[str] = None,
        feedback_details: Optional[Dict[str, Any]] = None
    ):
        """
        Add a candidate to the collection.
        
        Args:
            iteration: Iteration number
            candidate_id: Unique identifier for the candidate
            source: Source of the candidate ("GEPA_Reflection", "LLEGO_Crossover", etc.)
            prompt: The candidate prompt text
            score: Evaluation score (if available)
            feedback: Feedback text (if available)
            feedback_details: Additional feedback details (if available)
        """
        candidate = CandidateInfo(
            iteration=iteration,
            candidate_id=candidate_id,
            source=source,
            prompt=prompt,
            score=score,
            feedback=feedback,
            feedback_details=feedback_details
        )
        
        # Add to current iteration
        if self.current_iteration and self.current_iteration.iteration == iteration:
            self.current_iteration.candidates.append(candidate)
            
            # Update best candidate if this is better
            if score is not None:
                if (self.current_iteration.best_score is None or 
                    score > self.current_iteration.best_score):
                    self.current_iteration.best_candidate = candidate
                    self.current_iteration.best_score = score
        
        # Add to all candidates list
        self.all_candidates.append(candidate)
        
    def add_feedback(
        self,
        candidate_id: str,
        feedback: str,
        feedback_details: Optional[Dict[str, Any]] = None
    ):
        """
        Add feedback to an existing candidate.
        
        Args:
            candidate_id: ID of the candidate to update
            feedback: Feedback text
            feedback_details: Additional feedback details
        """
        for candidate in self.all_candidates:
            if candidate.candidate_id == candidate_id:
                candidate.feedback = feedback
                candidate.feedback_details = feedback_details
                break
        
        # Also update in iterations
        for iteration in self.iterations:
            for candidate in iteration.candidates:
                if candidate.candidate_id == candidate_id:
                    candidate.feedback = feedback
                    candidate.feedback_details = feedback_details
                    break
    
    def add_score(
        self,
        candidate_id: str,
        score: float
    ):
        """
        Add score to an existing candidate.
        
        Args:
            candidate_id: ID of the candidate to update
            score: Evaluation score
        """
        for candidate in self.all_candidates:
            if candidate.candidate_id == candidate_id:
                candidate.score = score
                break
        
        # Also update in iterations
        for iteration in self.iterations:
            for candidate in iteration.candidates:
                if candidate.candidate_id == candidate_id:
                    candidate.score = score
                    # Update best candidate if needed
                    if (iteration.best_score is None or score > iteration.best_score):
                        iteration.best_candidate = candidate
                        iteration.best_score = score
                    break
    
    def save_to_json(self, filename: Optional[str] = None) -> Path:
        """
        Save collected data to JSON file.
        
        Args:
            filename: Optional filename (auto-generated if not provided)
            
        Returns:
            Path to saved file
        """
        if filename is None:
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"candidates_and_feedback_{timestamp}.json"
        
        filepath = self.output_dir / filename
        
        data = {
            "seed_prompt": self.seed_prompt,
            "total_iterations": len(self.iterations),
            "total_candidates": len(self.all_candidates),
            "iterations": [asdict(iter_info) for iter_info in self.iterations],
            "all_candidates": [asdict(candidate) for candidate in self.all_candidates],
            "timestamp": datetime.now().isoformat()
        }
        
        with open(filepath, 'w', encoding='utf-8') as f:
            json.dump(data, f, indent=2, ensure_ascii=False)
        
        return filepath
    
    def save_to_markdown(self, filename: Optional[str] = None) -> Path:
        """
        Save collected data to Markdown file (presentation-ready format).
        
        Args:
            filename: Optional filename (auto-generated if not provided)
            
        Returns:
            Path to saved file
        """
        if filename is None:
            timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
            filename = f"candidates_and_feedback_{timestamp}.md"
        
        filepath = self.output_dir / filename
        
        with open(filepath, 'w', encoding='utf-8') as f:
            # Header
            f.write("# Optimization Candidates and Feedback\n\n")
            f.write(f"**Generated:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n")
            f.write(f"**Total Iterations:** {len(self.iterations)}\n")
            f.write(f"**Total Candidates:** {len(self.all_candidates)}\n\n")
            
            # Seed Prompt
            if self.seed_prompt:
                f.write("---\n\n")
                f.write("## 🌱 Seed Prompt\n\n")
                f.write("```\n")
                f.write(self.seed_prompt)
                f.write("\n```\n\n")
            
            # Iterations
            for iter_info in self.iterations:
                f.write("---\n\n")
                f.write(f"## πŸ”„ Iteration {iter_info.iteration}\n\n")
                
                # Best candidate for this iteration
                if iter_info.best_candidate:
                    f.write(f"### πŸ† Best Candidate (Score: {iter_info.best_score:.4f})\n\n")
                    f.write(f"**Source:** {iter_info.best_candidate.source}\n\n")
                    f.write(f"**Prompt:**\n```\n")
                    f.write(iter_info.best_candidate.prompt)
                    f.write("\n```\n\n")
                    
                    if iter_info.best_candidate.feedback:
                        f.write(f"**Feedback:**\n\n")
                        f.write(f"{iter_info.best_candidate.feedback}\n\n")
                
                # All candidates in this iteration
                f.write(f"### πŸ“ All Candidates ({len(iter_info.candidates)})\n\n")
                
                for idx, candidate in enumerate(iter_info.candidates, 1):
                    f.write(f"#### Candidate {idx}: {candidate.source}\n\n")
                    f.write(f"**ID:** `{candidate.candidate_id}`\n\n")
                    
                    if candidate.score is not None:
                        f.write(f"**Score:** `{candidate.score:.4f}`\n\n")
                    
                    f.write(f"**Prompt:**\n```\n")
                    f.write(candidate.prompt)
                    f.write("\n```\n\n")
                    
                    if candidate.feedback:
                        f.write(f"**Feedback:**\n\n")
                        f.write(f"{candidate.feedback}\n\n")
                    
                    if candidate.feedback_details:
                        f.write(f"**Feedback Details:**\n\n")
                        f.write("```json\n")
                        f.write(json.dumps(candidate.feedback_details, indent=2))
                        f.write("\n```\n\n")
                    
                    f.write("---\n\n")
            
            # Summary by source
            f.write("---\n\n")
            f.write("## πŸ“Š Summary by Source\n\n")
            
            sources = {}
            for candidate in self.all_candidates:
                if candidate.source not in sources:
                    sources[candidate.source] = []
                sources[candidate.source].append(candidate)
            
            for source, candidates in sources.items():
                f.write(f"### {source} ({len(candidates)} candidates)\n\n")
                for candidate in candidates:
                    score_str = f"Score: {candidate.score:.4f}" if candidate.score else "No score"
                    f.write(f"- **{candidate.candidate_id}** (Iteration {candidate.iteration}, {score_str})\n")
                f.write("\n")
        
        return filepath
    
    def get_summary(self) -> Dict[str, Any]:
        """Get a summary of collected data"""
        sources = {}
        for candidate in self.all_candidates:
            if candidate.source not in sources:
                sources[candidate.source] = 0
            sources[candidate.source] += 1
        
        scored_candidates = [c for c in self.all_candidates if c.score is not None]
        avg_score = sum(c.score for c in scored_candidates) / len(scored_candidates) if scored_candidates else None
        
        return {
            "total_iterations": len(self.iterations),
            "total_candidates": len(self.all_candidates),
            "candidates_by_source": sources,
            "candidates_with_scores": len(scored_candidates),
            "average_score": avg_score,
            "candidates_with_feedback": len([c for c in self.all_candidates if c.feedback])
        }