Home/Blog/Generative Ai/Mastering advanced prompt engineering: Part 1
advanced prompt engineering
Home/Blog/Generative Ai/Mastering advanced prompt engineering: Part 1

Mastering advanced prompt engineering: Part 1

14 min read
Dec 24, 2024
content
What is prompt engineering?
The evolution of prompt engineering
Advanced prompt engineering techniques
Chain-of-thought (CoT) prompting
CoT prompting example
Zero-shot CoT (ZS-CoT)
ZS-CoT prompting example
Automatic Chain-of-Thought (Auto-CoT)
Overview of Auto-CoT workflow
1. Question clustering
2. Demo construction
3. Sampling by selection criteria
4. Auto demos one by one
5. In-context reasoning for test questions
Self-consistency with chain-of-thought (CoT-SC)
How CoT-SC works
Example
Implementation strategies
Benefits and challenges of self-consistency
Tree-of-thoughts (ToT) prompting
Core principles of ToT
How ToT works
1. Initial thought generation
2. Thought expansion
3. Evaluation
4. Path selection
5. Iteration and depth
6. Backtracking
7. Final output generation
Example: City planning problem
Benefits and challenges of ToT
Conclusion
Next steps

In the rapidly evolving field of artificial intelligence (AI), prompt engineering has emerged as a critical skill for getting the best results from large language models (LLMs). As these models become increasingly advanced, the techniques we use to interact with them must also evolve. This blog post dives deep into advanced reasoning techniques that push the boundaries of what’s possible with prompt engineering.

In this blog, we’ll explore a wide range of techniques, starting with foundational methods like chain-of-thought (CoT) prompting and its advanced variations: Zero-shot CoT (ZS-CoT), automatic chain-of-thought (Auto-CoT), self-consistency CoT (CoT-SC), and tree of thoughts (ToT) prompting.

In the second part of the blog series, “Mastering advanced prompt engineering: Part 2,” we’ll explore cutting-edge approaches like graph-of-thoughts (GoT) and program-of-thoughts (PoT) prompting. Each method offers unique advantages and applications, making AI essential for complex problem-solving and decision-making tasks.

What is prompt engineering?#

Prompt engineering is the process of designing and refining the inputs, or prompts, given to a language model to achieve desired outputs. It involves crafting specific queries, instructions, or examples to guide the model in generating relevant and accurate responses.

Prompt engineering
Prompt engineering

While prompts can be as simple and straightforward as a brief question asked by a 5-year-old, we can enhance it with more information to get the desired response from the LLM. Here are some key aspects of prompt engineering:

  • Clarity and precision: Ensuring that prompts are clear and precise helps the model understand the task or question correctly. Ambiguous or vague prompts can lead to irrelevant or inaccurate responses.

  • Context provision: Providing sufficient context within the prompt can help the model generate more informed and contextually appropriate answers. This may include background information, specific constraints, or desired response formats.

  • Examples and templates: Using examples or templates in prompts can guide the model to produce outputs in a specific style or format. This is particularly useful for text generation, translation, or summarization tasks.

  • Iterative refinement: Prompt engineering often involves iterative refinement, where prompts are adjusted based on the model’s responses to achieve better results. This process can include changing wording, adding or removing context, or rephrasing questions.

  • Task-specific prompts: Different tasks may require different types of prompts. For example, a prompt for a chatbot might differ significantly from a prompt for a summarization task. Tailoring prompts to the specific task can improve performance.

  • Handling limitations: Understanding the language model’s limitations and designing prompts that work around them can enhance the quality of responses. This might involve avoiding ambiguous phrasing or providing explicit instructions to mitigate known issues with the model.

The evolution of prompt engineering#

Before we dive into specific techniques, it’s important to understand the context in which these advanced prompt engineering methods have developed. Traditional prompt engineering focused on crafting clear, concise instructions to elicit desired responses from AI models. However, as the complexity of tasks increased—such as requiring multi-step reasoning, deeper contextual understanding, and the ability to handle cross-domain knowledge and ambiguity—it became clear that more advanced approaches were needed to achieve higher accuracy and performance.

