python --version
pip install openai
Apply zero-shot CoT, few-shot CoT, and self-consistency to improve model reasoning accuracy.
1 import os 2 import re 3 from collections import Counter 4 import openai 5 6 client = openai.OpenAI(api_key=os.environ["OPENAI_API_KEY"]) 7 8 # ── Zero-shot CoT ─────────────────────────────────────────────────── 9 def direct_answer(question: str) -> str: 10 response = client.chat.completions.create( 11 model="gpt-4o-mini", 12 messages=[{"role": "user", "content": question}], 13 max_tokens=50, temperature=0, 14 ) 15 return response.choices[0].message.content.strip() 16 17 def cot_answer(question: str) -> str: 18 response = client.chat.completions.create( 19 model="gpt-4o-mini", 20 messages=[{ 21 "role": "user", 22 "content": f"{question} 23 24 Let's think step by step.", 25 }], 26 max_tokens=300, temperature=0, 27 ) 28 return response.choices[0].message.content.strip() 29 30 # ── Few-shot CoT ──────────────────────────────────────────────────── 31 FEW_SHOT_EXAMPLES = """Q: A store has 24 apples. If 3/8 are red and the rest are green, how many are green? 32 A: Let me solve step by step. 33 Red apples: 24 × (3/8) = 24 × 3 / 8 = 72 / 8 = 9 34 Green apples: 24 - 9 = 15 35 Answer: 15 green apples. 36 37 Q: If a train travels 120km in 2 hours, how far does it travel in 5 hours? 38 A: Let me solve step by step. 39 Speed: 120 / 2 = 60 km/h 40 Distance in 5 hours: 60 × 5 = 300 km 41 Answer: 300 km. 42 43 """ 44 45 def few_shot_cot(question: str) -> str: 46 prompt = FEW_SHOT_EXAMPLES + f"Q: {question} 47 A: " 48 response = client.chat.completions.create( 49 model="gpt-4o-mini", 50 messages=[{"role": "user", "content": prompt}], 51 max_tokens=300, temperature=0, 52 ) 53 return response.choices[0].message.content.strip() 54 55 # ── Self-consistency ──────────────────────────────────────────────── 56 def extract_final_number(text: str) -> str | None: 57 """Extract the final numeric answer from CoT response.""" 58 numbers = re.findall(r'd+(?:.d+)?', text) 59 return numbers[-1] if numbers else None 60 61 def self_consistent_answer(question: str, n: int = 5) -> str: 62 """Sample n CoT responses and majority-vote the answer.""" 63 responses = [] 64 for _ in range(n): 65 response = client.chat.completions.create( 66 model="gpt-4o-mini", 67 messages=[{ 68 "role": "user", 69 "content": f"{question} 70 71 Let's think step by step.", 72 }], 73 max_tokens=300, temperature=0.7, 74 ) 75 text = response.choices[0].message.content 76 answer = extract_final_number(text) 77 if answer: 78 responses.append(answer) 79 80 if not responses: 81 return "Unable to extract consistent answer" 82 83 counter = Counter(responses) 84 most_common, votes = counter.most_common(1)[0] 85 return f"{most_common} (confidence: {votes}/{n})" 86 87 # ── Comparison ───────────────────────────────────────────────────── 88 PROBLEM = "A bakery makes 48 cupcakes. They sell 3/4 of them in the morning and 1/2 of the remaining in the afternoon. How many cupcakes are left?" 89 90 print(f"Problem: {PROBLEM} 91 ") 92 print(f"Direct: {direct_answer(PROBLEM)}") 93 print(f"Zero-shot CoT:\n{cot_answer(PROBLEM)[:200]}...") 94 print(f"Few-shot CoT:\n{few_shot_cot(PROBLEM)[:200]}...") 95 print(f"Self-consistent (n=3): {self_consistent_answer(PROBLEM, n=3)}") 96
Sign in to share your feedback and join the discussion.