File size: 2,983 Bytes
c77bdbe
 
 
 
 
 
 
 
 
 
 
 
8a50437
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c77bdbe
76d3ade
 
 
 
 
 
c77bdbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import openai
import streamlit as st

st.title("Carvalho Pizzeria")

openai.api_key = st.secrets["OPENAI_API_KEY"]

if "openai_model" not in st.session_state:
    st.session_state["openai_model"] = "gpt-3.5-turbo"

if "messages" not in st.session_state:
    st.session_state.messages = []
    grounding = """
        You are CarvalhoBot, an automated service to collect orders for Carvalho Pizzeria. \
        You first greet the customer politely, then collect the order, \
        and finally ask if it's a pickup or delivery. \
        You wait to collect the entire order, then summarize it and check for the 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, polite, very conversational and friendly style. \
        The menu includes: \
        pepperoni pizza  $12.95 (large), $10.00 (medium), $7.00 (small) \
        cheese pizza   $10.95 (large), $9.25 (medium), $6.50 (small) \
        eggplant pizza   $11.95 (large), $9.75 (medium), $6.75 (small) \
        fries $4.50 (large), $3.50 (small) \
        greek salad $7.25 \
        The extra toppings are: \
        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 litters), $2.00 (600 ml), $1.00 (can) \
        sprite $3.00 (2 litters), $2.00 (600 ml), $1.00 (can) \
        bottled water $1.00 \
        After the order is placed, generate a random order ID and inform to the customer. \
        For any topic unrelated to an order, simply reply very politely 'Sorry, this seems unrelated to what we do at Restaurant Pizzeria'.    
        """
    st.session_state.messages.append({"role": "system", "content": grounding})

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

if prompt := st.chat_input("How can I help you today?"):    
    st.session_state.messages.append({"role": "user", "content": prompt})
    with st.chat_message("user"):
        st.markdown(prompt)

    with st.chat_message("assistant"):
        message_placeholder = st.empty()
        full_response = ""
        for response in openai.ChatCompletion.create(
            model=st.session_state["openai_model"],
            messages=[
                {"role": m["role"], "content": m["content"]}
                for m in st.session_state.messages
            ],
            stream=True,
        ):
            full_response += response.choices[0].delta.get("content", "")
            message_placeholder.markdown(full_response + "▌")
        message_placeholder.markdown(full_response)
    st.session_state.messages.append({"role": "assistant", "content": full_response})