python --version
pip install openai
Write effective zero-shot prompts with role assignment, explicit instructions, and output specifications.
1 import os 2 import openai 3 4 client = openai.OpenAI(api_key=os.environ["OPENAI_API_KEY"]) 5 6 # ── Zero-shot prompt anatomy ───────────────────────────────────────── 7 # 1. Role (who the model is) 8 # 2. Task (what to do) 9 # 3. Format (how to respond) 10 # 4. Constraints (what not to do) 11 12 def prompt_v1_vague(text: str) -> str: 13 """Bad: vague, no structure.""" 14 return f"Classify this: {text}" 15 16 def prompt_v2_structured(text: str) -> str: 17 """Better: role + task + format.""" 18 return f"""You are an expert sentiment analyst. 19 Task: Classify the sentiment of the customer review below. 20 Output format: A single word — exactly one of: positive, negative, neutral 21 Review: {text} 22 Sentiment:""" 23 24 def prompt_v3_detailed(text: str) -> str: 25 """Best: full structure with constraints.""" 26 return f"""You are a senior NLP specialist with expertise in customer sentiment analysis. 27 28 Task: Analyze the sentiment of the customer review below, considering: 29 - Overall emotional tone 30 - Presence of complaints vs praise 31 - Implicit vs explicit sentiment 32 33 Output requirements: 34 - Return ONLY a JSON object 35 - Use exactly this schema: {{"sentiment": "positive|negative|neutral", "confidence": 0.0-1.0, "key_phrase": "<most revealing phrase>"}} 36 37 Customer Review: {text}""" 38 39 # ── Test on sample reviews ─────────────────────────────────────────── 40 REVIEWS = [ 41 "The product quality is excellent but shipping took 3 weeks.", 42 "Complete waste of money. Broke after one day.", 43 "Delivered on time and does exactly what it says.", 44 ] 45 46 def call(prompt: str) -> str: 47 response = client.chat.completions.create( 48 model="gpt-4o-mini", 49 messages=[{"role": "user", "content": prompt}], 50 max_tokens=80, temperature=0, 51 ) 52 return response.choices[0].message.content.strip() 53 54 import json 55 56 print("=== Zero-Shot Prompt Comparison ===") 57 for review in REVIEWS[:2]: 58 print(f"\nReview: {review[:60]}...") 59 60 r1 = call(prompt_v1_vague(review)) 61 print(f" V1 (vague): {r1[:50]}") 62 63 r2 = call(prompt_v2_structured(review)) 64 print(f" V2 (structured): {r2[:50]}") 65 66 r3 = call(prompt_v3_detailed(review)) 67 try: 68 parsed = json.loads(r3) 69 print(f" V3 (detailed): {parsed['sentiment']} ({parsed['confidence']:.0%}) — '{parsed['key_phrase']}'") 70 except json.JSONDecodeError: 71 print(f" V3 (detailed): {r3[:50]}") 72 73 # ── Role impact experiment ─────────────────────────────────────────── 74 def with_role(role: str, text: str) -> str: 75 return f"{role}\n\nSummarize in one sentence: {text}" 76 77 TEXT = "Transformer models use self-attention mechanisms to process sequences in parallel, enabling massive parallelization during training and achieving state-of-the-art results across NLP tasks." 78 79 print("\n=== Role Impact ===") 80 roles = [ 81 "Answer the following:", 82 "You are a helpful assistant.", 83 "You are an expert ML researcher who specializes in explaining complex concepts clearly.", 84 ] 85 for role in roles: 86 result = call(with_role(role, TEXT)) 87 print(f" [{role[:40]}...]\n → {result[:80]}\n") 88
Sign in to share your feedback and join the discussion.