The following illustration represents the evolution of prompt engineering techniques from manual prompting to advanced prompting:

The evolution of prompt engineering techniques
The evolution of prompt engineering techniques

Advanced prompt engineering techniques#

Prompt engineering has evolved because large language models (LLMs) are now expected to handle more complex tasks. New techniques in prompt engineering help these models reason better and solve problems more accurately by structuring prompts in ways that encourage more advanced, multi-step reasoning processes. These techniques can guide AI models to tackle complex tasks more accurately and transparently.

Advanced prompt engineering generally fall into several categories, each with its own strengths and ideal use cases. These techniques range from linear, step-by-step approaches to more complex, interconnected reasoning frameworks. By understanding and applying these methods, prompt engineers can significantly improve the performance of AI models on a wide range of tasks, from mathematical problem-solving to complex decision-making scenarios.

We’ll explore some of the most powerful and widely used advanced prompt engineering techniques, starting with the foundational chain-of-thought prompting and its variations.

Chain-of-thought (CoT) prompting#

Chain-of-thought (CoT) promptingWei, Jason, Xuezhi Wang, Dale Schuurmans, Maarten Bosma, Fei Xia, Ed Chi, Quoc V. Le, and Denny Zhou. "Chain-of-thought prompting elicits reasoning in large language models." Advances in neural information processing systems 35 (2022): 24824-24837 guides LLMs to solve complex tasks by breaking them into step-by-step logical processes, improving accuracy, and making reasoning more transparent.

Educative Byte: The basic premise of CoT is to guide the AI through a series of logical steps, much like a teacher might lead a student through a complex math problem. This approach not only improves the accuracy of the final answer but also provides transparency into the reasoning process, making it easier to identify and correct errors.

CoT prompting example#

Let’s consider a simple mathematics problem:

Human prompt: Solve this problem step-by-step: If a train travels 120 miles in 2 hours, what is its average speed in miles per hour?

To test the prompt in a chatbot, you’ll likely get a response similar to the one below. Click the “Show Response” to see the full details.

To better understand CoT prompting, let’s look at its advantages and limitations in the table below:

Advantages of CoT Prompting

Limitations of CoT Prompting

Improves problem-solving abilities of language models.

Labor-intensive: Creating handcrafted reasoning chains is time-consuming and expensive.

Provides transparency in the reasoning process.

Scalability issues: Challenging to create CoT examples for every task or domain.

Can be applied to a wide range of tasks.

_

Zero-shot CoT (ZS-CoT)#

To address the scalability issue of CoT prompting, researchers developed ZS-CoT, which uses the prompt “let’s think step by step” to encourage step-by-step reasoning without providing explicit examples.

ZS-CoT prompting example#

Human prompt: A store has 100 apples. How many apples are left if 30% of the apples are rotten and half of the remaining apples are sold? Let’s think step by step.

To test the prompt in a chatbot, you’ll likely get a response similar to the one below. Click the “Show Response” to see the full details.

To clearly see the benefits and drawbacks of ZS-CoT, refer to the table below:

Advantages of ZS-CoT

Limitations of ZS-CoT

Eliminates the need for task-specific examples.

Sometimes produces inaccurate reasoning chains

ZS-CoT’s “let’s think step by step” prompt enables tackling a wide range of problems without prior training, such as solving novel logic puzzles or basic physics questions.

ZS-CoT may struggle with highly complex or specialized problems, like advanced quantum mechanics calculations or legal interpretations requiring deep domain expertise.

Automatic Chain-of-Thought (Auto-CoT)#

Automatic chain-of-thought (Auto-CoT)Zhang, Zhuosheng, Aston Zhang, Mu Li, and Alex Smola. "Automatic chain of thought prompting in large language models." arXiv preprint arXiv:2210.03493 (2022). is an advanced technique that addresses the limitations of both traditional CoT and ZS-CoT by automating the process of generating reasoning chains for diverse questions.

Overview of Auto-CoT workflow#

The workflow consists of several key steps:

  1. Question clustering

  2. Demo construction

  3. Sampling by selection criteria

  4. Auto demos, one by one

  5. In-context reasoning for test questions

Let’s explore each of these steps in detail.

1. Question clustering#

The process begins with a diverse set of questions. These questions are clustered based on their characteristics and complexity, which helps ensure that the final set of examples covers many problem types.

Example:

  • Cluster 1 might contain arithmetic problems

  • Cluster 2 might contain word problems involving time calculations

  • Cluster k might contain logic puzzles or complex multi-step problems

2. Demo construction#

After clustering, the system selects representative questions from each cluster. For each selected question, the large language model (LLM) is prompted to generate a reasoning chain using the “let’s think step by step” approach.

Example:

Q: While shopping for music online, Zoe bought 3 country albums and 5 pop albums. Each album came with a lyric sheet and had 3 songs. How many songs did Zoe buy in total?

A: Let’s think step by step:

  1. Zoe bought 3 country albums. Each album has 3  songs. So, they bought 3 * 3 = 9 songs from the country albums.

  2. Zoe bought 5 pop albums. Each album has 3  songs. So, they bought 5 * 3 = 15 songs from the pop albums.

  3. To find the total, we add the songs from both types of albums: 9 + 15 = 24 songs in total.

Therefore, Zoe bought 24 songs in total.

3. Sampling by selection criteria#

From the constructed demos, a subset is selected based on certain criteria. This ensures that the final set of examples is diverse and representative of different problem types and reasoning strategies.

4. Auto demos one by one#

The selected demos are then used as examples for the LLM to learn from. These automatically generated reasoning chains guide the model in approaching similar problems.

Example:

Q: A chef needs to cook 9 potatoes. They have already cooked 7. If each potato takes 3 minutes to cook, how long will it take them to cook the rest?

A: Let’s think step by step:

  1. The chef has already cooked 7 potatoes.

  2. They need to cook 9 potatoes in total.

  3. So, they still need to cook: 9 - 7 = 2 potatoes

  4. Each potato takes 3 minutes to cook.

  5. Time to cook remaining potatoes: 2 * 3 = 6 minutes

Therefore, it will take the chef 6 minutes to cook the rest of the potatoes.

5. In-context reasoning for test questions#

Finally, when presented with a new test question, the LLM uses the auto-generated demos as context to reason through the problem.

Example test question:

Q: A pet store had 64 puppies. In one day, they sold 28 of them and put the rest into cages, with 4 in each cage. How many cages did they use?

LLM Response:

Let’s think step by step:

  1. The pet store had 64 puppies initially.

  2. They sold 28 puppies in one day.

  3. To find how many puppies are left, we subtract: 64 - 28 = 36 puppies remaining

  4. These remaining puppies are put into cages with 4 in each cage.

  5. To find the number of cages, we divide the remaining puppies by 4:36 ÷ 4 = 9 cages

Therefore, they used 9 cages to house the remaining puppies

To understand the strengths and challenges of Auto-CoT, refer to the table below:

Advantages of Auto-CoT

Limitations and Considerations

Automation: Reduces the need for manual creation of reasoning chains.

Quality of generated chains: Accuracy depends on the LLM’s capabilities.

Scalability: Can generate examples for a wide variety of question types.

Clustering effectiveness: Diversity relies on good clustering.

Diversity: Ensures coverage of different problem-solving approaches.

Computational resources: Requires significant processing power for large-scale use.

Adaptability: Can be applied to a wide range of domains without extensive retraining, making it versatile for general use.

Domain specificity: While adaptable to many areas, it may require some tuning or additional prompting for highly specialized or technical domains to achieve optimal performance.

Auto-CoT prompting represents a significant advancement in automated reasoning for AI systems. By utilizing the power of LLMs to generate diverse and step-by-step reasoning chains, it provides a scalable approach to improving problem-solving capabilities across a wide range of tasks. As research in this field continues, we can expect further refinements and applications of Auto-CoT in various domains, from education to complex decision-making systems.

