Prompt chaining is a technique that involves chaining multiple prompts together to generate a coherent output. This technique can be used to get a more specific answer from a language model.
For example, instead of asking a language model:
Write a short, rhyming poem about triangles and flowers
In fields of green, where blooms dance free,
Triangles sway 'neath skies of glee.
Petals soft, in hues so bright,
Catch the sun's warm, golden light.
Triangles tall, with edges sharp,
Stand like guardians, never to harp.
Their angles keen, their lines so true,
Nature's geometry, in full view.
Amongst the petals, a secret lies,
In sacred math, where beauty flies.
For triangles and flowers, hand in hand,
Compose the art of this wondrous land.
So let us marvel, in awe and delight,
At triangles and flowers, a symphony bright.
For in their union, we find the grace,
Of nature's magic, in every space.
You could chain multiple prompts together to guide the model in a more specific direction:
User: Write a one sentence story about triangles and flowers
Assistant: In the heart of a blooming garden, a triangle-shaped flower bed became the talk of the town, as each flower within it grew into a vibrant triangle, creating a geometric spectacle unlike any other.
User: Add a part about the flowers being multicolored
Assistant: In the heart of a blooming garden, a triangle-shaped flower bed became the talk of the town, as each flower within it grew into a vibrant triangle, their petals bursting with a multitude of colors, creating a geometric spectacle unlike any other.
User: Now turn it into a rhyming poem
In the heart of a garden, where the sun does shower,
Lies a bed of blooms, with an unusual power.
Shaped like a triangle, it's the town's best hour,
Each flower within, a geometric tower.
Petals of triangles, in colors so bright,
A spectacle of geometry, in the soft sunlight.
Red, blue, yellow, in every possible light,
Creating a canvas, oh what a sight!
This allows the model to generate more specific and targeted outputs by building on the previous prompts.
Prompt chaining can also be used for document question answering, where the user can ask a series of questions to extract specific information from a document or text.
For example, to extract information about a specific topic from a document, you could chain multiple prompts together:
User: Here is a sonnet. I will ask you a few questions about it later. [content from https://shakespeare.mit.edu/Poetry/sonnet.CXXX.html]
Assistant: Thank you for sharing this sonnet. It’s a beautiful piece of poetry. I’m ready to answer any questions you have about it. Please feel free to ask.
User: What does the sonnet talk about in one sentence?
Assistant: The sonnet expresses the speaker’s love for his mistress, which is unique and genuine, despite her not meeting conventional standards of beauty.
User: What is an example that the author gives?
Assistant: The author gives several examples to illustrate his mistress’s unconventional beauty. One such example is: “If hairs be wires, black wires grow on her head.” This suggests that the speaker’s mistress has coarse, black hair, unlike the fine, golden hair often idealized in love poetry. Despite this, the speaker’s love for her is genuine and rare.
User: Why does the author say that he still loves her?
The author professes his love for his mistress because he values her genuine qualities and uniqueness, rather than the false comparisons often used in traditional love poetry.
Prompt chaining works best for complex tasks where the output needs to be guided in a specific direction, such as creative writing, story generation, document question answering, and similar tasks.