রোডম্যাপ
PHASE 7 · অধ্যায় 33

প্রম্পট ইঞ্জিনিয়ারিং

Prompt Engineering

AI কে সঠিক ভাবে instructions দেওয়ার বিজ্ঞান।

ভূমিকা

একই LLM, একই question — দুজন user এর কাছে দুই রকম answer। কেন? Prompt লেখার তফাৎ। 'Write a poem' vs 'Write a 4-line haiku about autumn rain in Tagore style' — দ্বিতীয় টায় quality আকাশ-পাতাল ভালো। Prompt Engineering — AI কে effectively instruct করার বিজ্ঞান এবং art।

ধারণা

Prompt Engineering = LLM থেকে best output বের করার জন্য input design করার practice। Core techniques: Zero-shot (শুধু instruction), Few-shot (কিছু example), Chain-of-Thought (step by step think কর), Role assignment ('You are an expert...'), Constraint specification (format, length, style), Self-consistency (multiple sample, majority pick), ReAct (reason + act loop)।

সহজ ব্যাখ্যা

ভাবুন employee কে instruction দেওয়া — vague বললে ('do something') যা ইচ্ছা করবে। Specific বললে ('write a 300-word product description focused on durability, target audience 30-50 age, in friendly tone, end with CTA') — exact যা চান পাবেন। LLM ও তাই — যত clear, structured, contextual instruction, তত predictable + high-quality output।

বাস্তব ব্যবহার

  • ChatGPT custom GPT — system prompt এ অনেক engineering।
  • Cursor/Copilot — code completion prompt internally optimized।
  • Marketing copy generation — brand voice prompt template।
  • Customer support bot — persona + escalation rule প্রম্পট এ।
  • Coding agent — ReAct prompting + tool use।

ধাপে ধাপে বিশ্লেষণ

1
Step 1 — Goal clearly define
Output কেমন হবে — exact specification।
2
Step 2 — Context provide
Background information, constraints।
3
Step 3 — Example দিন (few-shot)
2-3টা input-output pair।
4
Step 4 — Format specify
JSON, bullet, table, markdown।
5
Step 5 — Iterate
Output দেখে prompt refine।

Python কোড

import os
from openai import OpenAI

client = OpenAI(
    base_url="https://ai.gateway.lovable.dev/v1",
    api_key=os.environ["LOVABLE_API_KEY"],
)

def ask(messages, label):
    res = client.chat.completions.create(
        model="google/gemini-2.5-flash",
        messages=messages,
        temperature=0.2,
    )
    print(f"\n--- {label} ---")
    print(res.choices[0].message.content)

problem = "If a shop sold 240 apples on Monday and twice as many on Tuesday, how many in total?"

ask([{"role": "user", "content": problem}], "Zero-shot")

ask([
    {"role": "system", "content": "You are a math tutor. Think step by step before giving the final answer."},
    {"role": "user", "content": problem},
], "Chain-of-Thought")

few_shot = [
    {"role": "system", "content": "Convert sentence to JSON with fields: subject, action, object."},
    {"role": "user", "content": "Sara reads the book."},
    {"role": "assistant", "content": '{"subject": "Sara", "action": "reads", "object": "the book"}'},
    {"role": "user", "content": "The cat chased the mouse."},
]
ask(few_shot, "Few-shot Structured Output")
ব্যাখ্যা

তিনটা প্রম্পটিং style demonstrate। Zero-shot — শুধু prompt। Chain-of-Thought — system message এ 'step by step' instruction যোগ। Few-shot — user/assistant pair দিয়ে pattern শেখানো, structured (JSON) output এর জন্য effective।

সাধারণ ভুল

  • Vague instruction — 'write something good' মানে কী?
  • Constraint না দেওয়া (length, format) — output unpredictable।
  • Few-shot এ inconsistent example — model confused।
  • System আর user message gulier।

অনুশীলন

  1. একটা task এ zero-shot vs few-shot accuracy compare।
  2. JSON output prompt লিখুন, parse করে validate।
  3. Chain-of-Thought দিয়ে math problem solve।
  4. Role prompt ('You are a senior lawyer...') effect দেখুন।

ছোট প্রজেক্ট

Prompt Template Library

5টা common task (summarize, translate, code review, email draft, sentiment) এর জন্য optimized prompt template তৈরি করুন। Each template এ system message + few-shot example। CLI দিয়ে template choose করে input দিয়ে output পান।

সারাংশ

  • Prompt Engineering = LLM থেকে best output বের করার design discipline।
  • Zero-shot, Few-shot, CoT, Role, Format — core techniques।
  • Clear, specific, structured prompt = predictable output।
  • Iteration ছাড়া perfect prompt হয় না।
  • Modern AI engineer এর core skill।