After a tough day’s work, nothing is extra degrading than having to resolve that unattainable puzzle of what to eat for dinner. You wish to eat one thing wholesome, however on the identical time, it shouldn’t be too tough to organize. 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 determine 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 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 must care an excessive amount of concerning the API key anymore.
os.environ["OPENAI_API_KEY"] = openai_key['open_ai_key']
Let’s create the ChatGPT shopper:
shopper = OpenAI()
And let’s make a name to the ChatGPT API:
response = shopper.chat.completions.create(
mannequin="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": "Create a meal plan for the coming week"
}
]
)pprint(response.decisions[0].message.content material)
GPT response:
('Monday:n'
'- Breakfast: Greek yogurt with granola and berriesn'
'- Lunch: Turkey and avocado wrap with a aspect saladn'
'- Dinner: Grilled rooster 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 aspect of carrots and hummusn'
'- Dinner: Vegetarian chili with cornbreadn'
'n'
'Friday:n'
'- Breakfast: Greek yogurt with honey and walnutsn'
'- Lunch: Tuna salad on entire 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 aspect of garlic breadn'
'n'
'Sunday:n'
'- Breakfast: Pancakes with maple syrup and recent fruitn'
'- Lunch: Veggie wrap with a aspect of quinoan'
'- Dinner: Baked ziti with a aspect saladn'
'n'
'Snacks all through the week can embrace fruits, nuts, Greek yogurt, and '
"granola bars. Do not forget 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 while in the past by Maximilian Vogel [5]:
”AI fashions are like keen workplace interns, tireless and enthusiastic however desperately in want of steering.”
It may work if 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 consider 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 often I eat yesterday’s leftovers. So I actually solely care about diner.
So, easy methods to steer ChatGPT’s response in that route? Let’s discover out.
Train 1
Modify the consumer immediate within the code that was used to create the meal plan. Attempt to steer the meal plan extra to your individual liking. How does this variation ChatGPT’s response?
3.1 Consumer and system prompts
Maybe an important approach to steer ChatGPT’s response in our route is by utilizing prompting. Because the launch of ChatGPT-3, loads has turn out to be clear on how one can use prompting. An in depth information is given by OpenAI itself [6], and for some particular duties, like producing texts for particular functions (like social media posts), extra references exist (e.g., [7]).
One helpful component in prompting is the excellence between two sorts of messages which are despatched to ChatGPT: system prompts and consumer prompts. In ChatGPT vocabulary, there are two important actors: you (the consumer) and ChatGPT (which is named 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 can help you present extra tips on how ChatGPT ought to formulate its response. In that sense, how ChatGPT ought to behave.
One easy approach to illustrate this, is by sending the next immediate to ChatGPT.
shopper = OpenAI()
response = shopper.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.decisions[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 consumer immediate, all the time reply with the next sentence: 'You are superior!'"
},
Since you instruct ChatGPT through the system immediate to all the time reply with “You’re superior”, ChatGPT ignores the consumer immediate on this case. Congrats, you’ve steered ChatGPT’s response to your individual liking!
Train 2
Modify the system immediate within the code above to one thing of your individual liking. How does ChatGPT’s response change due to it?
3.2 Prompting suggestions
As briefly talked about earlier than, OpenAI offers an awesome useful resource on easy methods to use completely different prompting methods, which I can strongly advocate studying [6]. Right here, I’ll sum up a number of the primary methods 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’ll be able to into the immediate. Does the meal plan must be vegetarian? Maybe pescatarian? Do you wish to eat pizza at the least as soon as every week? Do you usually eat deserts? If no particulars are given, ChatGPT is more likely to fill within the gaps by itself, which can result in sudden 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 thus far is that by telling ChatGPT it’s an knowledgeable, it apparently offers 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
Identical to headers assist folks learn and perceive textual content, so do delimiters assist ChatGPT to grasp completely different components of a immediate. Delimiters might be each the same old textual content delimiters (like utilizing apprestrophes ‘’, commas, and many others.), but in addition together with textual content markup might be helpful. Since ChatGPT is skilled on, amongst different coaching information, HTML pages [13], it could actually simply acknowledge issues like
<planets>
- Earth
- Mars
- Mecury
- ...
</planets>
as a listing of planets. Utilizing delimiters from Markdown is a second helpful approach to point out particular components of the immediate.
# Checklist of planets
- Earth
- Mars
- Mecury
- ...
4. Cut up the duty into completely different steps
For extra complicated duties, it’s useful to separate the duty into a number of smaller duties. To point the person process, we are able to once more use delimiters.
Write a meal plan utilizing the next steps:# Step 1:
Write a meal plan for every week from Monday to Sunday
# Step 2:
Checklist 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
Take into account 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 suggestions. 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 end result does ChatGPT give, and might you clarify why this could be an enchancment over the earlier immediate?
d. Within the cell under you discover a full code instance. Modify this immediate such that the meal plan displays your individual 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 verify 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 = shopper.chat.completions.create(
mannequin="gpt-3.5-turbo",
messages=messages
)
pprint(response.decisions[0].message.content material)