Openai+Deeplearning.AI: ChatGPT Prompt Engineering(六)

I would like to share with you the recently learned Deeplearning.AI and openai jointly created the ChatGPT Prompt Engineering online course. The following are the first five blogs I wrote about the course:

ChatGPT Prompt Engineering(一)
ChatGPT Prompt Engineering(二)
ChatGPT Prompt Engineering(三)
ChatGPT Prompt Engineering(四)
ChatGPT Prompt Engineering(五)

Today we come to the fifth part: personalized chatbots.

A personalized chat robot refers to a chat mode dedicated to specific tasks to communicate with users to answer questions, solve puzzles, or complete a specific task. But first we need to set up the main code to access ChatGPT through the API:

import openai
 
openai.api_key ='YOUR_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, 
    )
    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, 
    )
    return response.choices[0].message["content"]

Above we defined two functions get_completion and get_completion_from_messages to access ChatGPT. These two functions both use the "gpt-3.5-turbo" model of ChatGPT, and get_completion_from_messages has one more entry parameter temperature than get_completion, which means that get_completion_from_messages allows external calls Modify the temperature parameter of the model when changing the function. I have already introduced the function of the temperature parameter in the previous blog , so I won’t go into details here.

Compared with the previous "text-davinci-003", the "gpt-3.5-turbo" model is different in the data structure of the dialogue with the user. The "gpt-3.5-turbo" model adds roles to both sides of the dialogue. , here the roles will be divided into 3 types: system, user, assistant. Both system and assistant are the roles of ChatGPT itself, user is the role of the user, and system is the initial role of ChatGPT, which is used to tell what role ChatGPT will play in the next conversation with the user. Generally, we will define it like this System role: "You are a XXXX," (for example, you are a lawyer, you are a teacher, you are a programmer...), assistant and user roles are used to save the relationship between the chat robot and the user Conversation content. In general, in order for ChatGPT to remember the context of the dialogue, we will save part or all of the dialogue content in the dialogue structure (messages), so that ChatGPT has the ability to remember the context. Let's take a look at the dialogue structure of the simulated ChatGPT:

messages =  [  
{'role':'system', 'content':'You are an assistant that speaks like Shakespeare.'},    
{'role':'user', 'content':'tell me a joke'},   
{'role':'assistant', 'content':'Why did the chicken cross the road'},   
{'role':'user', 'content':'I don\'t know'}  ]

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

messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},    
{'role':'user', 'content':'Hi, my name is Isa'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

 

messages =  [  
{'role':'system', 'content':'You are friendly chatbot.'},    
{'role':'user', 'content':'Yes,  can you remind me, What is my name?'}]
response = get_completion_from_messages(messages, temperature=1)
print(response)

 

messages =  [  
{'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?'}  ]
response = get_completion_from_messages(messages, temperature=1)
print(response)

Here we call the get_completion_from_messages function, and pass the dialog structure messages and temperature parameters, and we set the temperature to 1, which tells ChatGPT that it can generate random results. Therefore, if we execute the get_completion_from_messages function multiple times, ChatGPT may generate replies with different results each time. At the same time, we noticed that ChatGPT could remember the user's name as Isa in the last conversation. This is because the Duolun conversation between the user and ChatGPT was stored in the last messages, which also contained the user's name, so ChatGPT finally Can remember the user's name is Isa, but the previous conversation ChatGPT could not remember the user's name, because the user's name was not recorded in the previous messages.

 order robot

Next, we will let ChatGPT act as an order robot through which users order pizza, and the task of the robot is to collect the user's order information for a pizzeria and answer the user's questions about ordering pizza. Let's take a look at the messages structure:

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

 The context variable here is the structure of the conversation between ChatGPT and the customer. The content of the "system" role in the context variable is translated into Chinese so that everyone can better understand the definition of the system role:

context1 = [ {'role':'system', 'content':"""
你是一个订单机器人,这是一项为比萨餐厅收集订单的自动化服务。 \
你先问候客户,然后收集订单,\
然后问是自取还是送货。 \
您等待收集整个订单,然后对其进行汇总并最后检查客户是否还想添加任何其他内容。 \
如果是送货,你索要一个地址。 \
最后你收款了。\
确保阐明所有选项、附加功能和尺寸,以便从菜单中唯一标识该项目。\
你以简短、非常友好的对话方式回应。 \
菜单包括\
意大利辣香肠披萨 12.95, 10.00, 7.00 \
芝士披萨 10.95, 9.25, 6.50 \
茄子披萨 11.95, 9.75, 6.75 \
薯条 4.50, 3.50 \
希腊沙拉 7.25 \
配料:\
额外的奶酪 2.00, \
蘑菇 1.50 \
香肠 3.00 \
加拿大培根 3.50 \
艾酱1.50\
辣椒 1.00 \
饮料:\
可乐 3.00, 2.00, 1.00 \
雪碧 3.00, 2.00, 1.00 \
瓶装水 5.00 \
"""} ]  # accumulate messages

When defining the system role, we first told ChatGPT that it is an order robot whose main task is to collect orders and wait. After explaining the process and steps of ordering pizza to ChatGPT, the next step is the content of the menu, so that ChatGPT can order according to the menu. Customers recommend different kinds of pizza. Here is the chat with the order bot after we executed the program:

 

 

 Here we had a conversation with ChatGPT in Chinese throughout the whole process. Although we used English when defining the system roles of ChatGPT, ChatGPT can still translate the English menu content into Chinese and then reply to us. The whole conversation process looks very professional and smooth.

Below we output the order information in JSON format:

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 '},    
)
 

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

 

 

