File size: 10,013 Bytes
24f95f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Prompt evolution through A/B testing and versioning.

Creates prompt variants, tests them, and promotes better versions.
"""

import json
import logging
from datetime import datetime
from pathlib import Path
from typing import Dict, Any, Optional, List
import uuid

logger = logging.getLogger(__name__)


class PromptOptimizer:
    """Manages prompt evolution and A/B testing."""
    
    def __init__(self, data_dir: str, model_fn):
        self.data_dir = Path(data_dir) / "prompt_versions"
        self.data_dir.mkdir(parents=True, exist_ok=True)
        self.model_fn = model_fn
    
    async def create_prompt_variant(self, prompt_name: str, current_prompt: str, goal: str) -> Dict[str, Any]:
        """
        Create a new prompt variant using LLM.
        
        Args:
            prompt_name: Name of the prompt (e.g., "research", "verifier")
            current_prompt: Current prompt text
            goal: Optimization goal (e.g., "improve clarity", "reduce tokens")
            
        Returns:
            Prompt variant with metadata
        """
        logger.info(f"Creating prompt variant for {prompt_name} with goal: {goal}")
        
        optimization_prompt = f"""You are a prompt engineering expert. Improve the following prompt based on this goal: {goal}

Current prompt:
{current_prompt}

Create an improved version that:
1. Maintains the same core functionality
2. {goal}
3. Is clear and actionable

