Iterative Prompt Construction with LLMs

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 that 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 with 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
__ASK__
Give me a sample bulleted list with two-level nesting.

Sample Output:

Here's an example of a sample bulleted list with two-level nesting:

- Main Topic 1
  - Subtopic 1.1
  - Subtopic 1.2
    - Detail 1.2.1
    - Detail 1.2.2
- Main Topic 2
  - Subtopic 2.1
  - Subtopic 2.2
    - Detail 2.2.1
    - Detail 2.2.2
- Main Topic 3
  - Subtopic 3.1
  - 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
__ASK__
Given the bulleted list below, convert it into a JSON document

----
- Main Topic 1
  - Subtopic 1.1
  - Subtopic 1.2
    - Detail 1.2.1
    - Detail 1.2.2
- Main Topic 2
  - Subtopic 2.1
  - Subtopic 2.2
    - Detail 2.2.1
    - Detail 2.2.2
- Main Topic 3
  - Subtopic 3.1
  - Subtopic 3.2

Sample Output:

JSON
{
  "Main Topic 1": {
    "Subtopic 1.1": [],
    "Subtopic 1.2": [
      "Detail 1.2.1",
      "Detail 1.2.2"
    ]
  },
  "Main Topic 2": {
    "Subtopic 2.1": [],
    "Subtopic 2.2": [
      "Detail 2.2.1",
      "Detail 2.2.2"
    ]
  },
  "Main Topic 3": {
    "Subtopic 3.1": [],
    "Subtopic 3.2": []
  }
}
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal