Course Notes — ChatGPT Prompt Engineering for Developers
Link — https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/
Note: I highly recommend to have first hand experience from Andrew course only. This article is just having few main content/learnings from it for revision purpose.
Load OpenAI Key
Model used
Two types of LLMs
Principles of Prompting
1st Principle (Tactic — 01)
1st Principle (Tactic — 01 Example) — Use of Delimiter
Advantage of using delimiter — Avoid Prompt Injection (Conflicting instruction)
1st Principle (Tactic — 02)
1st Principle (Tactic — 02 Example) — Structured Output
1st Principle (Tactic — 03,04)
1st Principle (Tactic — 04 Example) — Few-shot prompt
2nd Principle (Tactic — 01)
Based on above principle, sample example I tried on https://chat.openai.com/
Mistakes happen when prompt is not upto the mark. Below is sample example —
correct by using structured prompt as above solution is indeed not correct.
Model Limitations
Summarization Examples
- Review
- Generate general feedback summary
- Generate feedback summary for price department
- Generate feedback summary for shipping department
Language Detection, Translation
Proof reading and correction
Difference before and after proof reading
Redline — python library used (https://pypi.org/project/redlines/)
Expanding — Brainstorm with LLM. If you have few words and want to write complete essay using same.
Temperature — Degree of randomness in expression
OrderBot — Automate the collection of user prompts and assistant responses to build a OrderBot. The OrderBot will take orders at a pizza restaurant. Below is the code copied from course.
### Step-01: Import Libraries
import os
import openai
from dotenv import load_dotenv, find_dotenv
import panel as pn # GUI
pn.extension()
### Step-02: User defined functions
def collect_messages(_):
prompt = inp.value_input
inp.value = ''
context.append({'role':'user', 'content':f"{prompt}"})
response = get_completion_from_messages(context)
context.append({'role':'assistant', 'content':f"{response}"})
panels.append(
pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
panels.append(
pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))
return pn.Column(*panels)
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
# print(str(response.choices[0].message))
return response.choices[0].message["content"]
### Step-03: Setup OpenAI
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.getenv('OPENAI_API_KEY')
### Step-04: Setting context + Interactive bot
context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant. \
You first greet the customer, then collects the order, \
and then asks if it's a pickup or delivery. \
You wait to collect the entire order, then summarize it and check for a final \
time if the customer wants to add anything else. \
If it's a delivery, you ask for an address. \
Finally you collect the payment.\
Make sure to clarify all options, extras and sizes to uniquely \
identify the item from the menu.\
You respond in a short, very conversational friendly style. \
The menu includes \
pepperoni pizza 12.95, 10.00, 7.00 \
cheese pizza 10.95, 9.25, 6.50 \
eggplant pizza 11.95, 9.75, 6.75 \
fries 4.50, 3.50 \
greek salad 7.25 \
Toppings: \
extra cheese 2.00, \
mushrooms 1.50 \
sausage 3.00 \
canadian bacon 3.50 \
AI sauce 1.50 \
peppers 1.00 \
Drinks: \
coke 3.00, 2.00, 1.00 \
sprite 3.00, 2.00, 1.00 \
bottled water 5.00 \
"""} ] # accumulate messages
panels = [] # collect display
inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")
interactive_conversation = pn.bind(collect_messages,\
button_conversation)
dashboard = pn.Column(
inp,
pn.Row(button_conversation),
pn.panel(interactive_conversation, loading_indicator=True, height=300),
)
dashboard
Conclusion
LLMs are very powerful tools, please use them responsibly and create positive impact.
Please give a clap to this this article if it has helped you. I also welcome your feedback in the Comments section below.
Thanks !!