-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy path19-refine-loop.vvm
More file actions
50 lines (40 loc) · 1.56 KB
/
19-refine-loop.vvm
File metadata and controls
50 lines (40 loc) · 1.56 KB
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
# VVM Example 19: Refine Loop
# Iterative improvement using the refine helper
# This demonstrates the "Evaluator-Optimizer" pattern from Anthropic
agent coder(model="sonnet", prompt="Write clean, well-tested code.")
agent reviewer(model="opus", prompt="Review code critically. Find bugs and issues.")
# Code to review and improve
initial_code = """
def calculate_discount(price, discount_percent):
return price - (price * discount_percent)
"""
# The evaluator: check if code passes review
def passes_review(code, iteration):
review = @reviewer `Review this code. List any bugs, edge cases, or improvements needed.
If the code is production-ready with no issues, respond with "APPROVED".`(code)
return ?`contains APPROVED with no significant issues listed`(review)
# The optimizer: improve based on feedback
def apply_feedback(code, iteration):
# Get detailed review
review = @reviewer `Review this code critically. Focus on:
1. Bug risks
2. Edge cases not handled
3. Missing input validation
4. Code clarity
Provide specific, actionable feedback.`(code)
# Apply the feedback
improved = @coder `Improve this code based on the review feedback.
Code: {code}
Review: {review}
Fix all issues mentioned. Add tests if appropriate.`(pack(code, review))
return improved
# refine loops until passes_review returns true or max iterations reached
# Each iteration: evaluate -> if not done -> improve -> repeat
final_code = refine(
initial_code,
max=5,
done=passes_review,
step=apply_feedback
)
# The final code should handle edge cases, have validation, etc.
export final_code