您现在的位置是:首页 >技术杂谈 >ChatGPT提示词工程(七):Chatbot聊天机器人网站首页技术杂谈
ChatGPT提示词工程(七):Chatbot聊天机器人
目录
一、说明
这是吴恩达 《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"]
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"]
这里,可以自定义消息,message
里面的role
,可以是system
、user
、assistant
:
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:告诉模型,我不知道
然后,代码运行结果(即模型输出):
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
然后,代码运行结果(即模型输出):
接下来,继续对话
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)
先看运行结果:
上一轮对话中,我告诉模型我叫 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带上了前一轮对话我们问的问题和它回答的结果,后面再加上我们此次要问的问题
运行结果:
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
函数会收集我们每轮对话,再我要问机模型问题时把前面的对话都发给模型
运行结果:
下单完成后,订单机器人就可以,把我们下的订单总结成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)
https://blog.csdn.net/Jay_Xio/article/details/130463604