Start learning free

LESSON 18 OF 129 · AI ENGINEERING: PROMPTS THAT HOLD UP

Ask for a Format

Practice

A model likes to answer in friendly paragraphs. A person can read them, but your code can't: where does the product's name end, and which of two prices is the right one? When a program will read the answer, ask for a format: a fixed shape that is the same every time.

A good format names every line: a fixed label, a colon and a space, then what belongs there. Add "and nothing else", or the model may open with a greeting and close with an offer to help. For a book shop, the end of a prompt could read:

Answer with exactly two lines and nothing else.
Title: the book's title
Year: the year it came out

An answer in a known shape is easy to take apart. split("\n") cuts a text into its lines, and split(": ") cuts a line into its label and its value:

"Year: 1965".split(": ")    # ["Year", "1965"]

In this lesson the model recommends a gift for a tea lover in three labeled lines. Your code reads them into a dictionary and prints the fields its own way:

Name: Teapot
Price: $22
Why: A tea lover can brew their favorite tea in it every day.
Product: Teapot for $22
Reason: A tea lover can brew their favorite tea in it every day.

Your task: the start of prompt and the empty dictionary fields are ready. Send the prompt and print the answer. Add the format to the prompt: exactly three lines and nothing else, then one piece for each line (Name:, Price:, Why:). Split the answer into lines, store each value in fields under its label, and print the two lines that start with Product: and Reason:.


Assignment
Follow the Coding Tutorial to ask the model for a three-line format and read its answer into a dictionary.


Hint
\n inside a string is a line break: the format's pieces end with it, so each label starts on a new line of the prompt. In the loop, parts[0] is the label and parts[1] is the value.


STEP 1 OF 12
    Ctrl + Enter to check