The Art of Prompt Engineering: Restaurant Chatbot for order

NIRAV SHAH
7 min readSep 11, 2023

Introduction

Prompt engineering is a crucial aspect of fine-tuning and optimizing the interactions between humans and artificial intelligence (AI) systems. It involves crafting well-designed instructions or queries to elicit desired responses from AI models. While AI models have become increasingly powerful, they often require carefully constructed prompts to provide accurate, relevant, and context-aware outputs. In this article, we explore the significance of prompt engineering, its methods, challenges, and the impact it has on various AI applications.

The Significance of Prompt Engineering

  1. Improved Accuracy: Prompt engineering can significantly improve the accuracy of AI models. By providing clear and context-rich prompts, users can guide AI systems to generate more precise and relevant responses. This is particularly important in applications such as language translation, content generation, and question-answering.
  2. Customization: Prompt engineering allows users to customize AI interactions to meet specific needs. Depending on the context, users can tweak prompts to obtain different types of responses, making AI models more adaptable and versatile.
  3. Reduced Bias: Well-engineered prompts can help mitigate biases in AI systems. By carefully crafting prompts that avoid bias-inducing language or assumptions, developers can ensure that AI outputs are fair and impartial.
  4. Efficiency: Effective prompt engineering can save time and computational resources. Instead of relying solely on model fine-tuning, developers can achieve desired results with less data by using well-crafted prompts.

Methods of Prompt Engineering

  1. Clear and Specific Language: Use precise and unambiguous language in prompts. Vague or ambiguous instructions can lead to unexpected or incorrect outputs.
  2. Contextual Information: Provide relevant context within the prompt to guide the AI system’s understanding. For example, when asking a question, include background information to help the AI model generate a more informed response.
  3. Multiple Iterations: Experiment with different prompts and iterations to find the most effective one. This iterative process involves testing and refining prompts to achieve the desired outcome.
  4. Feedback Loops: Continuously gather user feedback to improve prompts over time. User feedback can reveal common issues and areas for prompt refinement.

Challenges in Prompt Engineering

  1. Bias and Fairness: Ensuring that prompts do not inadvertently introduce bias into AI outputs is a significant challenge. Developers must be vigilant in crafting unbiased prompts to avoid reinforcing stereotypes or promoting discrimination.
  2. Complex Tasks: For complex tasks, designing effective prompts can be challenging. Users may need to invest significant effort in prompt engineering to achieve the desired results, which can be time-consuming.
  3. Generalization: Prompts that work well for one AI model may not be suitable for others. Generalizing prompt engineering techniques across different models and domains can be difficult.

Impact on AI Applications

Prompt engineering has a profound impact on various AI applications:

  1. Chatbots and Virtual Assistants: Well-crafted prompts enable chatbots and virtual assistants to provide more helpful and context-aware responses, improving user satisfaction.
  2. Content Generation: In content generation tasks, such as writing articles or generating code, effective prompts are essential for producing high-quality outputs.
  3. Translation Services: For language translation, prompts with clear source text and context can result in more accurate translations.
  4. Data Analysis: Prompt engineering is critical in data analysis tasks, helping users extract relevant insights from large datasets using AI models.

Real-life Example — Restaurant Chatbot for order

Problem: A busy restaurant owner wants a nice chat application for their customer. This way he can make orders faster & customer does not have to wait in a queue.

Solution Outline: Our approach involves presenting a comprehensive restaurant menu with clear pricing details in a friendly and conversational tone. We aim to create a user-friendly experience by using polite prompts that guide users through the ordering process, ensuring they specify their preferred pizza size and inquire about any additional items or requirements. Furthermore, our chatbot will provide a succinct summary of the user’s order, including selected items, sizes, and costs, facilitating a confident review before concluding the conversation. Through these strategies, we seek to enhance user engagement, streamline the ordering process, and ultimately boost overall customer satisfaction with our service.

Prompt Input: We would provide a restaurant menu with a price list. We also ask prompt style as a friendly style. The prompt would ensure the size of the pizza If the user needs anything else while closing the conversation. Also instructed to provide a summary.

Code

The following code, designed for Jupyter Notebook, requires an OpenAI API key for obtaining results. If you don’t have one yet, you can generate it by signing up at https://platform.openai.com/.

# pip install openai
# pip install python-dotenv
# pip install panel

import os
import openai
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file

