Find helpful tutorials or share inspiring use cases on AI technology for higher education.

Shared content may not reflect the policies of Tilburg University on the use of AI. 

If You Learn Conditional Prompts, Then You “Become a Prompt Engineer”

Welcome to already the fourth part of our series about Prompt Engineering. Hopefully, we haven’t overwhelmed you with the difficult yet fascinating topics in our last part, where we focused on extracting formatted output and chained several parts of a prompt together. This sounds like we are really becoming prompt engineers.

In this article, we will focus on conditionalizing our output based on our prompt, and last but not least you will create your own Mock Exam Assistant!

The Concept of Conditional Prompts

Conditional prompt learning is a method of dynamically adapting prompts to the input, rather than using static prompts that remain the same for all inputs. In some cases, it is useful to include logic or certain conditions in your prompts. This is where conditional prompts comes into play. Conditional Prompts allow you to embed logical conditions directly into your instructions to a language model. By adopting an if-then (or if-else) structure, you can make your prompts more dynamic, responsive, and context-aware.

In short, conditional prompting can be thought of as a series of if-else statements that determine how the prompt is constructed. Instead of a static prompt that is always the same, we use a flexible approach that adapts to different situations.

Basic Structure

  1. Condition: “If condition is true, perform Action X.”
  2. Alternative: “Otherwise, perform Action Y.”

When the specified condition is met, ChatGPT executes Action X; if not, it defaults to Action Y.

Static Prompt (Base Case)
Description: In a traditional setting, the prompt is static and unchanging. Think of a fixed template, such as: “Identify an object in this image.” This is similar to an else statement in its purest form, because it does not take into account variations in the input.

Conditional Prompt (If-Else)
Description: In conditional prompt learning, we introduce one or more if statements that change the prompt based on certain conditions. These conditions can be based on all kinds of factors, such as characteristics of the input data or the context of the task.

Markdown
IF (Image contains an animal):
  THEN: “Recognize the animal in this image.”
ELSE IF (Image contains a building):
  THEN: “Recognize the building in this image.”
ELSE: “Describe the object in this image.”

Nested Conditional prompts

The ‘if-else’ structure can be layered. An initial ‘if’ statement can decide on a general direction, and then there can be more specific ‘if-else’ statements deeper in the structure to further refine the prompt.

Markdown
### Ask the Student:
"Explain why government stimulus programs following an economic shock must meet the 3T's: timely, targeted, and temporary. For each, provide a possible consequence if this requirement is not met."

### Assessment and Feedback Structure<

#### 1. IF the answer is fully correct and detailed:
 - **Feedback:**
"Excellent! You have a clear and comprehensive understanding of why stimulus programs must adhere to the 3Ts. Your explanation of 'timely,' 'targeted,' and 'temporary' is precise, and the consequences you provided are relevant and well-thought-out."
 - **Follow-Up Question:**
"Can you now provide an example of a historical stimulus program (such as the U.S. New Deal or EU recovery measures) and discuss to what extent it met these 3Ts?"

#### 2. ELSE IF the answer is mostly correct but not detailed:
- **Feedback:**
"Well done! You understand the core concept of the 3Ts. Your explanation of 'timely,' 'targeted,' and 'temporary' is correct but lacks specific details."
- **Specific Follow-Up:**
"For example, can you elaborate further on 'targeted'? What does it mean for a stimulus program to be specifically targeted, and what are the consequences if it is not well-focused? Try to provide more concrete examples."

#### 3. ELSE IF the answer is partially correct:
- **Feedback:**
Your answer is partially correct. You have grasped some aspects of the 3Ts, but there are also mistakes or unclear points in your explanation."
- **Targeted Feedback and Explanation:**
"Let’s review the 3Ts and clarify them:
   - **Timely:** Stimulus must occur quickly after a shock to be effective. A delayed response can diminish or even nullify its impact.
   - **Targeted:** Stimulus must focus on the sectors or groups most affected. If too broad, resources may be wasted, and the impact could be limited.
   - **Temporary:** Stimulus must be temporary to avoid creating long-term dependency. A permanent stimulus could cause market distortions and inefficient resource use."
   - **Retry Prompt:**
   "Now try again to explain why it is important for a stimulus program to meet these 3 criteria, using the insights above."
   
 #### 4. ELSE the answer is completely incorrect:
