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

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 allows you to create more dynamic and responsive interactions by using logical conditions to determine the outcome.

Essentially, they follow an if-then style where we structure the prompt with logical conditions, such as “if a condition is true, do X; otherwise, do Y.” This means that if the specified condition is met, the system will execute action X; if not, it will execute action Y. This approach will tailor the response of the LLM to specific criteria, making prompts more context-aware.

ChatGPT-4o example case

In this example, the desktop version is more convenient than the API. By using the file upload system of ChatGPT-4.0, you can first upload your summary. Then, you can upload a second file, such as a photo of something you want to check, in the video underneath it is a macroecnomic model. We have used the following prompt:

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.

This means we have used the following prompt structure: 

 If the model inside the picture is mentioned in the summary:

  – ChatGPT-4o will search for it in the summary and extract the relevant text sections.

– Else if the model is not mentioned in the summary:

  – ChatGPT-4o provides an explanation to include in our summary!

It must be stated that it is impressive that ChatGPT-4o can identify the model in the image itself. So, even if you are unsure of the model, ChatGPT-4o will determine it for you.

API 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:

Python
# Create the instructions
instructions = """
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). Instead of writing step 4 - ... write Hint:. 
"""

# Create the output format
output_format = """
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 = """
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 = """
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)
"""

# Create the final prompt
prompt = instructions + output_format + f"```{problem}```" + f"```{student_solution}"
response = ChatGPT_API(prompt)
print(response)

Download our template file here:

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 used f-strings and the API tools discussed in the second article. 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

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!