The Chat Format
How you can utilize the chat format to have extended conversations with chatbots personalized or specialized for specific tasks or behaviors. ChatGPT uses a series of messages in a “role” with a “content” format.
JSON
[
{'role':'system', 'content':'You are friendly chatbot.'},
{'role':'user', 'content':'Hi, my name is Isa'},
{'role':'assistant', 'content': "Hi Isa! It's nice to meet you. Is there anything I can help you with today?"},
{'role':'user', 'content':'Yes, you can remind me, What is my name?'}
]
- “System” is like whispering into the ear of the LLM. It is used to pass information to the AI before the conversation begins and provide any information the AI might need during the conversation.
- “User” is the actual user. This will be you if you’re using the ChatGPT web interface.
- “Assistant” will be the responses from the LLM directed at the user.
It doesn’t have a “memory” of the previous conversation so you must submit the entire history of the conversation as it’s updated in order to get responses that are based on the entirety of the conversation. We refer to this history as “context”.
A simple chatbot
Automate the collection of user prompts and assistant responses to build a bot to take an order for a pizza restaurant.
Create the functions and variables to display the bot while keeping up with the “context” throughout. The below example is written in Python but should give you an idea of what the functionality and interface build would look like.
Python
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
Gather the information from the conversation, convert it to a usable format, and display
Note: While you would want a chatbot to be conversational with a user, when it comes time to output the data for processing, you will want it to be less creative and more predictable. That means possibly setting the “temperature” to 0
to keep it more consistent.