- **Feedback:**
"The answer is not correct. That's okay! Let’s go over the basic principles of stimulus programs and the 3Ts step by step."
- **Simplified Explanation:** 
"Stimulus programs are used by governments to help the economy recover after a significant shock, such as a recession. The 3Ts are essential criteria to ensure these programs are efficient and effective:
- **Timely:** The program must be launched quickly to address immediate damage.
- **Targeted:** It should focus on the sectors or groups most in need, so resources are used efficiently.
- **Temporary:** The program must end once its goal is achieved to avoid dependency or the continued use of public funds for unnecessary initiatives."
- **Simplified Question:**
"Why do you think it is important for a stimulus program to be launched quickly ('timely')? Try to explain this in your own words."


As illustrated in the example above, the branching mechanism of conditional learning addresses the inefficiencies of static prompting. First, it provides immediate feedback, allowing students to directly identify which parts of the topic they understand and where they need improvement (similar to static prompting). Second, this is where nested conditions truly help the learning experience. They provide customized support by calibrating feedback to the specific needs of the student. By adjusting answers based on the student’s level of comprehension, conditional learning guides learners struggling with concepts to receive reinforcement, while those ready for more advanced material are challenged to deepen their expertise.

Moreover, in line with how we designed our prompt, this approach facilitates active participation. By requiring students to reformulate, expand, or refine their answers based on feedback, the process shifts their role from passive recipients to active participants in their own learning. Lastly, the use of structured ‘if-else’ logic creates a transparent framework for evaluation and progression, reducing ambiguity in feedback and making the learning process more intuitive. With some effort, this approach can also be directly aligned with how you want to provide feedback by manually specifying the frameworks that the chatbot must fulfill.

Combining Text and Files

So far, we have limited ourselves to working with text alone, but we can take this further by incorporating and conditionalizing on other types of files. For instance, you can start by uploading your summary (e.g., a text file) and then add a second file, such as a photo of something you’d like to analyze.

Example Use Case and Prompt

Here’s an example prompt to illustrate how this works:

Markdown
**Prompt:** Act as a helpful assistant, I will add an image of a specific macroeconomic model and next, I will add a current summary I am working on. Your task is to look whether this model is in the summary. **If it is**, extract the text about it and give it to me. **If not**, identify the model and give me a bachelor course summary about it.

How This Works: Prompt Structure

To implement this, we use the following logical prompt structure:

  1. If the model in the image is mentioned in the summary:
    • ChatGPT-4 will analyze the summary and extract the relevant text sections for you.
  2. Else, if the model is not mentioned in the summary:
    • ChatGPT-4 will identify the model from the image and provide a concise explanation that you can include in your summary.

Example Case: Inner Monologue

For some study applications, the reasoning process that a model uses to arrive at a final answer would be unuseful to share with the user. For example, in tutoring applications (yes, we are talking now from a teacher’s perspective), we may want to encourage students to work out their own answers, as the model’s reasoning process about the student’s solution could reveal the answer to the student.

Inner monologue is a tactic that can be used to mitigate this. The idea of inner monologue is to instruct the model to put parts of the output that are meant to be hidden from the user into a structured format that makes parsing them easy. Then, before presenting the output to the user, the output is parsed and only part of the output is made visible.

Here’s an example to illustrate how conditional prompts and inner monologue can be implemented together to achieve these results:

Markdown
**Use the following procedure to come to the final output**:
```
Step 1 - First work out your own solution to the problem delimited by three backticks. Don't rely on the student's solution, also given delimited by three backticks after the problem statement, since it may be incorrect. Don’t specify your own solution but use it for step 2. 
Step 2 - Compare your solution to the student's solution and evaluate if the student's solution is correct or not. Again don’t show your own solution.
Step 3 - If the student made a mistake, determine what hint you could give the student without giving away the answer. Enclose all your work for this step within triple quotes. 
Step 4 - If the student made a mistake, provide the hint from the previous step to the student (outside of triple quotes).
"""

DO not show the logical reasoning of the model, the steps described above, but showcase the following output:
Problem Statement: <insert here the problem > 
Student Solution: <insert the student solution here>
Feedback: <insert Hint or state that the solution is correct>


Problem: { Most economists recommend that government stimulus programs after a major economic shock are 3T: timely,
targeted, and temporary. Motivate each one of these three T’s. For each separate T, explain what might go wrong if the program is not timely/targeted/temporary. }

Student Solution:```
Timely: should start quickly after shock in order to be effective. If the program starts too late, then some firms
may have gone bankrupt, jobs may have been lost, etc.
Targeted: should be aimed at the places where it is most effective. If for certain beneficiaries the value added
of the program is not high then we can speak of deadweight loss. Less targeted programs are less efficient.
Temporary: should end on time. If the program ends too late then it will hamper economic growth as creative
destruction is distorted (capital and labor are not allocated at their most productive places)```

Models Response:

Problem Statement: Most economists recommend that government stimulus programs after a major economic shock are 3T: timely, targeted, and temporary. Motivate each one of these three T’s. For each separate T, explain what might go wrong if the program is not timely/targeted/temporary.

Student Solution:
Timely: should start quickly after shock in order to be effective. If the program starts too late, then some firms
may have gone bankrupt, jobs may have been lost, etc.
Targeted: should be aimed at the places where it is most effective. If for certain beneficiaries the value added
of the program is not high then we can speak of deadweight loss. Less targeted programs are less efficient.
Temporary: should end on time. If the program ends too late then it will hamper economic growth as creative
destruction is distorted (capital and labor are not allocated at their most productive places)

Feedback: The solution for "Timely" and "Targeted" is correct. However, for "Temporary," consider the potential long-term fiscal impacts and dependency issues that might arise if the program is not ended on time.

Fantastic! The model follows the requirements!

This conditional prompt is perfect for studying for exams. Do you have a practice exam without answers? You can get to work with it and use the model to give feedback on your answers. The only thing you need to add in the .py file is your own question and answer.

By zooming in on the different parts of the prompt, we can see that we have applied many techniques from the previous articles. From the first article, we have structured our prompt components. We of Tilburg.ai have been working in the background by iterating on prompts, resulting in specific instructions. Additionally, we have used model chaining. Finally, we specified how the output should look by using placeholders under the heading “output_format,” and we applied conditional prompts from this article. This demonstrates how all these skills can be combined into one single prompt!

Conclusion

Conditional prompts influence the way the chatbot responds, depending on the specific input from the user, rather than a fixed set of prompts or responses. This could lead to a more dynamic and personalized interaction. Here are the potential benefits, based on the principles of conditional prompt learning:

  • Improved generalization to unexpected questions/requests: Conditional prompts can help a chatbot better handle unexpected or unusual questions or requests from users. Instead of relying on predefined answers, a model with conditional prompts can generate a more relevant and personalized response
  • Contextual relevance: Conditional prompts make the chatbot better at understanding and responding to the context of the conversation. The response is not only based on the current question, but also takes into account previous interactions and the general intent of the user.
  • Increased flexibility and adaptability: Conditional prompts allow the chatbot to dynamically adapt to the specific needs of each user. The model is not limited to a fixed set of responses, but can generate unique and personalized interactions based on the input. This makes for a more natural and engaging dialogue.

The concept of conditional prompts in a chatbot is close to what is known in text processing as “contextual understanding” and “dynamic response generation”. This means that instead of fine-tuning large, pre-trained models, a relatively small adjustment to the input (the contextual prompt) can drastically affect the output of the chatbot. The chatbot will thus be able to respond in a way that is tailored to the context of the dialogue.

In short, conditional prompts for a chatbot allow for a more flexible, relevant, and personalized dialogue.

What’s Next?

Reflecting on our journey through the fourth part of the Prompt Engineering series, it’s clear we’ve made significant progress. Here’s what you’ve accomplished:

  • You now understand how to use conditional prompts.
  • You’ve employed the inner monologue technique to develop a mock exam assistant.

These skills bring you one step closer to becoming a proficient prompt engineer. Stay tuned for the next topic: shot prompting!