Start learning free

LESSON 29 OF 129 · AI ENGINEERING: STRUCTURED OUTPUT

Retry with the Error

Practice

Your program can now tell a good answer from a bad one. What should it do with a bad one? Ask again, but not the same way: the same request gets the same answer. The model can fix its answer when you tell it what was wrong.

The model remembers nothing between requests. So a retry sends the whole conversation: the first message, the model's own answer with the role "assistant", and then your correction with the role "user".

messages = [
    {"role": "user", "content": "Name a fruit."},
    {"role": "assistant", "content": "Carrot"},
    {"role": "user", "content": "A carrot is a vegetable. Name a fruit."},
]

Your correction comes straight from find_problems: glue the list of problems into one text and put an instruction in front of it. And don't ask forever: every try costs time and tokens. classify makes at most 3 tries, the first one and 2 retries, then returns None so a person can take over.

With the course model, the finished program prints this. The last three lines come from the check: how many requests were sent, and what the retry for the late order held.

negative 1 | My teapot arrived with a cracked lid. I'm very disappointed.
Try 1 has problems: ['sentiment must be positive, neutral or negative', 'priority must be a whole number']
negative 1 | Where is my order? It has been two weeks!
Try 1 has problems: ['priority must be from 1 to 3']
Try 2 has problems: ['priority must be a whole number']
Try 3 has problems: ['priority must be from 1 to 3']
Gave up: Do you sell gift cards?
Model calls: 6
The retry for the late order sent these roles: user, assistant, user
Its last message: Fix these and answer with JSON only: sentiment must be positive, neutral or negative; priority must be a whole number

Your task: ask, find_problems and the loop over the inbox are already written, and classify ends with return None. Fill in classify: up to 3 tries, return the answer as soon as it has no problems, and after a bad try send the model its own answer back together with the problems.


Assignment
Follow the Coding Tutorial to write the retry loop in classify: ask, parse, check, and on a bad answer add the answer and your feedback to the conversation before the next try.


Hint
Everything happens inside one for loop over range(3). A good answer leaves the function with return data, so the lines below the if run only for a bad one. The model's answer goes back first, as an "assistant" message, then your feedback as a "user" message.


STEP 1 OF 10
    Ctrl + Enter to check