Create Your Personal Meal Planner Utilizing ChatGPT | by Corné de Ruijt | Sep, 2024

After a tough day’s work, nothing is extra degrading than having to resolve that not possible puzzle of what to eat for dinner. You need to eat one thing wholesome, however on the identical time, it shouldn’t be too tough to arrange. And whereas your thoughts is pounding on this puzzle, the sign to simply go for the unhealthy dinner grows. Why torture ourselves with this puzzle? Let’s simply let ChatGPT resolve what to have for dinner.

First, let’s load the required packages:

import yaml
from openai import OpenAI

from 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 Part 1.2.

with open('keys.yaml', 'r') as file:
openai_key = yaml.safe_load(file)

Subsequent, we set an environmental parameter to the OpenAI key. That is simply to make our lives considerably simpler. After this, we don’t need to care an excessive amount of in regards to the API key anymore.

os.environ["OPENAI_API_KEY"] = openai_key['open_ai_key']

Let’s create the ChatGPT consumer:

consumer = OpenAI()

And let’s make a name to the ChatGPT API:

response = consumer.chat.completions.create(
mannequin="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "Create a meal plan for the coming week"
}
]
)

pprint(response.selections[0].message.content material)

GPT response:

('Monday:n'
'- Breakfast: Greek yogurt with granola and berriesn'
'- Lunch: Turkey and avocado wrap with a facet saladn'
'- Dinner: Grilled hen with roasted vegetablesn'
'n'
'Tuesday:n'
'- Breakfast: Oatmeal with bananas and almondsn'
'- Lunch: Quinoa salad with grilled shrimpn'
'- Dinner: Beef stir-fry with brown ricen'
'n'
'Wednesday:n'
'- Breakfast: Complete grain toast with avocado and hard-boiled eggsn'
'- Lunch: Chickpea salad with feta cheesen'
'- Dinner: Baked salmon with asparagus and candy potatoesn'
'n'
'Thursday:n'
'- Breakfast: Smoothie with spinach, banana, and almond milkn'
'- Lunch: Turkey and cheese sandwich with a facet of carrots and hummusn'
'- Dinner: Vegetarian chili with cornbreadn'
'n'
'Friday:n'
'- Breakfast: Greek yogurt with honey and walnutsn'
'- Lunch: Tuna salad on complete wheat crackersn'
'- Dinner: Grilled steak with roasted brussels sprouts and mashed potatoesn'
'n'
'Saturday:n'
'- Breakfast: Scrambled eggs with spinach and feta cheesen'
'- Lunch: Caprese salad with grilled chickenn'
'- Dinner: Spaghetti with marinara sauce and a facet of garlic breadn'
'n'
'Sunday:n'
'- Breakfast: Pancakes with maple syrup and contemporary fruitn'
'- Lunch: Veggie wrap with a facet of quinoan'
'- Dinner: Baked ziti with a facet saladn'
'n'
'Snacks all through the week can embrace fruits, nuts, Greek yogurt, and '
"granola bars. Remember to remain hydrated with loads of water all through "
'the day.')

The reply does really feel a bit like a quote I discovered in a Medium submit a short time in the past by Maximilian Vogel [5]:

”AI fashions are like keen workplace interns, tireless and enthusiastic however desperately in want of steerage.”

It may work in case you have a full-time cook dinner working for you and limitless time and finances for getting ready meals, however in any other case…

Now to think about it, since I’m Dutch, I often solely care about my dinners, since for breakfast and lunch I eat the identical boring meal daily (oatmeal and a sandwich), or sometimes I eat yesterday’s leftovers. So I actually solely care about diner.

So, how you can steer ChatGPT’s response in that path? Let’s discover out.

Train 1
Regulate the person immediate within the code that was used to create the meal plan. Attempt to steer the meal plan extra to your personal liking. How does this transformation ChatGPT’s response?

3.1 Consumer and system prompts

Maybe crucial method to steer ChatGPT’s response in our path is by utilizing prompting. Because the launch of ChatGPT-3, rather a lot has turn out to be clear on how one can use prompting. An intensive information is given by OpenAI itself [6], and for some particular duties, like producing texts for particular functions (like social media posts), further references exist (e.g., [7]).

