The complex story

So far we have used zero-shot prompting: give the AI instructions and hope for the best. Advanced techniques like few-shot examples and chain-of-thought reasoning dramatically improve output quality, especially for complex tasks.

service.py
python
# Few-shot prompting: show the AI examples of good output
prompt = """You are a bedtime storyteller.

Example story for a 4-year-old about friendship:
"Bunny and Bear lived next to each other in the forest.
One rainy day, Bunny shared her umbrella with Bear.
From that day on, they were best friends forever."

Example story for a 7-year-old about courage:
"Sam was afraid of the dark cave, but his lost puppy was inside.
He took a deep breath, turned on his flashlight, and walked in.
The puppy was just around the first corner, wagging its tail."

Now write a story for {request.character_name}, age {request.character_age},
about {request.story_theme}:"""

Few-shot examples teach the AI the exact style, length, and tone you want. Two or three examples are usually enough to establish a pattern.

Chain-of-thought prompting asks the AI to reason through its approach before generating the final output. For our story generator, this means asking the AI to first plan the story arc, then write. This produces more coherent narratives with better structure.

Prompt chaining takes that idea one step further: instead of one call that plans and writes, you make two focused calls. The first call produces a story plan, and the second call receives that plan as input and writes the story. Each prompt does one job well, and you can inspect or fix the plan between steps.

service.py
python
# Prompt chaining: two focused calls instead of one overloaded prompt
# Step 1: Plan the story arc
plan_prompt = f"""Outline a three-beat story arc for a
{request.character_age}-year-old about {request.story_theme}.
One short line per beat."""
story_plan = await llm_provider.generate(plan_prompt, temperature=0.4)

# Step 2: Write the story from the plan
write_prompt = f"""You are a gentle bedtime storyteller.
Write a story for {request.character_name} that follows this arc:
{story_plan}"""
story = await llm_provider.generate(write_prompt, temperature=0.8)

The output of the planning call becomes the input of the writing call. Note the temperatures: low for the structured plan, higher for the creative writing. Try extending the project with this pattern.

Quiz: Quiz

Loading practice…

Checkpoint: Prompt engineering checkpoint

Loading practice…