openai.api_key = os.getenv('OPENAI_API_KEY')
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"]
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)

import panel as pn # GUI
pn.extension()

panels = [] # collect display

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


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

messages = context.copy()
messages.append(
{'role':'system', 'content':'create a json summary of the previous food order. Itemize the price for each item\
The fields should be 1) pizza, include size 2) list of toppings 3) list of drinks, include size 4) list of sides include size 5)total price '},
)
#The fields should be 1) pizza, price 2) list of toppings 3) list of drinks, include size include price 4) list of sides include size include price, 5)total price '},

response = get_completion_from_messages(messages, temperature=0)
print(response)

use requirements.txt

You can install all dependencies with the pip install command:

pip install -r requirements.txt

# requirements.txt

aiohttp==3.8.5
aiosignal==1.3.1
async-timeout==4.0.3
attrs==23.1.0
bleach==6.0.0
bokeh==3.2.2
boto3==1.28.21
botocore==1.31.21
certifi==2023.7.22
charset-normalizer==3.2.0
contourpy==1.1.0
frozenlist==1.4.0
idna==3.4
Jinja2==3.1.2
jmespath==1.0.1
linkify-it-py==2.0.2
Markdown==3.4.4
markdown-it-py==3.0.0
MarkupSafe==2.1.3
mdit-py-plugins==0.4.0
mdurl==0.1.2
multidict==6.0.4
numpy==1.25.2
openai==0.28.0
packaging==23.1
pandas==2.1.0
panel==1.2.2
param==1.13.0
Pillow==10.0.0
python-dateutil==2.8.2
python-dotenv==1.0.0
pytz==2023.3.post1
pyviz_comms==3.0.0
PyYAML==6.0.1
requests==2.31.0
s3transfer==0.6.1
six==1.16.0
tornado==6.3.3
tqdm==4.66.1
typing_extensions==4.7.1
tzdata==2023.3
uc-micro-py==1.0.2
urllib3==1.26.16
webencodings==0.5.1
xyzservices==2023.7.0
yarl==1.9.2

Result


Assistant: Hello! Welcome to our pizza restaurant. How can I assist you today?
User: Hi

Assistant: Hi there! How can I help you today? Are you looking to place an order?
User: yes

Assistant: Great! What would you like to order? We have a variety of options including pizzas, fries, greek salad, and drinks.
User: I need 3 pizza

Assistant: Sure! Which types of pizzas would you like? We have pepperoni, cheese, and eggplant pizza.
User: 1 pepperoni and 2 cheese

Assistant: Got it! One pepperoni pizza and two cheese pizzas. What sizes would you like for each pizza? We have small, medium, and large.
User: all small

Assistant: Alright, three small pizzas coming right up! Is there anything else you would like to add to your order, such as fries, greek salad, or drinks?
User: No thanks

Assistant: Alright, just to confirm, you would like one small pepperoni pizza and two small cheese pizzas. Is that correct?
User: yes

Assistant: Great! Is this order for pickup or delivery?
User: pickup

Assistant: Perfect! Your order for one small pepperoni pizza and two small cheese pizzas is set for pickup. Is there anything else I can assist you with?
User: That’s it.

Assistant: Alright, just to confirm, your order for one small pepperoni pizza and two small cheese pizzas is set for pickup. If you have any additional requests or changes, please let me know. Otherwise, your total comes to $27.95. How would you like to pay?
User: hey you seem to be counted wrong. I should pay 34.85

Assistant: Apologies for the confusion. Let me recalculate the total for you. The correct total for one small pepperoni pizza ($12.95) and two small cheese pizzas ($10.95 each) is $34.85. Thank you for catching that! How would you like to pay?
============================
{
"pizza": [
{
"type": "pepperoni",
"size": "small",
"price": 12.95
},
{
"type": "cheese",
"size": "small",
"price": 10.95
},
{
"type": "cheese",
"size": "small",
"price": 10.95
}
],
"toppings": [],
"drinks": [],
"sides": [],
"total_price": 34.85
}

Conclusion

Prompt engineering is an art that can significantly enhance the interactions between humans and AI systems. By carefully crafting prompts with clear language, contextual information, and consideration for bias, developers can harness the full potential of AI models across various applications. As AI technology continues to advance, prompt engineering will remain a crucial skill for optimizing AI-driven solutions and ensuring they meet user expectations while upholding ethical standards.

Reference

https://learn.deeplearning.ai/

--

--