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}
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
