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

Practice: Prompts, Tokens, and API Calls

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

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

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

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

ဒီ lesson က အသစ်ဘာမှ သင်ပေးမှာမဟုတ်ဘဲ Basic နဲ့ Intermediate chapter တွေမှာ သင်ခဲ့တဲ့ prompt engineering, tokens/context window, နဲ့ AI API ခေါ်နည်း knowledge ကို ပြန်လှန်စမ်းသပ်ဖို့ practice set ဖြစ်ပါတယ်။ Task တစ်ခုချင်းစီက ရှေ့က lesson တွေမှာဖတ်ခဲ့တဲ့ concept ကို ကိုယ်တိုင်လက်တွေ့ကျင့်ကြည့်ဖို့ ရည်ရွယ်ထားပါတယ်။ Prompt ကောင်းတစ်ခု ဘယ်လိုရေးရမလဲ၊ token ဘယ်လောက်ကုန်နိုင်လဲ ခန့်မှန်းတတ်ဖို့၊ API request ကို basic level ကနေ ကိုယ်တိုင်ဖွဲ့ကြည့်တတ်ဖို့ဆိုတာတွေကို အာရုံစိုက်ပါတယ်။ ခက်ခဲမှု အနည်းငယ်ရှိပေမယ့် foundation level အတွက်ပဲ ဖြစ်ပါတယ်။

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

Task 1: ဆိုင်တစ်ခုအတွက် customer support chatbot persona ရေးတဲ့ prompt တစ်ခုကို vague ဖြစ်တဲ့ version ("help customers") ကနေ role, context, constraints, output format ပါတဲ့ clear prompt အဖြစ် ပြန်ရေးပါ။ Task 2: 'Myanmar ရဲ့ သမိုင်းနောက်ခံအကျဉ်းချုပ်ကို စာကြောင်း 10 ကြောင်းနဲ့ ရေးပါ' ဆိုတဲ့ prompt တစ်ခုကို ကြည့်ပြီး၊ input text ရဲ့ token count ကို roughly ခန့်မှန်းကြည့်ပါ (English စာလုံး 4 လုံးလောက်ကို 1 token လို့ မှတ်ယူပါ)၊ ပြီးရင် context window 4096 tokens ရှိတဲ့ model တစ်ခုနဲ့ conversation history 10 rounds ကို ဆက်ထည့်ရင် ဘယ်အချိန်လောက်မှာ context ပြည့်နိုင်လဲ estimate ချကြည့်ပါ။ Task 3: fetch API ကိုသုံးပြီး AI text-completion endpoint တစ်ခုကို call တဲ့ JavaScript function တစ်ခုကို API key ကို environment variable ကနေယူပြီး error handling (try/catch) ပါအောင် ရေးပါ။ Task 4 (optional bonus): အပေါ်က function ကို temperature parameter တစ်ခု ထပ်ထည့်ပြီး creative output vs. factual output အတွက် value ဘယ်လိုပြောင်းသုံးရမလဲ comment နဲ့ ရှင်းပါ။

Code နမူနာ

javascript
// Task 3 starter skeleton — fill in the missing parts
async function askAI(userPrompt) {
  const apiKey = process.env.AI_API_KEY; // TODO: never hardcode this

  try {
    const response = await fetch("https://api.example.com/v1/chat", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${apiKey}`,
      },
      body: JSON.stringify({
        model: "example-model-1",
        messages: [{ role: "user", content: userPrompt }],
        // TODO: add a temperature value here for Task 4
      }),
    });

    if (!response.ok) {
      throw new Error(`API error: ${response.status}`);
    }

    const data = await response.json();
    return data.choices[0].message.content;
  } catch (err) {
    console.error("askAI failed:", err.message);
    return null;
  }
}

// Task 1 & 2 — write your rewritten prompt and token estimate as comments here:
// const clearPrompt = "...";
// const estimatedTokens = ...;
You should see
Function ကို run လိုက်ရင် AI ဆီကနေ text response ပြန်ရ၊ error ဖြစ်ရင် console မှာ error message သာပြပြီး app crash မဖြစ်ပါဘူး။

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

Task 1 ရေးထားတဲ့ prompt ကို ChatGPT/Claude webUI မှာ 5 မိနစ်အတွင်း တကယ်ကို run ကြည့်ပြီး original vague prompt ရလဒ်နဲ့ ယှဉ်ကြည့်ပါ။

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

Token estimate က rough approximation ပဲဖြစ်ပါတယ်၊ production မှာတော့ model provider ပေးထားတဲ့ tokenizer library သုံးပြီးမှ တိကျတဲ့ count ကို ယုံကြည်သင့်ပါတယ်။

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

  • API key ကို code ထဲမှာ တိုက်ရိုက် string အဖြစ် hardcode ထည့်ပြီး Git ထဲအထိ commit ဖြစ်သွားတတ်ခြင်း
  • Prompt ရေးတုန်းက output format (JSON လား၊ list လား၊ paragraph လား) ကို ဘယ်တော့မှ ညွှန်းမပေးဘဲ ထားခဲ့ခြင်း

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

Task 1 ရေးထားတဲ့ prompt ကို ChatGPT/Claude webUI မှာ 5 မိနစ်အတွင်း တကယ်ကို run ကြည့်ပြီး original vague prompt ရလဒ်နဲ့ ယှဉ်ကြည့်ပါ။

You'll know it worked when: Function ကို run လိုက်ရင် AI ဆီကနေ text response ပြန်ရ၊ error ဖြစ်ရင် console မှာ error message သာပြပြီး app crash မဖြစ်ပါဘူး။

Practice: Prompts, Tokens, and API Calls | Thuta Learning