Write the repair loop
Write ask_with_repair(model, prompt, check, max_attempts). model(messages) returns the reply text, and messages is a list of (role, text) tuples. check(text) returns None if the reply is fine, or a sentence that says what is wrong.
Start with messages = [("user", prompt)]. On each attempt call model(messages), then check. If the reply is fine return (reply, attempt), counting attempts from 1. Otherwise append ("assistant", reply) and ("user", "That reply was rejected: " + problem) and try again. After max_attempts failures return (None, max_attempts).
What Check tests
- A good first reply uses one attempt
- The second call sees the bad reply and the problem
- It gives up after max_attempts
- max_attempts of 1 never repairs
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Output
Press Run to see what your program prints.