Improved prompt:"""
        
        try:
            improved_prompt = await self.model_fn(optimization_prompt, max_tokens=2000)
            
            variant = {
                "id": str(uuid.uuid4()),
                "prompt_name": prompt_name,
                "version": self._get_next_version(prompt_name),
                "prompt_text": improved_prompt,
                "goal": goal,
                "status": "testing",
                "created_at": datetime.utcnow().isoformat(),
                "test_count": 0,
                "win_count": 0,
                "win_rate": 0.0,
            }
            
            self._save_variant(variant)
            logger.info(f"Created prompt variant: {variant['id']} (v{variant['version']})")
            
            return variant
        
        except Exception as e:
            logger.error(f"Failed to create prompt variant: {e}")
            raise
    
    async def test_prompt_variant(self, variant_id: str, test_input: str, expected_quality: str) -> Dict[str, Any]:
        """
        Test a prompt variant with sample input.
        
        Args:
            variant_id: Variant ID
            test_input: Test input
            expected_quality: Expected quality criteria
            
        Returns:
            Test results
        """
        variant = self._load_variant(variant_id)
        if not variant:
            raise ValueError(f"Variant not found: {variant_id}")
        
        logger.info(f"Testing prompt variant: {variant_id}")
        
        try:
            # Run the prompt with test input
            test_prompt = variant["prompt_text"].replace("{input}", test_input)
            output = await self.model_fn(test_prompt, max_tokens=1000)
            
            # Evaluate quality
            quality_score = await self._evaluate_quality(output, expected_quality)
            
            # Update variant stats
            variant["test_count"] += 1
            if quality_score >= 0.7:  # Consider it a "win" if quality >= 70%
                variant["win_count"] += 1
            variant["win_rate"] = variant["win_count"] / variant["test_count"]
            
            self._save_variant(variant)
            
            return {
                "variant_id": variant_id,
                "quality_score": quality_score,
                "output": output,
                "win_rate": variant["win_rate"],
            }
        
        except Exception as e:
            logger.error(f"Failed to test prompt variant: {e}")
            raise
    
    def compare_prompts(self, variant_id_a: str, variant_id_b: str) -> Dict[str, Any]:
        """
        Compare two prompt variants.
        
        Args:
            variant_id_a: First variant ID
            variant_id_b: Second variant ID
            
        Returns:
            Comparison results
        """
        variant_a = self._load_variant(variant_id_a)
        variant_b = self._load_variant(variant_id_b)
        
        if not variant_a or not variant_b:
            raise ValueError("One or both variants not found")
        
        return {
            "variant_a": {
                "id": variant_a["id"],
                "version": variant_a["version"],
                "win_rate": variant_a["win_rate"],
                "test_count": variant_a["test_count"],
            },
            "variant_b": {
                "id": variant_b["id"],
                "version": variant_b["version"],
                "win_rate": variant_b["win_rate"],
                "test_count": variant_b["test_count"],
            },
            "winner": variant_a["id"] if variant_a["win_rate"] > variant_b["win_rate"] else variant_b["id"],
        }
    
    def promote_prompt(self, variant_id: str, min_tests: int = 10, min_win_rate: float = 0.7) -> bool:
        """
        Promote a prompt variant to production.
        
        Args:
            variant_id: Variant ID
            min_tests: Minimum number of tests required
            min_win_rate: Minimum win rate required
            
        Returns:
            True if promoted, False otherwise
        """
        variant = self._load_variant(variant_id)
        if not variant:
            raise ValueError(f"Variant not found: {variant_id}")
        
        # Validate promotion criteria
        if variant["test_count"] < min_tests:
            logger.warning(f"Variant {variant_id} has insufficient tests: {variant['test_count']} < {min_tests}")
            return False
        
        if variant["win_rate"] < min_win_rate:
            logger.warning(f"Variant {variant_id} has insufficient win rate: {variant['win_rate']} < {min_win_rate}")
            return False
        
        # Archive current production version
        current_production = self._get_production_variant(variant["prompt_name"])
        if current_production:
            current_production["status"] = "archived"
            current_production["archived_at"] = datetime.utcnow().isoformat()
            self._save_variant(current_production)
        
        # Promote variant
        variant["status"] = "production"
        variant["promoted_at"] = datetime.utcnow().isoformat()
        self._save_variant(variant)
        
        logger.info(f"Promoted prompt variant {variant_id} to production")
        return True
    
    def archive_prompt(self, variant_id: str):
        """Archive a prompt variant."""
        variant = self._load_variant(variant_id)
        if not variant:
            raise ValueError(f"Variant not found: {variant_id}")
        
        variant["status"] = "archived"
        variant["archived_at"] = datetime.utcnow().isoformat()
        self._save_variant(variant)
        
        logger.info(f"Archived prompt variant: {variant_id}")
    
    def list_versions(self, prompt_name: str) -> List[Dict[str, Any]]:
        """List all versions of a prompt."""
        versions = []
        
        for file_path in self.data_dir.glob(f"{prompt_name}_*.json"):
            try:
                with open(file_path, 'r') as f:
                    variant = json.load(f)
                versions.append(variant)
            except Exception as e:
                logger.error(f"Failed to read variant {file_path}: {e}")
        
        # Sort by version descending
        versions.sort(key=lambda x: x.get("version", 0), reverse=True)
        return versions
    
    def _save_variant(self, variant: Dict[str, Any]):
        """Save a prompt variant to disk."""
        file_path = self.data_dir / f"{variant['prompt_name']}_{variant['id']}.json"
        with open(file_path, 'w') as f:
            json.dump(variant, f, indent=2)
    
    def _load_variant(self, variant_id: str) -> Optional[Dict[str, Any]]:
        """Load a prompt variant from disk."""
        for file_path in self.data_dir.glob(f"*_{variant_id}.json"):
            with open(file_path, 'r') as f:
                return json.load(f)
        return None
    
    def _get_production_variant(self, prompt_name: str) -> Optional[Dict[str, Any]]:
        """Get the current production variant for a prompt."""
        for file_path in self.data_dir.glob(f"{prompt_name}_*.json"):
            try:
                with open(file_path, 'r') as f:
                    variant = json.load(f)
                if variant.get("status") == "production":
                    return variant
            except Exception as e:
                logger.error(f"Failed to read variant {file_path}: {e}")
        return None
    
    def _get_next_version(self, prompt_name: str) -> int:
        """Get the next version number for a prompt."""
        versions = self.list_versions(prompt_name)
        if not versions:
            return 1
        return max(v.get("version", 0) for v in versions) + 1
    
    async def _evaluate_quality(self, output: str, expected_quality: str) -> float:
        """Evaluate output quality using LLM."""
        eval_prompt = f"""Evaluate the quality of this output based on the criteria: {expected_quality}

Output:
{output}

Rate the quality from 0.0 to 1.0, where:
- 0.0 = Completely fails criteria
- 0.5 = Partially meets criteria
- 1.0 = Fully meets criteria

Provide only the numeric score:"""
        
        try:
            score_text = await self.model_fn(eval_prompt, max_tokens=10)
            score = float(score_text.strip())
            return max(0.0, min(1.0, score))  # Clamp to [0, 1]
        except Exception as e:
            logger.error(f"Failed to evaluate quality: {e}")
            return 0.5  # Default to neutral score