python --version
pip install openai
Learn to select, order, and format few-shot examples to maximize model accuracy.
1 import os 2 import openai 3 4 client = openai.OpenAI(api_key=os.environ["OPENAI_API_KEY"]) 5 6 # ── Fixed few-shot prompt ──────────────────────────────────────────── 7 def classify_sentiment(text: str, num_shots: int = 3) -> str: 8 all_examples = [ 9 ("The food was absolutely amazing and the service was top notch!", "positive"), 10 ("Waited an hour, food was cold, never coming back.", "negative"), 11 ("It was fine, nothing special but nothing bad either.", "neutral"), 12 ("Best coffee I've ever had! Will definitely return!", "positive"), 13 ("The product broke after one day. Terrible quality.", "negative"), 14 ] 15 16 examples = all_examples[:num_shots] 17 shots = "\n".join( 18 f"Text: {ex}\nSentiment: {label}" for ex, label in examples 19 ) 20 21 prompt = f"""Classify the sentiment as positive, negative, or neutral. 22 23 {shots} 24 25 Text: {text} 26 Sentiment:""" 27 28 response = client.chat.completions.create( 29 model="gpt-4o-mini", 30 messages=[{"role": "user", "content": prompt}], 31 max_tokens=10, 32 temperature=0, 33 ) 34 return response.choices[0].message.content.strip().lower() 35 36 # ── Dynamic few-shot with keyword similarity ───────────────────────── 37 LABELED_DATASET = [ 38 ("The movie was thrilling from start to finish.", "positive"), 39 ("Dull and predictable, I fell asleep.", "negative"), 40 ("Average movie, nothing memorable.", "neutral"), 41 ("Outstanding performances by the entire cast!", "positive"), 42 ("Waste of time and money.", "negative"), 43 ("Decent enough for a rainy afternoon.", "neutral"), 44 ("A masterpiece that will stand the test of time.", "positive"), 45 ("Poorly written script, terrible direction.", "negative"), 46 ] 47 48 def select_similar_examples(query: str, dataset: list, n: int = 3) -> list: 49 """Simple keyword-based similarity for example selection.""" 50 query_words = set(query.lower().split()) 51 52 def similarity(item): 53 text_words = set(item[0].lower().split()) 54 return len(query_words & text_words) 55 56 ranked = sorted(dataset, key=similarity, reverse=True) 57 return ranked[:n] 58 59 def dynamic_few_shot(text: str, n: int = 3) -> str: 60 examples = select_similar_examples(text, LABELED_DATASET, n) 61 shots = "\n".join(f"Text: {ex}\nSentiment: {label}" for ex, label in examples) 62 prompt = f"Classify sentiment (positive/negative/neutral):\n\n{shots}\n\nText: {text}\nSentiment:" 63 64 response = client.chat.completions.create( 65 model="gpt-4o-mini", 66 messages=[{"role": "user", "content": prompt}], 67 max_tokens=10, temperature=0, 68 ) 69 return response.choices[0].message.content.strip().lower() 70 71 # ── Ablation: count experiment ────────────────────────────────────── 72 test_inputs = [ 73 ("This film is a genuine work of art.", "positive"), 74 ("Slow pacing and no character development.", "negative"), 75 ("It's okay, I've seen better.", "neutral"), 76 ] 77 78 print("=== Shot Count Ablation ===") 79 for n in [1, 3, 5]: 80 correct = 0 81 for text, expected in test_inputs: 82 pred = classify_sentiment(text, num_shots=n) 83 if expected in pred: 84 correct += 1 85 print(f" {n}-shot accuracy: {correct}/{len(test_inputs)}") 86 87 print("\n=== Dynamic vs Fixed Few-Shot ===") 88 for text, expected in test_inputs: 89 fixed = classify_sentiment(text, num_shots=3) 90 dynamic = dynamic_few_shot(text, n=3) 91 print(f" Expected: {expected} | Fixed: {fixed} | Dynamic: {dynamic}") 92
Sign in to share your feedback and join the discussion.