Inadequacies

The ChatGPT we define here is an ordering robot. In principle, it should not reply to any questions that have nothing to do with ordering. I tried some topics that have nothing to do with ordering. The ordering robot can also reply normally, which shows that our system role definition still has For room for improvement, it should be clearly informed that ChatGPT should not respond to any questions unrelated to ordering when the system role is defined.

 

 try to improve

In the previous system role definition, we did not tell ChatGPT which questions should not be answered, so the robot will answer any questions that have nothing to do with ordering, which violates our original intention. To this end, we can add some restrictive words when defining the system role. Through these restrictive words, we can try to make the robot politely reject any questions that have nothing to do with ordering. Next, we will insert such sentences when defining the role:

 This sentence means: "If the customer's question is not related to ordering pizza, then you are saying: Sorry, I am only an ordering robot, and I can't answer any questions that are not related to ordering pizza."

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. \
If the customer's question has nothing to do with ordering pizza or \
ordering food, you say:Sorry, I'm an order bot and I can't answer questions \
not related to ordering pizza.\
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

 Generally speaking, our improvement has achieved certain results. Basically, the robot can refuse to answer questions that have nothing to do with ordering, but I always feel that it is not perfect. If we want the robot to strictly refuse to answer all questions that have nothing to do with order, we seem to It is also necessary to teach the robot to accurately identify whether there is a relationship between the customer's problem and the ordering event, and the strength of this relationship. What do you think about this issue?

About the model parameter Temperature

If the model has multiple optional results, then when the temperature is 0, the result with the highest probability will be selected. If the value of temperature is increased, the randomness of the model selection result will be increased, that is to say, the possibility The results that are not high may also be selected, let's look at an example:

If my actual liking for the three foods is: pizza 53%, sushi 30%, and tocos 5%, that is to say, my favorite food is pizza, the least favorite is tacos, and the middle one is sushi. When we select a different Temperature, we will get the following results:

 

 When the temperature is 0, the model will select pizza 100%, and when the temperature is 0.3, the model will select pizza with a probability of 2/3, and sushi with a probability of 1/3. When the temperature is 0.7, three foods will be selected The probability of each of you is 1/3. So the larger the temperature, the greater the randomness. Can you understand the role of temperature in this way?

Summarize

Today we learned how to use ChatGPT to develop a personalized chat robot so that it can replace humans to complete certain tasks. Among them, we need to define the system role of the machine in detail to prevent the robot from answering questions that have nothing to do with the task itself.

We also introduced the function and operation of the model parameter temperature. For example, the larger the temperature, the more random the output of the model will be.

References

DLAI - Learning Platform Beta

Guess you like

Origin blog.csdn.net/weixin_42608414/article/details/130950530