Self-consistency with chain-of-thought (CoT-SC)#

Self-consistency (CoT-SC)Wang, Xuezhi, Jason Wei, Dale Schuurmans, Quoc Le, Ed Chi, Sharan Narang, Aakanksha Chowdhery, and Denny Zhou. "Self-consistency improves chain of thought reasoning in language models." arXiv preprint arXiv:2203.11171 (2022). is an advanced technique that enhances CoT prompting for language models. It improves accuracy and reliability by using multiple reasoning paths for a single problem and then aggregating the results to produce a more robust answer.

How CoT-SC works#

The self-consistency method consists of three key steps:

  1. CoT prompting: The process begins with a standard CoT prompt, asking the model to explain its reasoning step-by-step.

  2. Sampling diverse reasoning paths: Instead of using a “greedy decode” approach, the method samples from the language model’s decoder to generate a diverse set of reasoning paths for the same problem.

  3. Marginalizing and aggregating: The final step involves marginalizing out the reasoning paths and aggregating the results by choosing the most consistent answer in the final answer set.

This approach replaces the single-path strategy of traditional CoT with a method that explores and utilizes multiple reasoning paths.

The self-consistency with CoT workflow (source: Xuezhi Wang, Self-Consistency Improves Chain of Thought Reasoning in Language Models)
The self-consistency with CoT workflow (source: Xuezhi Wang, Self-Consistency Improves Chain of Thought Reasoning in Language Models)

Example#

Let’s walk through a comprehensive example to illustrate the self-consistency technique:

Problem: A store is having a 30% off sale. If an item originally costs $80, what is the final price after applying a 5% coupon to the sale price?

We’ll generate three chains of thought:

Chain 1:

  1. Calculate the sale price: $80 - (30% of $80)

    1. 30% of $80 = $80 × 0.30 = $24

    2. Sale price = $80 - $24 = $56

  2. Apply the 5% coupon to the sale price: $56 - (5% of $56)

    1. 5% of $56 = $56 × 0.05 = $2.80

    2. Final price = $56 - $2.80 = $53.20

    3. Answer: $53.20

Chain 2:

  1. Calculate the total discount percentage: 30% + 5% = 35%

  2. Apply the total discount to the original price: $80 - (35% of $80)

    1. 35% of $80 = $80 × 0.35 = $28

    2. Final price = $80 - $28 = $52

    3. Answer: $52

Chain 3:

  1. Apply the 30% discount: $80 × (1 - 0.30) = $80 × 0.70 = $56

  2. Apply the 5% coupon: $56 × (1 - 0.05) = $56 × 0.95 = $53.20

  3. Answer: $53.20

Analysis and aggregation:

  • Two chains (1 and 3) arrived at $53.20

  • One chain (2) produced $52

Final answer: $53.20 (most consistent result)

Confidence: Moderate (2 out of 3 chains agree)

Implementation strategies#

To effectively implement self-consistency in your prompts, consider the following strategies:

  • CoT prompt design: Craft initial prompts that encourage step-by-step reasoning.

  • Sampling setup: Configure the language model’s decoder to sample multiple diverse reasoning paths instead of using greedy decoding.

  • Diversity encouragement: Include instructions or techniques to ensure variety in the generated reasoning paths.

  • Answer extraction: Implement a system that automatically extracts final answers from each chain.

  • Consistency analysis: Develop an algorithm to compare answers and identify the most consistent one across all generated paths.

  • Scalability: For complex problems, increase the number of sampled reasoning paths, balancing accuracy and computational cost.

Benefits and challenges of self-consistency#

Self-consistency in prompting offers several benefits over traditional CoT methods but also comes with certain challenges. Below is a table summarizing the key benefits and challenges associated with implementing self-consistency:

Benefits

Challenges and Considerations

Improved accuracy: By considering multiple approaches, we reduce the risk of being misled by a single flawed chain of reasoning.

Computational cost: Requires more resources due to the need to generate and process multiple reasoning paths.

Robustness: Handles variations in problem-solving approaches and reduces the impact of occasional errors.

