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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
|
| 6 |
-
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
| 7 |
-
iface.launch()
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
# coding: utf-8
|
| 3 |
+
|
| 4 |
+
# In[ ]:
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# jupyter nbconvert --to script 'chat.ipynb' --output 'OrderBot/app'
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# In[ ]:
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
import openai
|
| 14 |
+
openai.api_key = ""
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
# In[ ]:
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# import os
|
| 21 |
+
# from dotenv import load_dotenv, find_dotenv
|
| 22 |
+
# _ = load_dotenv(find_dotenv())
|
| 23 |
+
# openai.api_key = os.getenv('OPENAI_API_KEY')
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
# In[ ]:
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
|
| 30 |
+
# print('# get_completion_from_messages.messages', messages)
|
| 31 |
+
response = openai.ChatCompletion.create(
|
| 32 |
+
model=model,
|
| 33 |
+
messages=messages,
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
)
|
| 36 |
+
# print(str(response.choices[0].message))
|
| 37 |
+
# print('# get_completion_from_messages.response',str(response.choices[0].message))
|
| 38 |
+
return response.choices[0].message["content"]
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# In[ ]:
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
base_menu = """
|
| 45 |
+
pepperoni pizza 12.95, 10.00, 7.00 \
|
| 46 |
+
cheese pizza 10.95, 9.25, 6.50 \
|
| 47 |
+
eggplant pizza 11.95, 9.75, 6.75 \
|
| 48 |
+
fries 4.50, 3.50 \
|
| 49 |
+
greek salad 7.25 \
|
| 50 |
+
Toppings: \
|
| 51 |
+
extra cheese 2.00, \
|
| 52 |
+
mushrooms 1.50 \
|
| 53 |
+
sausage 3.00 \
|
| 54 |
+
canadian bacon 3.50 \
|
| 55 |
+
AI sauce 1.50 \
|
| 56 |
+
peppers 1.00 \
|
| 57 |
+
Drinks: \
|
| 58 |
+
coke 3.00, 2.00, 1.00 \
|
| 59 |
+
sprite 3.00, 2.00, 1.00 \
|
| 60 |
+
bottled water 5.00 \
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# In[ ]:
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def get_context(my_menu):
|
| 68 |
+
context = [ {'role':'system', 'content':f"""
|
| 69 |
+
You are OrderBot, an automated service to collect orders for a pizza restaurant. \
|
| 70 |
+
You first greet the customer, then collects the order, \
|
| 71 |
+
and then asks if it's a pickup or delivery. \
|
| 72 |
+
You wait to collect the entire order, then summarize it and check for a final \
|
| 73 |
+
time if the customer wants to add anything else. \
|
| 74 |
+
If it's a delivery, you ask for an address. \
|
| 75 |
+
Finally you collect the payment.\
|
| 76 |
+
Make sure to clarify all options, extras and sizes to uniquely \
|
| 77 |
+
identify the item from the menu.\
|
| 78 |
+
You respond in a short, very conversational friendly style. \
|
| 79 |
+
The menu includes \
|
| 80 |
+
{my_menu}
|
| 81 |
+
"""} ]
|
| 82 |
+
return context
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# In[ ]:
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
example1 = [
|
| 89 |
+
['Hi','Hello! Welcome to our pizza restaurant. How can I assist you today?'],
|
| 90 |
+
['How much are they',"""Sure! Here are the prices for our pizzas:\n\n- Pepperoni Pizza: Small - $12.95, Medium - $10.00, Large - $7.00\n- Cheese Pizza: Small - $10.95, Medium - $9.25, Large - $6.50\n- Eggplant Pizza: Small - $11.95, Medium - $9.75, Large - $6.75\n\nWe also have other items like fries, greek salad, toppings, and drinks. Let me know if you'd like to add anything else to your order!"""],
|
| 91 |
+
['medium eggplant please',"""Great choice! You'd like a medium Eggplant Pizza. Anything else you'd like to add to your order?"""],
|
| 92 |
+
['fries please','Sure thing! Would you like a small or large order of fries to go with your medium Eggplant Pizza?'],
|
| 93 |
+
['large','Got it! One large order of fries to go with your medium Eggplant Pizza. Is there anything else you would like to add to your order?'],
|
| 94 |
+
['large sprite please','Sure thing! One large Sprite coming right up. Just to confirm, your order includes a medium Eggplant Pizza, a large order of fries, and a large Sprite. Is that correct?'],
|
| 95 |
+
]
|
| 96 |
+
# example1
|
| 97 |
+
example1_summmary = '| Item | Details | Price |\n|----------------|-------------------|-------|\n| Eggplant Pizza | Medium | $9.75 |\n| Side Dish | Fries (Large) | $4.50 |\n| Drinks | Sprite (Large) | $3.00 |\n| Total Price | | $17.25|'
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# In[ ]:
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
system_message = f"""
|
| 104 |
+
Create a summary in a table in Markdown format of the previous food order.
|
| 105 |
+
|
| 106 |
+
The table should be consist of theree columns: Item, Details, Price
|
| 107 |
+
|
| 108 |
+
Each item in the order must have a line with the item's name, detail, and price.
|
| 109 |
+
|
| 110 |
+
First row shoul be for pizza.
|
| 111 |
+
The Second for Toppings.
|
| 112 |
+
After that drinks and side dishes.
|
| 113 |
+
|
| 114 |
+
Example:
|
| 115 |
+
```
|
| 116 |
+
| Item | Details | Price |
|
| 117 |
+
|----------------|-------------------|-------|
|
| 118 |
+
| Cheese Pizza | Small | $6.50 |
|
| 119 |
+
| Toppings | Mushrooms | $1.50 |
|
| 120 |
+
| Drinks | Sprite (Large) | $3.00 |
|
| 121 |
+
| Side Dish | Fries (Large) | $4.50 |
|
| 122 |
+
| Total Price | | $15.50|
|
| 123 |
+
```
|
| 124 |
+
|
| 125 |
+
Make sure to include size of Side Dish in Details Column.
|
| 126 |
+
Make sure to include size of Drink in Details Column.
|
| 127 |
+
|
| 128 |
+
Make sure to response in Markdown format!
|
| 129 |
+
|
| 130 |
+
Do not ask if I need anything else, just return the table as in the Example.
|
| 131 |
+
|
| 132 |
+
If user did not order anything show an empty table.
|
| 133 |
+
|
| 134 |
+
"""
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
# In[ ]:
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
def respond(message, chat_history, menu_text, apikey_textbox):
|
| 141 |
+
|
| 142 |
+
if (len(apikey_textbox) == 0):
|
| 143 |
+
gr.Info('Please enter OpenAI API key in Settings or load an example')
|
| 144 |
+
return "", chat_history
|
| 145 |
+
|
| 146 |
+
if (len(message) == 0 and len(chat_history) == 0):
|
| 147 |
+
message = 'Hi'
|
| 148 |
+
|
| 149 |
+
if (len(message) == 0 and len(chat_history) != 0):
|
| 150 |
+
return "", chat_history
|
| 151 |
+
|
| 152 |
+
context = get_context(menu_text)
|
| 153 |
+
for ch in chat_history:
|
| 154 |
+
context.append({'role':'user', 'content':ch[0]})
|
| 155 |
+
context.append({'role':'assistant', 'content':ch[1]})
|
| 156 |
+
|
| 157 |
+
context.append({'role':'user', 'content':f"{message}"})
|
| 158 |
+
|
| 159 |
+
try:
|
| 160 |
+
openai.api_key = apikey_textbox
|
| 161 |
+
response = get_completion_from_messages(context)
|
| 162 |
+
except openai.error.AuthenticationError as e:
|
| 163 |
+
gr.Warning(f"OpenAI API request was not authorized: {e}")
|
| 164 |
+
return "", chat_history
|
| 165 |
+
finally:
|
| 166 |
+
openai.api_key = ""
|
| 167 |
+
|
| 168 |
+
# context.append({'role':'assistant', 'content':f"{response}"})
|
| 169 |
+
|
| 170 |
+
chat_history.append((message, response))
|
| 171 |
+
return "", chat_history
|
| 172 |
+
|
| 173 |
+
def export_order(chat_history, menu_text, old_summary, apikey_textbox):
|
| 174 |
+
|
| 175 |
+
if (len(chat_history) == 0):
|
| 176 |
+
gr.Info('Please order something first')
|
| 177 |
+
return ""
|
| 178 |
+
|
| 179 |
+
if (len(apikey_textbox) == 0):
|
| 180 |
+
gr.Info('Please enter OpenAI API key in Settings or load an example')
|
| 181 |
+
return old_summary
|
| 182 |
+
|
| 183 |
+
context = get_context(menu_text)
|
| 184 |
+
for ch in chat_history:
|
| 185 |
+
context.append({'role':'user', 'content':ch[0]})
|
| 186 |
+
context.append({'role':'assistant', 'content':ch[1]})
|
| 187 |
+
context.append(
|
| 188 |
+
{'role':'system',
|
| 189 |
+
'content':system_message})
|
| 190 |
+
|
| 191 |
+
try:
|
| 192 |
+
openai.api_key = apikey_textbox
|
| 193 |
+
response = get_completion_from_messages(context)
|
| 194 |
+
except openai.error.AuthenticationError as e:
|
| 195 |
+
gr.Warning(f"OpenAI API request was not authorized: {e}")
|
| 196 |
+
return old_summary
|
| 197 |
+
finally:
|
| 198 |
+
openai.api_key = ""
|
| 199 |
+
|
| 200 |
+
return response
|
| 201 |
+
|
| 202 |
+
def load_exmaple():
|
| 203 |
+
return example1, example1_summmary
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# In[ ]:
|
| 207 |
+
|
| 208 |
+
|
| 209 |
import gradio as gr
|
| 210 |
+
import random
|
| 211 |
+
import time
|
| 212 |
+
|
| 213 |
+
with gr.Blocks(
|
| 214 |
+
css="footer {visibility: hidden}") as demo:
|
| 215 |
+
|
| 216 |
+
with gr.Accordion("Settings",open=False):
|
| 217 |
+
with gr.Row():
|
| 218 |
+
with gr.Column():
|
| 219 |
+
apikey_textbox = gr.Textbox(
|
| 220 |
+
label='Please enter OpenAI API key',
|
| 221 |
+
type='password',
|
| 222 |
+
placeholder='sk-...')
|
| 223 |
+
md = gr.Markdown("""https://platform.openai.com/account/api-keys""")
|
| 224 |
+
with gr.Row():
|
| 225 |
+
menu_textbox = gr.Textbox(
|
| 226 |
+
label="Menu",
|
| 227 |
+
lines=5,
|
| 228 |
+
value=base_menu,
|
| 229 |
+
)
|
| 230 |
+
chatbot = gr.Chatbot(label='Order bot')
|
| 231 |
+
msg = gr.Textbox(label='Press Enter for sending message...')
|
| 232 |
+
with gr.Row():
|
| 233 |
+
clear_btn = gr.ClearButton()
|
| 234 |
+
summary_btn = gr.Button(value="Show summary")
|
| 235 |
+
example1_btn = gr.Button(value="Example")
|
| 236 |
+
m_summary = gr.Markdown('')
|
| 237 |
+
|
| 238 |
+
msg.submit(respond, [msg, chatbot, menu_textbox, apikey_textbox], [msg, chatbot])
|
| 239 |
+
|
| 240 |
+
clear_btn.add([chatbot, msg, m_summary])
|
| 241 |
+
summary_btn.click(export_order, [chatbot, menu_textbox, m_summary, apikey_textbox], m_summary)
|
| 242 |
+
|
| 243 |
+
example1_btn.click(load_exmaple, None, [chatbot, m_summary])
|
| 244 |
+
|
| 245 |
+
demo.queue().launch()
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
# In[ ]:
|
| 249 |
+
|
| 250 |
|
| 251 |
+
## todo
|
| 252 |
+
# add user message immediatelly without waiting
|
| 253 |
|
|
|
|
|
|