Advanced Prompt Techniques
Sau khi nắm vững cơ bản, đây là lúc khám phá các kỹ thuật nâng cao để tận dụng tối đa sức mạnh của LLMs.
🎯 Mục tiêu
- Master Self-consistency và Tree of Thought
- Implement Prompt Chaining cho complex tasks
- Hiểu ReAct pattern cho AI agents
- Áp dụng meta-prompting
1. Self-Consistency
Concept
Self-consistency = Chạy nhiều lần với CoT, chọn answer xuất hiện nhiều nhất.
1Run 1: "Let's think step by step..." → Answer: 422Run 2: "Let's think step by step..." → Answer: 42 3Run 3: "Let's think step by step..." → Answer: 454Run 4: "Let's think step by step..." → Answer: 425Run 5: "Let's think step by step..." → Answer: 426 7Final answer: 42 (appeared 4/5 times)Implementation
1from openai import OpenAI2from collections import Counter34client = OpenAI()56def self_consistent_answer(question, n_samples=5, temperature=0.7):7 answers = []8 9 prompt = f"""10 {question}11 12 Let's solve this step by step:13 1. First, I'll analyze the problem14 2. Then, work through the logic15 3. Finally, give the answer16 17 Answer (number only):18 """19 20 for _ in range(n_samples):21 response = client.chat.completions.create(22 model="gpt-4",23 messages=[{"role": "user", "content": prompt}],24 temperature=temperature25 )26 27 # Extract answer28 answer = response.choices[0].message.content.strip()29 answers.append(answer)30 31 # Majority voting32 counter = Counter(answers)33 most_common = counter.most_common(1)[0]34 35 return {36 "final_answer": most_common[0],37 "confidence": most_common[1] / n_samples,38 "all_answers": answers39 }4041# Usage42result = self_consistent_answer("If a train travels 120km in 2 hours, what's its speed?")43print(f"Answer: {result['final_answer']} (confidence: {result['confidence']:.0%})")Khi nào dùng
✅ Math problems, Logic puzzles, Code debugging ❌ Creative tasks, Subjective questions
2. Tree of Thought (ToT)
Concept
Tree of Thought = Explore multiple reasoning paths, evaluate và chọn best path.
1[Problem]2 / | \3 Path A Path B Path C4 / \ | / \5 A1 A2 B1 C1 C26 ✓ (best)Prompt Template
1Bạn là expert problem solver. Giải bài toán sau theo phương pháp 2Tree of Thought.3 4Problem: [problem]5 6Step 1: Generate 3 possible approaches7Step 2: Evaluate each approach (score 1-10)8Step 3: Pursue the best approach9Step 4: If stuck, backtrack and try another10 11Format:12## Approach 1: [name]13- Description: ...14- Evaluation: X/1015- Reason: ...16 17## Approach 2: [name]18...19 20## Best Approach: [chosen]21## Solution:22[detailed solution following chosen approach]Example: Business Strategy
1Problem: Công ty startup AI cần tăng revenue gấp 3 trong 12 tháng.2Resources: Team 10 người, $500K budget.3 4Generate 3 strategies, evaluate, và develop best one.Output:
1## Approach 1: B2B Enterprise Sales2- Focus: Target large enterprises with custom AI solutions3- Evaluation: 8/104- Pros: High deal value, recurring revenue5- Cons: Long sales cycle, need senior salespeople6 7## Approach 2: Product-Led Growth8- Focus: Free tier + viral features9- Evaluation: 6/1010- Pros: Scalable, low CAC11- Cons: Slow growth, may not hit 3x in 12 months12 13## Approach 3: Partnership Strategy14- Focus: Integrate with major platforms15- Evaluation: 9/1016- Pros: Fast distribution, credibility boost17- Cons: Revenue share, dependency18 19## Best Approach: Partnership Strategy20## Detailed Plan:21[expanded strategy with milestones, KPIs, action items]3. Prompt Chaining
Concept
Prompt Chaining = Chia complex task thành chain of simpler prompts.
1Task: Write a research paper2 ↓3Chain: [Outline] → [Draft Intro] → [Draft Body] → [Draft Conclusion] → [Edit] → [Format]Implementation
1def research_paper_chain(topic):2 # Step 1: Generate outline3 outline = call_llm(f"""4 Create detailed outline for research paper on: {topic}5 Include: Introduction, 3-4 main sections, Conclusion6 Format as hierarchical bullet points.7 """)8 9 # Step 2: Write introduction10 intro = call_llm(f"""11 Topic: {topic}12 Outline: {outline}13 14 Write engaging introduction (200 words).15 Include: Hook, context, thesis statement.16 """)17 18 # Step 3: Write body sections19 body_sections = []20 for section in extract_sections(outline):21 section_content = call_llm(f"""22 Topic: {topic}23 Section: {section}24 Previous content: {intro}25 26 Write this section (300 words).27 Include examples and evidence.28 """)29 body_sections.append(section_content)30 31 # Step 4: Write conclusion32 conclusion = call_llm(f"""33 Topic: {topic}34 Paper content: {intro} {' '.join(body_sections)}35 36 Write conclusion (150 words).37 Summarize key points, implications, future directions.38 """)39 40 # Step 5: Edit and polish41 full_paper = f"{intro}\n\n{'\n\n'.join(body_sections)}\n\n{conclusion}"42 43 final = call_llm(f"""44 Edit this paper for:45 - Grammar and clarity46 - Smooth transitions47 - Consistent tone48 49 Paper: {full_paper}50 """)51 52 return finalBenefits
- ✅ Better quality through focused tasks
- ✅ Easier debugging (identify which step failed)
- ✅ More control over each component
- ✅ Can optimize individual steps
4. ReAct Pattern (Reason + Act)
Concept
ReAct = LLM reasons about task, decides actions, observes results, repeats.
1Thought: I need to find the weather in Hanoi2Action: search_weather("Hanoi")3Observation: 28°C, sunny, humidity 75%4Thought: Now I have the weather data, I can respond5Action: respond("The weather in Hanoi is 28°C and sunny")Prompt Template
1You are an AI assistant that follows the ReAct pattern.2 3Available tools:4- search(query): Search the web5- calculate(expression): Do math6- lookup(term): Look up definition7 8For each step, output:9Thought: [your reasoning]10Action: [tool_name(params)] or respond(message)11Observation: [result - will be filled by system]12 13Task: {user_query}14 15Begin:Implementation Sketch
1import re23TOOLS = {4 "search": lambda q: web_search(q),5 "calculate": lambda expr: eval(expr),6 "lookup": lambda term: dictionary_lookup(term)7}89def react_agent(query, max_steps=5):10 context = f"Task: {query}\n\nBegin:\n"11 12 for step in range(max_steps):13 # Get LLM response14 response = call_llm(REACT_PROMPT + context)15 16 # Parse thought and action17 thought = extract_thought(response)18 action = extract_action(response)19 20 context += f"Thought: {thought}\nAction: {action}\n"21 22 # Check if done23 if action.startswith("respond"):24 final_message = extract_response_message(action)25 return final_message26 27 # Execute tool28 tool_name, params = parse_action(action)29 if tool_name in TOOLS:30 observation = TOOLS[tool_name](params)31 context += f"Observation: {observation}\n"32 else:33 context += f"Observation: Unknown tool\n"34 35 return "Max steps reached"5. Meta-Prompting
Concept
Meta-prompting = Dùng AI để generate/improve prompts.
Prompt Generator
1You are a prompt engineering expert.2 3Task: {user_task}4 5Generate an optimal prompt that:61. Uses appropriate technique (zero-shot, few-shot, CoT)72. Includes clear role/context83. Specifies output format94. Handles edge cases10 11Output the prompt in a code block, then explain why this structure works.Prompt Optimizer
1Current prompt: {original_prompt}2Issue: {what's wrong - e.g., "responses too vague"}3 4Suggest 3 improved versions with explanations.5For each:61. Show the improved prompt72. Explain the changes83. Predict how output will improvePrompt Debugger
1Prompt used: {prompt}2Expected output: {expected}3Actual output: {actual}4 5Analyze:61. Why did the prompt produce this output?72. What's missing or unclear?83. How to fix it?9 10Provide corrected prompt.6. Constrained Generation
Controlling Output
1Generate Python function with these CONSTRAINTS:2- Must use type hints3- Max 20 lines4- Must include docstring5- Must have error handling6- No external dependencies7- Follow PEP 88 9Function purpose: Parse CSV file and return dictionaryOutput Validation Prompt
1Generate JSON for user profile.2 3SCHEMA (must match exactly):4{5 "name": string (2-50 chars),6 "age": integer (18-120),7 "email": string (valid email format),8 "interests": array of strings (1-5 items)9}10 11After generating, validate against schema and fix any issues.12 13User info: John, 25 years old, john@email.com, likes coding and music7. Multi-Turn Strategy
Conversation Design
1conversation = [2 {3 "role": "system",4 "content": """You are a code reviewer. 5 Follow this process:6 1. First ask clarifying questions7 2. Then provide initial feedback8 3. Wait for response before detailed review9 4. Give final assessment with score"""10 },11 {12 "role": "user",13 "content": "Review my code: [code]"14 },15 # AI asks questions...16 {17 "role": "user", 18 "content": "It's for a web app, performance is priority"19 },20 # AI gives focused review...21]State Management
1class ConversationManager:2 def __init__(self, system_prompt):3 self.messages = [{"role": "system", "content": system_prompt}]4 self.state = {} # Track conversation state5 6 def add_user_message(self, content):7 self.messages.append({"role": "user", "content": content})8 9 def get_response(self):10 response = call_llm(self.messages)11 self.messages.append({"role": "assistant", "content": response})12 self.update_state(response)13 return response14 15 def update_state(self, response):16 # Track what info has been collected17 if "email" in response.lower():18 self.state["asked_email"] = True8. Hands-on Lab
Lab 1: Implement Self-Consistency
1# Complete this function2def solve_with_self_consistency(problem, n_trials=5):3 """4 Solve a math/logic problem using self-consistency.5 Return the most common answer.6 """7 # Your code here8 pass910# Test11problem = "A farmer has 17 sheep. All but 9 die. How many are left?"12answer = solve_with_self_consistency(problem)13print(f"Answer: {answer}") # Should be 9Lab 2: Build a Prompt Chain
Design a chain for: "Analyze a product review and generate response"
1Chain steps:21. [?]32. [?]43. [?]Lab 3: Create a ReAct Agent
Build an agent that can:
- Search for information
- Do calculations
- Answer user questions
📝 Quiz
-
Self-consistency hoạt động thế nào?
- Chạy 1 lần và verify
- Chạy nhiều lần, majority voting
- Dùng nhiều models khác nhau
- Cache và reuse responses
-
Tree of Thought khác CoT ở điểm nào?
- Nhanh hơn
- Explore nhiều paths, có backtracking
- Dùng ít tokens hơn
- Không cần reasoning
-
Prompt Chaining phù hợp khi nào?
- Tasks đơn giản
- Khi cần response nhanh
- Complex tasks cần nhiều steps
- Khi token limit thấp
-
ReAct pattern kết hợp gì?
- Reading và Acting
- Reasoning và Acting
- Retrieval và Acting
- Rewriting và Acting
🎯 Key Takeaways
- Self-consistency - Majority voting cải thiện accuracy
- Tree of Thought - Explore multiple paths, backtrack khi cần
- Prompt Chaining - Chia nhỏ complex tasks
- ReAct - Foundation cho AI agents
- Meta-prompting - Dùng AI để optimize prompts
🚀 Bài tiếp theo
Building Your First AI Application - Thực hành xây dựng AI chatbot với Streamlit và OpenAI API!
