Thuta Learning
ရှာဖွေရန်
ExercisesAIadvanced

Exercises: Models, Prompts & Chains Practice

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Exercises: Models, Prompts & Chains Practice ကို ကိုယ်တိုင် လေ့ကျင့်ကြည့်မယ်
  • သင်ခဲ့ပြီးသား skill တွေကို practice လုပ်ပြီး ခိုင်မာအောင်လုပ်မယ်
  • အမှားရှာ၊ ပြင်တတ်၊ ကိုယ်တိုင် check လုပ်တတ်မယ်

ခဏလေး ဒီလိုပဲ စဉ်းစားကြည့်

ဒီ lesson ဟာ သင်ခန်းစာအသစ် သင်ကြားခြင်း မဟုတ်ဘဲ, အရင်က Basic chapter မှာ လေ့လာခဲ့တဲ့ PromptTemplate, Models (temperature), နှင့် Chains (sequential/LCEL) များကို လက်တွေ့ ပြန်လည် အသုံးချကြည့်ရန် practice-only exercise set ဖြစ်ပါတယ်။ Task တွေဟာ တစ်ခုနှင့်တစ်ခု ဆက်စပ်နေပြီး, prompt variable design ကနေ output ကွာခြားမှု observe ခြင်း, နောက်ဆုံး chain နှစ်ခုကို ပေါင်းစပ်ခြင်းအထိ တဖြည်းဖြည်း အဆင့်မြင့်သွားပါတယ်။ ဒီ exercise ကို ပြီးအောင် လုပ်ဆောင်ခြင်းဖြင့် သင်ရဲ့ prompt engineering နှင့် chain composition စွမ်းရည်ကို self-test လုပ်နိုင်ပါလိမ့်မယ်။

လေ့ကျင့်ခန်းများ

Task 1: {topic} variable တစ်ခုပါဝင်သော PromptTemplate တစ်ခုကို ဖန်တီးပြီး "Write a short haiku about {topic}" ဆိုသည့် prompt ကို topic 2-3 မျိုးဖြင့် run ကြည့်ပါ။ Task 2: တူညီသော prompt တစ်ခုတည်းကို temperature=0 နှင့် temperature=0.9 ဖြင့် ChatOpenAI model နှစ်ခု ဖန်တီးပြီး output ဘယ်လောက်ကွာခြားလဲ compare လုပ်ကြည့်ပါ။ Task 3: chain နှစ်ခုကို ချိတ်ဆက်ပါ - ပထမ chain က idea တစ်ခုထုတ်ပေးပြီး, ဒုတိယ chain က အဲဒီ idea ကို ပိုပြီး detail ကျအောင် ပြန်ရေးပေးရမည်။ Task 4 (stretch): output ကို JSON format အဖြစ် ရအောင် prompt instruction ကို ပြင်ဆင်ကြည့်ပါ။

Code နမူနာ

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
Task တစ်ခုစီအတွက် expected output ကွဲပြားမှု (haiku စာသား, temperature ကွာခြားချက်, refine လုပ်ထားသော idea) ကို terminal ပေါ်တွင် တွေ့ရမည်။

၅ မိနစ် စမ်းကြည့်

5 မိနစ်အတွင်း Task 2 ကို run ပြီး temperature=0 output ကို run 2 ကြိမ် repeat လုပ်ကြည့်ပါ - output တူညီနေလား သို့မဟုတ် ကွဲနေလား သတိပြုမိပါစေ။

သတိလေးတစ်ချက်

Temperature တန်ဖိုးမြင့်လေ output ကွဲပြားမှု များလေဖြစ်သောကြောင့် reproducible result လိုချင်တဲ့ test/demo များအတွက် temperature=0 ကို preference ပေးသင့်သည်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • PromptTemplate ထဲက variable name (ဥပမာ {topic}) နှင့် .invoke() ထဲ ထည့်သွင်းတဲ့ dictionary key မတူညီဘဲ KeyError တက်တတ်သည်
  • Chain 1 ရဲ့ output object (AIMessage) ကို Chain 2 ရဲ့ prompt variable ထဲ တိုက်ရိုက် string အဖြစ် မသုံးဘဲ ထည့်သွင်းမိသဖြင့် type error ဖြစ်တတ်သည်

အခု ကိုယ်တိုင် စမ်းကြည့်

5 မိနစ်အတွင်း Task 2 ကို run ပြီး temperature=0 output ကို run 2 ကြိမ် repeat လုပ်ကြည့်ပါ - output တူညီနေလား သို့မဟုတ် ကွဲနေလား သတိပြုမိပါစေ။

You'll know it worked when: Task တစ်ခုစီအတွက် expected output ကွဲပြားမှု (haiku စာသား, temperature ကွာခြားချက်, refine လုပ်ထားသော idea) ကို terminal ပေါ်တွင် တွေ့ရမည်။

Exercises: Models, Prompts & Chains Practice | Thuta Learning