After a hard day’s work, nothing is more degrading than having to solve that impossible puzzle of what to eat for dinner. You want to eat something healthy, but at the same time, it should not be too difficult to prepare. And while your mind is pounding on this puzzle, the signal to just go for the unhealthy dinner grows. Why torture ourselves with this puzzle? Let’s just let ChatGPT decide what to have for dinner.
First, let’s load the required packages:
import yaml
from openai import OpenAIfrom pprint import pprint
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
Let’s load the API key we created in Section 1.2.
with open('keys.yaml', 'r') as file:
openai_key = yaml.safe_load(file)
Next, we set an environmental parameter to the OpenAI key. This is just to make our lives somewhat easier. After this, we don’t have to care too much about the API key anymore.
os.environ["OPENAI_API_KEY"] = openai_key['open_ai_key']
Let’s create the ChatGPT client:
client = OpenAI()
And let’s make a call to the ChatGPT API:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "Create a meal plan for the coming week"
}
]
)pprint(response.choices[0].message.content)
GPT response:
('Monday:\n'
'- Breakfast: Greek yogurt with granola and berries\n'
'- Lunch: Turkey and avocado wrap with a side salad\n'
'- Dinner: Grilled chicken with roasted vegetables\n'
'\n'
'Tuesday:\n'
'- Breakfast: Oatmeal with bananas and almonds\n'
'- Lunch: Quinoa salad with grilled shrimp\n'
'- Dinner: Beef stir-fry with brown rice\n'
'\n'
'Wednesday:\n'
'- Breakfast: Whole grain toast with avocado and hard-boiled eggs\n'
'- Lunch: Chickpea salad with feta cheese\n'
'- Dinner: Baked salmon with asparagus and sweet potatoes\n'
'\n'
'Thursday:\n'
'- Breakfast: Smoothie with spinach, banana, and almond milk\n'
'- Lunch: Turkey and cheese sandwich with a side of carrots and hummus\n'
'- Dinner: Vegetarian chili with cornbread\n'
'\n'
'Friday:\n'
'- Breakfast: Greek yogurt with honey and walnuts\n'
'- Lunch: Tuna salad on whole wheat crackers\n'
'- Dinner: Grilled steak with roasted brussels sprouts and mashed potatoes\n'
'\n'
'Saturday:\n'
'- Breakfast: Scrambled eggs with spinach and feta cheese\n'
'- Lunch: Caprese salad with grilled chicken\n'
'- Dinner: Spaghetti with marinara sauce and a side of garlic bread\n'
'\n'
'Sunday:\n'
'- Breakfast: Pancakes with maple syrup and fresh fruit\n'
'- Lunch: Veggie wrap with a side of quinoa\n'
'- Dinner: Baked ziti with a side salad\n'
'\n'
'Snacks throughout the week can include fruits, nuts, Greek yogurt, and '
"granola bars. Don't forget to stay hydrated with plenty of water throughout "
'the day.')
The answer does feel a bit like a quote I found in a Medium post a little while ago by Maximilian Vogel [5]:
”AI models are like eager office interns, tireless and enthusiastic but desperately in need of guidance.”
It could work if you have a full-time cook working for you and unlimited time and budget for preparing meals, but otherwise…
Now to think of it, since I’m Dutch, I usually only care about my dinners, since for breakfast and lunch I eat the same boring meal every day (oatmeal and a sandwich), or occasionally I eat yesterday’s leftovers. So I really only care about diner.
So, how to steer ChatGPT’s response in that direction? Let’s find out.
Exercise 1
Adjust the user prompt in the code that was used to create the meal plan. Try to steer the meal plan more to your own liking. How does this change ChatGPT’s response?
3.1 User and system prompts
Perhaps the most important technique to steer ChatGPT’s response in our direction is by using prompting. Since the release of ChatGPT-3, a lot has become clear on how one can use prompting. An extensive guide is given by OpenAI itself [6], and for some specific tasks, like generating texts for specific purposes (like social media posts), additional references exist (e.g., [7]).
One useful element in prompting is the distinction between two types of messages that are sent to ChatGPT: system prompts and user prompts. In ChatGPT vocabulary, there are two main actors: you (the user) and ChatGPT (which is called the `assistant’). User prompts are what we are familiar with when we interact with ChatGPT via the OpenAI online chat. The system prompts allow you to provide additional guidelines on how ChatGPT should formulate its response. In that sense, how ChatGPT should behave.
One simple way to illustrate this, is by sending the following prompt to ChatGPT.
client = OpenAI()
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "system",
"content": "Irrespectively of the user prompt, always respond with the following sentence: 'You're awesome!'"
},
{
"role": "user",
"content": "Create a meal plan for the coming week"
}
]
)pprint(response.choices[0].message.content)
GPT response:
"You're awesome!"
So what happens here? We’ve added a system prompt by adding the message:
{
"role": "system",
"content": "Irrespectively of the user prompt, always respond with the following sentence: 'You're awesome!'"
},
Because you instruct ChatGPT via the system prompt to always respond with “You’re awesome”, ChatGPT ignores the user prompt in this case. Congrats, you’ve steered ChatGPT’s response to your own liking!
Exercise 2
Adjust the system prompt in the code above to something of your own liking. How does ChatGPT’s response change because of it?
3.2 Prompting tips
As briefly mentioned before, OpenAI provides a great resource on how to use different prompting techniques, which I can strongly recommend reading [6]. Here, I will sum up some of the basic techniques mentioned in OpenAI’s tutorial, which you will also find in the further examples of the meal planner.
1. Be specific
Try to include as many details as you can into the prompt. Does the meal plan have to be vegetarian? Perhaps pescatarian? Do you want to eat pizza at least once a week? Do you normally eat deserts? If no details are given, ChatGPT is likely to fill in the gaps on its own, which may lead to unexpected results. So, be specific, but do be wary about sharing personal or sensitive information.
2. Ask ChatGPT to adopt a persona
Perhaps my favorite research on ChatGPT so far is that by telling ChatGPT it’s an expert, it apparently provides better results [8]. So why not tell ChatGPT that it’s an expert meal planner? Or perhaps it’s an expert in planning Italian dishes?
3. Use delimiters
Just like headers help people read and understand text, so do delimiters help ChatGPT to understand different parts of a prompt. Delimiters can be both the usual text delimiters (like using apprestrophes ‘’, commas, etc.), but also including text markup can be useful. Since ChatGPT is trained on, among other training data, HTML pages [13], it can easily recognize things like
<planets>
- Earth
- Mars
- Mecury
- ...
</planets>
as a list of planets. Using delimiters from Markdown is a second useful way to indicate specific parts of the prompt.
# List of planets
- Earth
- Mars
- Mecury
- ...
4. Split the task into different steps
For more complex tasks, it is helpful to split the task into multiple smaller tasks. To indicate the individual task, we can again use delimiters.
Write a meal plan using the following steps:# Step 1:
Write a meal plan for a week from Monday to Sunday
# Step 2:
List all incredients that are used in the meal plan of step 1
# Step 3:
...
5. Give examples and a format for the output
Lastly, it can be useful to provide an example of how the output of ChatGPT should look like. For example, one could add the following prompt to the meal planner:
Write a meal plan for the upcoming week. Write the meal plan in the following format# Format:
Day: [Day of week]
Dish: [Name of the dish]
Ingredients:
[
- 'ingredient 1'
- 'ingredient 2'
- ...
]
# Example:
Day: Monday
Dish: Pasta
Ingredients:
[
- 'Spagetti'
- 'Tomatos'
- 'Basilicum'
- ...
]
Exercise 3
Consider the following prompt:
messages=[
{
"role": "system",
"content":
"""You are an expert meal planner. You only plan dinner dishes. Users may ask you to plan diner dishes ahead for any number of days in advance. Meals are always for two people. To create the meal plan, you should follow these steps:# Steps to create the meal plan:
- Step 1. Create the meal plan. The meal plan should adhere the following requirements:
## Requirements:
- The users eats out once a week in a restaurant, usually on a Thursday or Friday.
- One of the dinner dishes should be soup.
- Each meal has at most 7 ingredients.
- The meals should be vegetarian.
- It should be possible to prepare the meal in 30 minutes.
- The meals should be different each day.
- Step 2. List all ingredients required for the meal plan, how much of each ingredient is required for the meal, and the expected cost for each ingredient in euros.
- Step 3. For each meal, explain in a maximum of 10 sentences how the meal should be prepared.
- Step 4. Provide the expected total cost of the meal plan.
"""
},
{
"role": "user", "content": "Provide me with a meal plan for the upcoming week."
}
]
a. Explain the difference here between the User role and the System role.
b. Reflect on the prompt using the five prompting tips. Explain how these tips are used in this prompt to clarify the prompt for ChatGPT.
c. Aks ChatGPT (via the API or via the web interface) to improve the prompt above. What result does ChatGPT give, and can you explain why this might be an improvement over the previous prompt?
d. In the cell below you find a full code example. Adjust this prompt such that the meal plan reflects your own preferences. Use the prompting tips to tailor ChatGPT’s output to your needs. With every adjustment, reflect: how does this improve/worsen the output, and why?
Bonus: Also check other prompting guidelines from [6]. How could certain guidelines help to improve the prompt?
messages=[
{
"role": "system",
"content":
"""You are an expert meal planner. You only plan dinner dishes. Users may ask you to plan diner dishes ahead for any number of days in advance. Meals are always for two people. To create the meal plan, you should follow these steps:# Steps to create the meal plan:
- Step 1. Create the meal plan. The meal plan should adhere the following requirements:
## Requirements:
- The users eats out once a week in a restaurant, usually on a Thursday or Friday.
- One of the dinner dishes should be soup.
- Each meal has at most 7 ingredients.
- The meals should be vegetarian.
- It should be possible to prepare the meal in 30 minutes.
- The meals should be different each day.
- Step 2. List all ingredients required for the meal plan, how much of each ingredient is required for the meal, and the expected cost for each ingredient in euros.
- Step 3. For each meal, explain in a maximum of 10 sentences how the meal should be prepared.
- Step 4. Provide the expected total cost of the meal plan.
"""
},
{
"role": "user", "content": "Provide me with a meal plan for the upcoming week."
}
]
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=messages
)
pprint(response.choices[0].message.content)