Advanced Prompt Engineering: Techniques That Actually Work

Chain-of-thought, tree-of-thoughts, self-consistency, and systematic evaluation methods

返回教程列表
高级16 分钟

Advanced Prompt Engineering: Techniques That Actually Work

Chain-of-thought, tree-of-thoughts, self-consistency, and systematic evaluation methods

Beyond basic prompting: master chain-of-thought, self-consistency sampling, tree-of-thoughts, constitutional AI prompting, and systematic evaluation techniques that reliably improve LLM performance.

prompt engineeringchain of thoughtfew-shot learningllmai engineering

Advanced Prompt Engineering Techniques

Why Basic Prompting Is Not Enough

For complex reasoning, code generation, and nuanced judgment, you need structured approaches that guide how the model thinks, not just what it produces.

Technique 1: Chain-of-Thought (CoT)

Force the model to show reasoning, which dramatically improves accuracy on multi-step problems.


Simple: "What is 15% of 847?"
Answer: 127.05

With CoT: "What is 15% of 847? Think step by step."

  • 10% of 847 = 84.7
  • 5% = 84.7 / 2 = 42.35
  • 15% = 84.7 + 42.35 = 127.05
  • Answer: 127.05

    CoT improves accuracy by 10-40% on math and logical reasoning tasks.

    Technique 2: Self-Consistency Sampling

    Generate 5+ CoT responses, take the majority answer. Improves accuracy significantly.

    python
    from collections import Counter

    def self_consistent_query(question: str, samples: int = 5) -> str: prompt = f"Question: {question}\nThink step by step. Final answer on last line: Answer: X" answers = [] for _ in range(samples): r = client.chat.completions.create( model='gpt-4o', messages=[{'role': 'user', 'content': prompt}], temperature=0.7 ) text = r.choices[0].message.content for line in text.split('\n'): if line.startswith('Answer:'): answers.append(line.replace('Answer:', '').strip()) return Counter(answers).most_common(1)[0][0] if answers else 'Unknown'

    Technique 3: Tree of Thoughts

    Explore 3 approaches, score each, complete the best one:

    
    Problem: {problem}

    Generate 3 different approaches. For each:

  • Describe strategy briefly
  • Execute first few steps
  • Rate promise 1-10 with reason
  • Then select the best approach and complete it.

    Technique 4: Constitutional AI Prompting

    Define principles, then have the model critique and revise its own output:

    
    You are a medical information assistant. Follow these principles:
    
  • ACCURACY: Only state scientifically established facts; flag uncertainty
  • SAFETY: Always recommend consulting a doctor for diagnosis/treatment
  • CLARITY: Use plain language for non-specialists
  • Draft an initial response, critique it against each principle, then revise.

    Technique 5: XML-Tagged Structured Output

    More reliable than asking for JSON directly:

    
    Analyze this code and provide:
    
      
        
          critical|major|minor
          What is wrong
          Suggested fix
        
      
      1-10
      approve|request_changes
    
    

    Technique 6: Specific Role Prompting

    Weak: "You are a helpful assistant."

    Strong: "You are a senior TypeScript engineer with 8 years building large-scale React applications. You follow functional programming principles, prioritize type safety, and write maintainable code. You are direct and critical - you point out problems rather than just agreeing."

    Technique 7: Negative Space Prompting

    Tell the model what NOT to do:

    
    Write a marketing email.

    Do NOT:

  • Use cliches like game-changing or revolutionary
  • Open with a question (Are you tired of...?)
  • Use passive voice
  • Make vague promises
  • Exceed 150 words
  • DO:

  • Lead with a specific problem
  • Include one concrete data point
  • End with a single clear call to action
  • Measuring Prompt Quality

    Always evaluate systematically:

  • Create 20+ representative test cases
  • Define clear success criteria
  • Score outputs consistently
  • Track improvements version by version
  • Regression test before shipping new prompts
  • 相关工具

    OpenAIClaudeLangChainDSPy