Sampling configuration: The effectiveness depends on how sampling from the decoder is configured.

Confidence estimation: Consistency across different paths can indicate the model’s confidence in its answer.

Consistency metrics: Needs to determine appropriate methods for measuring consistency across diverse reasoning paths.

Diverse problem-solving: Encourages exploration of various methods, potentially leading to more efficient or insightful solutions.

Domain specificity: The optimal number of sampled paths may vary depending on the problem domain and complexity.

Error propagation: Systematic errors might persist across multiple reasoning paths.

Self-consistency represents a significant advancement in prompt engineering and language model reasoning. Utilizing multiple reasoning paths through sampling and consistency checks enhances the reliability and accuracy of language model outputs, particularly for complex problem-solving tasks. This approach effectively bridges the gap between traditional CoT prompting and more advanced, multi-path reasoning approaches, potentially leading to more reliable and accurate AI-generated solutions across various applications.

Tree-of-thoughts (ToT) prompting#

The tree-of-thoughts (ToT)Yao, Shunyu, Dian Yu, Jeffrey Zhao, Izhak Shafran, Tom Griffiths, Yuan Cao, and Karthik Narasimhan. "Tree of thoughts: Deliberate problem solving with large language models." Advances in Neural Information Processing Systems 36 (2024). technique significantly advances prompt engineering for LLMs. It builds upon and extends previous methods like input-output prompting (IO)Input-output prompting (IO) is a technique that involves providing an LLM with a specific input and expecting a corresponding output, serving as a foundational method in prompt engineering to guide the LLM towards producing desired outcomes., CoT, and CoT-SC, offering a more advanced approach to problem-solving. ToT allows language models to explore multiple reasoning paths simultaneously, providing more comprehensive and straightforward solutions.

IO vs. CoT vs. CoT-SC vs. ToT prompting
IO vs. CoT vs. CoT-SC vs. ToT prompting

Core principles of ToT#

The ToT approach is built on several key principles that differentiate it from other prompting techniques. These principles work together to create a powerful framework for complex problem-solving:

  • Branching structure: ToT creates a tree-like structure of thoughts, allowing for the exploration of multiple reasoning paths simultaneously.

  • Depth and breadth exploration: The technique enables both deep dives into specific lines of thought and broad exploration of various possibilities.

  • Dynamic evaluation: Thoughts are continuously evaluated at each level, prioritizing more promising paths.

  • Adaptive reasoning: The model can backtrack and explore alternative branches if a chosen path proves unfruitful.

  • State maintenance: Each node in the tree maintains a “state,” allowing for coherent and context-aware decision-making.

How ToT works#

The ToT technique follows a structured workflow that allows for the systematic exploration of ideas. Here’s a step-by-step breakdown of the process:

1. Initial thought generation#

The model begins by generating multiple initial thoughts or approaches to the given problem. This stage sets the foundation for the branching structure that follows.

Example: For a complex math problem, it might generate thoughts like:

  • Use algebra

  • Apply geometry

  • Consider calculus

2. Thought expansion#

After generating initial thoughts, each one is expanded into multiple sub-thoughts, creating branches. This expansion allows for a more detailed exploration of each approach.

Example: Under “Use algebra,” sub-thoughts might include:

  • Identify variables

  • Set up equations

  • Solve the system of equation

3. Evaluation#

At each level of the tree, thoughts are evaluated based on their potential to lead to a solution. This evaluation process is crucial for determining which paths are worth pursuing further.

Example: The model might determine that “Set up equations” is more promising than “Identify variables” for the current problem state.

4. Path selection#

Based on the evaluation, more promising paths are selected for further exploration. Less promising paths might be pruned or deprioritized, allowing the model to focus its resources on the most potential solutions.

5. Iteration and depth#

The process of expansion, evaluation, and selection continues, potentially going several levels deep. Each level adds more specificity and detail to the reasoning process, allowing for increasingly refined solutions.

6. Backtracking#

If a chosen path leads to a dead end or unsatisfactory result, the model can return to a previous node and explore alternative branches. This ability to backtrack ensures that the model doesn’t get stuck in unproductive lines of reasoning.