One helpful factor in prompting is the excellence between two sorts of messages which are despatched to ChatGPT: system prompts and person prompts. In ChatGPT vocabulary, there are two essential actors: you (the person) and ChatGPT (which known as the `assistant’). Consumer prompts are what we’re accustomed to after we work together with ChatGPT through the OpenAI on-line chat. The system prompts permit you to present further tips on how ChatGPT ought to formulate its response. In that sense, how ChatGPT ought to behave.

Fig. 2. System vs assistant vs person prompts

One easy approach to illustrate this, is by sending the next immediate to ChatGPT.

consumer = OpenAI()
response = consumer.chat.completions.create(
mannequin="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.selections[0].message.content material)

GPT response:

"You are superior!"

So what occurs right here? We’ve added a system immediate by including the message:

{
"position": "system",
"content material": "Irrespectively of the person immediate, at all times reply with the next sentence: 'You are superior!'"
},

Since you instruct ChatGPT through the system immediate to at all times reply with “You’re superior”, ChatGPT ignores the person immediate on this case. Congrats, you’ve steered ChatGPT’s response to your personal liking!

Train 2
Regulate the system immediate within the code above to one thing of your personal liking. How does ChatGPT’s response change due to it?

3.2 Prompting ideas

As briefly talked about earlier than, OpenAI supplies an excellent useful resource on how you can use totally different prompting strategies, which I can strongly suggest studying [6]. Right here, I’ll sum up a few of the fundamental strategies talked about in OpenAI’s tutorial, which additionally, you will discover within the additional examples of the meal planner.

1. Be particular

Attempt to embrace as many particulars as you may into the immediate. Does the meal plan need to be vegetarian? Maybe pescatarian? Do you need to eat pizza a minimum of as soon as per week? Do you usually eat deserts? If no particulars are given, ChatGPT is prone to fill within the gaps by itself, which can result in surprising outcomes. So, be particular, however do be cautious about sharing private or delicate data.

2. Ask ChatGPT to undertake a persona

Maybe my favourite analysis on ChatGPT to date is that by telling ChatGPT it’s an knowledgeable, it apparently supplies higher outcomes [8]. So why not inform ChatGPT that it’s an knowledgeable meal planner? Or maybe it’s an knowledgeable in planning Italian dishes?

3. Use delimiters

Similar to headers assist individuals learn and perceive textual content, so do delimiters assist ChatGPT to know totally different elements of a immediate. Delimiters might be each the same old textual content delimiters (like utilizing apprestrophes ‘’, commas, and so on.), but in addition together with textual content markup might be helpful. Since ChatGPT is skilled on, amongst different coaching knowledge, HTML pages [13], it might probably simply acknowledge issues like

<planets>
- Earth
- Mars
- Mecury
- ...
</planets>

as an inventory of planets. Utilizing delimiters from Markdown is a second helpful approach to point out particular elements of the immediate.

# Record of planets
- Earth
- Mars
- Mecury
- ...

4. Cut up the duty into totally different steps

For extra advanced duties, it’s useful to separate the duty into a number of smaller duties. To point the person activity, we will once more use delimiters.

Write a meal plan utilizing the next steps:

# Step 1:
Write a meal plan for per week from Monday to Sunday

# Step 2:
Record all incredients which are used within the meal plan of step 1

# Step 3:
...

5. Give examples and a format for the output

Lastly, it may be helpful to offer an instance of how the output of ChatGPT ought to appear to be. For instance, one may add the next immediate to the meal planner:

Write a meal plan for the upcoming week. Write the meal plan within the following format

# Format:
Day: [Day of week]
Dish: [Name of the dish]
Elements:
[
- 'ingredient 1'
- 'ingredient 2'
- ...
]

# Instance:
Day: Monday
Dish: Pasta
Elements:
[
- 'Spagetti'
- 'Tomatos'
- 'Basilicum'
- ...
]

Train 3

Contemplate the next immediate:

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. Clarify the distinction right here between the Consumer position and the System position.

b. Mirror on the immediate utilizing the 5 prompting ideas. Clarify how the following tips are used on this immediate to make clear the immediate for ChatGPT.

c. Aks ChatGPT (through the API or through the online interface) to enhance the immediate above. What outcome does ChatGPT give, and may you clarify why this may be an enchancment over the earlier immediate?

d. Within the cell under you discover a full code instance. Regulate this immediate such that the meal plan displays your personal preferences. Use the prompting tricks to tailor ChatGPT’s output to your wants. With each adjustment, replicate: how does this enhance/worsen the output, and why?

Bonus: Additionally examine different prompting tips from [6]. How may sure tips assist to enhance the immediate?

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 = consumer.chat.completions.create(
mannequin="gpt-3.5-turbo",
messages=messages
)

pprint(response.selections[0].message.content material)