Python / Python Modern Generative AI and Agents Interview Questions
What is prompt engineering and what are the most effective techniques for getting better outputs from LLMs?
Prompt engineering is the practice of crafting inputs to LLMs to elicit more accurate, relevant, and reliable outputs without changing the model's weights. Since LLMs are sensitive to the exact phrasing, structure, and context of the prompt, small changes can dramatically affect output quality.
| Technique | Description | When to use |
|---|---|---|
| Zero-shot | Direct question with no examples | Simple tasks the model handles well |
| Few-shot | 2–5 input-output examples in the prompt before the query | Specific output format; tasks needing consistency |
| Chain-of-Thought (CoT) | Prompt with 'Let's think step by step' or examples showing reasoning | Math, logic, multi-step reasoning |
| Role prompting | System prompt: 'You are an expert Python developer' | Tonality and expertise alignment |
| Output format constraint | Instruct model to respond in JSON / a specific schema | Downstream parsing |
| Self-consistency | Sample k responses, majority-vote the answer | Reducing hallucination on factual Q&A |
from openai import OpenAI client = OpenAI() # ââ Few-shot prompting few_shot_prompt = '''Classify the sentiment of each review as POSITIVE or NEGATIVE. Review: 'This headset has amazing sound quality and fits perfectly.' Sentiment: POSITIVE Review: 'Stopped working after two days. Very disappointed.' Sentiment: NEGATIVE Review: '{user_review}' Sentiment:''' # ââ Chain-of-Thought prompting cot_prompt = ( 'A train travels 120 miles in 2 hours, then 90 miles in 1.5 hours. ' 'What is its average speed for the entire journey? ' 'Think through this step by step before giving the final answer.' ) # ââ Structured / JSON output structured_prompt = ( 'Extract the company name, role, and years of experience from this text. ' 'Return ONLY valid JSON matching this schema: ' '{"company": str, "role": str, "years": int}\n\n' 'Text: She worked at Acme Corp as a senior engineer for 5 years.' ) resp = client.chat.completions.create( model='gpt-4o', messages=[{'role': 'user', 'content': structured_prompt}], temperature=0, # deterministic for parsing tasks response_format={'type': 'json_object'}, # enforces JSON output ) import json data = json.loads(resp.choices[0].message.content) print(data) # {'company': 'Acme Corp', 'role': 'senior engineer', 'years': 5}
More Related questions...