from flush import FewShotPromptTemplate

Let’s first look at a very simple example of how a FewShotPromptTemplate can be used to format a prompt:

Creating Examples

To get started, we can create a list of few-shot examples. Each example is a tuple that contains the input first and the formatted output next.

examples = [
    ('''Write a compressed perfect image description with adjectives and nouns 
    of a Female cyborg walking in a winter landscape in Norway:''', 
    '''Gorgeous female cyborg, shimmering, sci-fi, armor, strides, snow, tree, 
    banks, frosted, ice, gleaming, metal, blue, optics, robotic, movements, still, 
    pristine, beauty, Nordic, vista'''),

    ('''Write a compressed perfect image description with adjectives and nouns 
    of a Female cyborg walking in a winter landscape in Norway:''', 
    '''Beautiful female cyborg, powerful sci-fi armor, snow, trees, banks, 
    frosted, ice, gleaming, metal, white, high tech, robotic, still, pristine, 
    majestic, Nordic, epic'''),

    ('''Write a compressed perfect image description with adjectives and nouns 
    of a Female cyborg walking in a winter landscape in Norway:''', 
    '''Magnificent female cyborg, dazzling futuristic armor, snow-covered, trees, 
    banks, iced, chilly, shining, metallic, electric-blue, lenses, precise, 
    mechanical, motion, serene, picturesque, glorious, Nordic''')
]

Defining the Main Template

Now, we define the input that we will pass along with the few shot examples. We use Python’s f-string as the template format for prompt templates.

main_template = '''Write a compressed perfect image description with 
                   adjectives and nouns of a Female cyborg walking in a 
                   {season} landscape in Norway:?'''

This can then be passed into model input.

Feeding the Examples and Main Template to FewShotPromptTemplate

Finally, create a FewShotPromptTemplate object. This boject takes in the few-shot examples, as well as the main template.

introduction_text = "You are to construct prompts for AI image generation models."

prompt_template = FewShotPromptTemplate(
    examples=examples,
    main_template=main_template,
    introduction=introduction_text
)

Note that we also pass in introduction, an optional parameter simply used to clarify the central theme of the examples if needed.

Formatting the Template

Now, we use the format method to create a prompt with new content as shown below.

formatted_prompt = prompt_template.format(season="summer")
print(formatted_prompt)

This will give us the following output:

You are to construct prompts for AI image generation models.

Write a compressed perfect image description with adjectives and nouns 
of a Female cyborg walking in a winter landscape in Norway:
Gorgeous female cyborg, shimmering, sci-fi, armor, strides, snow, tree, 
banks, frosted, ice, gleaming, metal, blue, optics, robotic, movements, still, 
pristine, beauty, Nordic, vista

Write a compressed perfect image description with adjectives and nouns 
of a Female cyborg walking in a winter landscape in Norway:
Beautiful female cyborg, powerful sci-fi armor, snow, trees, banks, 
frosted, ice, gleaming, metal, white, high tech, robotic, still, pristine, 
majestic, Nordic, epic

Write a compressed perfect image description with adjectives and nouns 
of a Female cyborg walking in a winter landscape in Norway:
Magnificent female cyborg, dazzling futuristic armor, snow-covered, trees, 
banks, iced, chilly, shining, metallic, electric-blue, lenses, precise, 
mechanical, motion, serene, picturesque, glorious, Nordic

Write a compressed perfect image description with adjectives and nouns 
of a Female cyborg walking in a summer landscape in Norway: