Command Palette

Search for a command to run...

Hectal
Phase 4: Structured Output
LLM Engineering · Exercise 6 of 6 Previous
Advanced

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

  1. A good first reply uses one attempt
  2. The second call sees the bad reply and the problem
  3. It gives up after max_attempts
  4. 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.

main.py
Sign in to run your code.

Output

Press Run to see what your program prints.