Thuta Learning
ExercisesAIadvanced

Exercises: Models, Prompts & Chains Practice

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Practice Exercises: Models, Prompts & Chains Practice on your own
  • Practice the skills you've already learned to make them stick
  • Learn to find bugs, fix them, and check your own work

Let's think this through for a moment

This lesson isn't teaching anything new — it's a practice-only exercise set for putting the PromptTemplate, Models (temperature), and Chains (sequential/LCEL) concepts from the earlier Basic chapter back to work. The tasks build on each other, ramping up gradually from designing prompt variables and observing output differences, all the way to combining two chains. Working through this exercise will let you self-test your prompt engineering and chain composition skills.

Exercises

Task 1: Create a PromptTemplate containing a {topic} variable, and run the prompt "Write a short haiku about {topic}" with 2-3 different topics. Task 2: Create two ChatOpenAI models with the same prompt but temperature=0 and temperature=0.9, and compare how much the outputs differ. Task 3: Chain two chains together — the first chain produces an idea, and the second chain rewrites that idea in more detail. Task 4 (stretch): Adjust the prompt instructions so the output comes back as JSON.

Code Example

python
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate

# Task 1: Variable ပါသော prompt
haiku_prompt = PromptTemplate.from_template(
    "Write a short haiku about {topic}"
)
# TODO: llm ကို ချိတ်ပြီး topic 2-3 မျိုး run ကြည့်ပါ

# Task 2: temperature ကွာခြားမှု
llm_cold = ChatOpenAI(model="gpt-4o-mini", temperature=0)
llm_hot = ChatOpenAI(model="gpt-4o-mini", temperature=0.9)
# TODO: prompt တစ်ခုတည်းကို llm_cold နှင့် llm_hot ဖြင့် run ပြီး compare လုပ်ပါ

# Task 3: chain နှစ်ခု ဆက်စပ်ခြင်း (LCEL)
idea_prompt = PromptTemplate.from_template("Give one app idea about {topic}")
refine_prompt = PromptTemplate.from_template("Expand this idea in 3 sentences: {idea}")
llm = ChatOpenAI(model="gpt-4o-mini")

idea_chain = idea_prompt | llm
# TODO: idea_chain ရဲ့ output ကို refine_prompt ထဲ ထည့်သွင်းပြီး ဒုတိယ chain ကို ဆက်ဆောက်ပါ

# Task 4 (stretch): JSON output
# TODO: prompt instruction ထဲမှာ "Respond only in JSON with keys: title, description" ဆိုသလို ထည့်ကြည့်ပါ
You should see
For each task, you'll see the expected output differences (the haiku text, the temperature difference, the refined idea) in the terminal.

5-Minute Try-It

In 5 minutes, run Task 2 and repeat the temperature=0 output twice — notice whether the output stays the same or differs.

A Quick Warning

The higher the temperature, the more the output varies, so for tests/demos where you want reproducible results, temperature=0 is the way to go.

Easy traps

  • The variable name in the PromptTemplate (e.g. {topic}) doesn't match the dictionary key passed into .invoke(), causing a KeyError
  • Passing Chain 1's output object (AIMessage) straight into Chain 2's prompt variable instead of using it as a string, causing a type error

Now Try It Yourself

In 5 minutes, run Task 2 and repeat the temperature=0 output twice — notice whether the output stays the same or differs.

You'll know it worked when: For each task, you'll see the expected output differences (the haiku text, the temperature difference, the refined idea) in the terminal.

Exercises: Models, Prompts & Chains Practice | Thuta Learning