Lesson 2
Iterative Prompt Construction: A Step-by-Step Guide
Introduction

Welcome to the subsequent step in mastering prompt engineering with Large Language Models (LLMs): Iterative Prompt Construction. Through iterative refinement, we can transform our initial, high-level questions into more precise prompts which yield exactly the outputs we need. Let's delve into how we can utilize this technique.

Understanding Iterative Prompt Construction

The idea behind iterative prompt construction is to start from a simple prompt and use LLM output to enhance your prompt instead of writing the whole prompt from scratch.

In most cases, complex prompts require formatting guidelines, comprehensive examples, detailed requirements and much more. Doing it all from scratch could be a daunting task. This is where starting simple and using your initial outputs to iteratively enhance and improve your prompt becomes handy.

The Starting Point

Let’s assume we want to build a comprehensive prompt for returning JSON documents from bulleted lists. Creating the full prompt from scratch might take some time but in collaboration with an LLM to build it iteratively we can do it much faster.

Markdown
1__ASK__ 2Give me a sample bulleted list with two-level nesting.

Sample Output:

1Here's an example of a sample bulleted list with two-level nesting: 2 3- Main Topic 1 4 - Subtopic 1.1 5 - Subtopic 1.2 6 - Detail 1.2.1 7 - Detail 1.2.2 8- Main Topic 2 9 - Subtopic 2.1 10 - Subtopic 2.2 11 - Detail 2.2.1 12 - Detail 2.2.2 13 - Specific Detail 2.2.2.1 14 - Specific Detail 2.2.2.2 15- Main Topic 3 16 - Subtopic 3.1 17 - Subtopic 3.2

In this output, we have an example we could use to improve our initial prompt.

Refinement Through Iteration

Let's go to the next step of our iteration and ask for the conversion while giving the example:

Markdown
1__ASK__ 2Given the bulleted list below, convert it into a JSON document 3 4---- 5- Main Topic 1 6 - Subtopic 1.1 7 - Subtopic 1.2 8 - Detail 1.2.1 9 - Detail 1.2.2 10- Main Topic 2 11 - Subtopic 2.1 12 - Subtopic 2.2 13 - Detail 2.2.1 14 - Detail 2.2.2 15 - Specific Detail 2.2.2.1 16 - Specific Detail 2.2.2.2 17- Main Topic 3 18 - Subtopic 3.1 19 - Subtopic 3.2

Sample Output:

1Here is the bulleted list converted into a JSON document: 2 3{ 4 "Main Topic 1": { 5 "Subtopic 1.1": [], 6 "Subtopic 1.2": [ 7 "Detail 1.2.1", 8 "Detail 1.2.2" 9 ] 10 }, 11 "Main Topic 2": { 12 "Subtopic 2.1": [], 13 "Subtopic 2.2": [ 14 { 15 "Detail 2.2.1": [] 16 }, 17 { 18 "Detail 2.2.2": [ 19 "Specific Detail 2.2.2.1", 20 "Specific Detail 2.2.2.2" 21 ] 22 } 23 ] 24 }, 25 "Main Topic 3": { 26 "Subtopic 3.1": [], 27 "Subtopic 3.2": [] 28 } 29}
Honing In On Precision

Even with a refined ask, we might not hit the mark perfectly. The model could return a long-winded reply or perhaps not format the JSON as neatly as we'd like. This is where precise constraints come into play. By explicitly stating our needs — such as requiring a response containing only the JSON and no additional commentary — we further refine the prompt. Also, in case the initial result we got wasn't exactly what we had in mind, we could make some manual tweaks to get it exactly where we need it.

Markdown
1__ASK__ 2Given a bulleted list, convert it into a JSON document 3 4__CONSTRAINTS__ 5- Your response should include ONLY the JSON representation 6- Ensure the JSON is correctly formatted according to standard conventions 7- Closely follow the format and example given below. 8 9__EXAMPLE__ 10 11Input: 12- Main Topic 1 13 - Subtopic 1.1 14 - Subtopic 1.2 15 - Detail 1.2.1 16 - Detail 1.2.2 17- Main Topic 2 18 - Subtopic 2.1 19 - Subtopic 2.2 20 - Detail 2.2.1 21 - Detail 2.2.2 22 23output: 24{ 25 "Main Topic 1": { 26 "Subtopic 1.1": [], 27 "Subtopic 1.2": [ 28 "Detail 1.2.1", 29 "Detail 1.2.2" 30 ] 31 }, 32 "Main Topic 2": { 33 "Subtopic 2.1": [], 34 "Subtopic 2.2": [ 35 "Detail 2.2.1", 36 "Detail 2.2.2" 37 ] 38 } 39}

Now that's a solid prompt that should work highly consistently. We'll see some examples of running this prompt in the practice section to observe its power.

Practice Makes Perfect

The journey from an initial, vague request to a highly specific prompt capturing the exact essence of our need lies at the heart of Iterative Prompt Construction. This method enables us to build powerful prompts much faster than doing it all manually. Let's see how well you've grasped this idea in the upcoming practices.

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.