7. Final output generation#

The final output is derived from the most successful path(s) through the tree. This output often includes not just the answer, but the reasoning process that led to it, providing a comprehensive solution to the problem.

Example: City planning problem#

To illustrate the power and flexibility of the ToT technique, let’s apply it to a complex city planning problem:

Problem: Develop a comprehensive plan to reduce traffic congestion and improve air quality in a rapidly growing city of 2 million people.

The following prompt uses the ToT prompting technique for the problem mentioned above:

Let’s approach the problem of reducing traffic congestion and improving air quality in a city of 2 million people using a tree-of-thoughts method:

  1. Generate four distinct high-level approaches to address the problem.

  2. For each approach, develop 3–4 specific strategies or implementations.

  3. Choose the two most promising strategies and expand each into 4–5 detailed action items.

  4. Evaluate each action item based on potential impact, cost-effectiveness, and feasibility.

  5. If any path seems unproductive, backtrack and explore an alternative branch.

  6. Continue this process, going deeper where necessary, until you have a comprehensive plan with at least three levels of detail.

  7. Summarize the most promising overall approach, integrating the best elements from different branches of your thought process.

In the above example, we can witness how ToT can be applied to the city planning problem.

Benefits and challenges of ToT#

The ToT approach offers numerous benefits over traditional prompting techniques, making it a powerful tool for enhancing problem-solving and reasoning in complex tasks. However, implementing ToT comes with its own challenges that must be carefully managed to fully realize its potential.

Benefits

Challenges and Considerations

Enhanced problem-solving capability: ToT enables models to handle more complex problems by exploring multiple solution paths simultaneously.

Computational complexity: Generating and evaluating multiple thought branches can be computationally intensive, especially for deep trees.

Improved reasoning transparency: The tree structure clearly visualizes the model’s thought process, making it easier to understand and validate its reasoning.

Evaluation metric design: Defining effective criteria for assessing the promise of each thought path is crucial and can significantly impact the outcome.

Flexibility and adaptability: ToT’s ability to backtrack and explore alternative paths makes it adaptable to various problems and changing conditions.

Balancing exploration and exploitation: Determining how deep to explore each branch versus how many branches to consider is a key challenge.

More comprehensive solutions: By considering multiple approaches and their interactions, ToT generates more holistic and nuanced solutions.

Maintaining coherence: Ensuring different branches of thought remain coherent and relevant to the original problem as the tree deepens.

Reduced bias: Systematic exploration of multiple paths helps mitigate the impact of initial biases or assumptions.

Handling uncertainty: Incorporating mechanisms to deal with uncertain or probabilistic information in the decision-making process.

Scalability: ToT can be applied to complex problems, from simple tasks to intricate challenges.

The ToT technique represents a significant leap forward in prompt engineering, enabling language models to tackle complex problems more effectively. It allows the model to think like a human, exploring multiple options, making strategic decisions, and rethinking its choices when necessary. ToT opens up new possibilities for AI-assisted problem-solving across various domains. As ToT continues to evolve, it promises to enhance AI’s ability to provide detailed and well-reasoned solutions to even the most challenging issues.

Conclusion#

Mastering prompt engineering is essential for anyone looking to harness the full potential of advanced LLMs. This blog has explored foundational techniques such as CoT prompting and its advanced variations like ZS-CoT, Auto-CoT, and CoT-SC. We’ve also dived into advanced strategies like ToT prompting, which structures reasoning processes in a hierarchical manner, enabling models to tackle more complex problems with greater depth. As AI evolves, these advanced reasoning techniques will become increasingly crucial for driving innovation in problem-solving and decision-making across various domains.

Next steps#

Ready to dive deeper into the world of prompt engineering? Here are two exciting resources to get you started:

  1. Become a Prompt Engineer

  2. Introduction to Prompt Engineering with Llama 3



Written By:
Saif Ali
advanced prompt engineering
Join 2.5 million developers at
Explore the catalog

Free Resources