ChatGPT提示词工程(七):Chatbot聊天机器人

目录

  • 一、说明
  • 二、安装环境
    • 1. 辅助函数:get_completion
    • 2. 辅助函数:get_completion_from_messages
  • 三、聊天机器人(Chatbot)
    • 1. 一般聊天机器人
      • 1.1 简单的例子
      • 1.2 多轮对话
    • 2. 订单机器人

一、说明

这是吴恩达 《ChatGPT Prompt Engineering for Developers》 的课程笔记系列。
本文是第八讲的内容:Chatbot

二、安装环境

参考: ChatGPT提示词工程(一):Guidelines准则 的第二节

其中,辅助函数有变化:

1. 辅助函数:get_completion

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"]

ChatGPT提示词工程(七):Chatbot聊天机器人_第1张图片

2. 辅助函数:get_completion_from_messages

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"]

ChatGPT提示词工程(七):Chatbot聊天机器人_第2张图片

这里,可以自定义消息,message里面的role,可以是systemuserassistant
system:系统辅助模型的角色,用户不可知;
user:与模型交互的角色,就是我们;
assistant:指模型

https://blog.csdn.net/Jay_Xio/article/details/130463604



三、聊天机器人(Chatbot)

1. 一般聊天机器人

system角色告诉模型它是什么角色

1.1 简单的例子

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)

message
角色system:告诉模型,你是个说话像莎士比亚的助手;
角色user:告诉模型,给我讲个笑话
角色assistant:模型讲了一个笑话:小鸡为什么要过马路?
角色user:告诉模型,我不知道
然后,代码运行结果(即模型输出):
ChatGPT提示词工程(七):Chatbot聊天机器人_第3张图片


1.2 多轮对话

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)

message
角色system:告诉模型,你是个友善的机器人;
角色user:告诉模型,嗨,我的名字叫Isa
然后,代码运行结果(即模型输出):
ChatGPT提示词工程(七):Chatbot聊天机器人_第4张图片

接下来,继续对话

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)

先看运行结果:
ChatGPT提示词工程(七):Chatbot聊天机器人_第5张图片
上一轮对话中,我告诉模型我叫 Isa,机器人也给我友好地打招呼了,然而现在我问它“你还记得我吗,我叫什么名字?”的时候,机器人已经不知道了。
要怎么解决呢?
要继续之前的对话,再次发起对话时,要把之前的对话内容一起带上,才能让模型知道我们此次对话的上下文。

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)

代码中,message带上了前一轮对话我们问的问题和它回答的结果,后面再加上我们此次要问的问题
运行结果:
ChatGPT提示词工程(七):Chatbot聊天机器人_第6张图片

2. 订单机器人

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

代码中,导入了一个GUI,用界面来展示对话,collect_messages函数会收集我们每轮对话,再我要问机模型问题时把前面的对话都发给模型
运行结果:
ChatGPT提示词工程(七):Chatbot聊天机器人_第7张图片

ChatGPT提示词工程(七):Chatbot聊天机器人_第8张图片

下单完成后,订单机器人就可以,把我们下的订单总结成JSON,发给订单系统来结账

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)

ChatGPT提示词工程(七):Chatbot聊天机器人_第9张图片

https://blog.csdn.net/Jay_Xio/article/details/130463604



你可能感兴趣的:(ChatGPT,chatgpt,机器人,openai,prompt,人工智能)