一、說明
這是吳恩達 《ChatGPT Prompt Engineering for Developers》 的課程筆記系列。
本文是第八講的內(nèi)容:Chatbot
二、安裝環(huán)境
參考: ChatGPT提示詞工程(一):Guidelines準則 的第二節(jié)
其中,輔助函數(shù)有變化:
1. 輔助函數(shù):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. 輔助函數(shù):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
:系統(tǒng)輔助模型的角色,用戶不可知;user
:與模型交互的角色,就是我們;assistant
:指模型
https://blog.csdn.net/Jay_Xio/article/details/130463604
文章來源地址http://www.zghlxwxcb.cn/news/detail-464594.html
三、聊天機器人(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:告訴模型,我不知道
然后,代碼運行結(jié)果(即模型輸出):
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
然后,代碼運行結(jié)果(即模型輸出):
接下來,繼續(xù)對話
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)
先看運行結(jié)果:
上一輪對話中,我告訴模型我叫 Isa,機器人也給我友好地打招呼了,然而現(xiàn)在我問它“你還記得我嗎,我叫什么名字?”的時候,機器人已經(jīng)不知道了。
要怎么解決呢?
要繼續(xù)之前的對話,再次發(fā)起對話時,要把之前的對話內(nèi)容一起帶上,才能讓模型知道我們此次對話的上下文。
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帶上了前一輪對話我們問的問題和它回答的結(jié)果,后面再加上我們此次要問的問題
運行結(jié)果:
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
函數(shù)會收集我們每輪對話,再我要問機模型問題時把前面的對話都發(fā)給模型
運行結(jié)果:
下單完成后,訂單機器人就可以,把我們下的訂單總結(jié)成JSON,發(fā)給訂單系統(tǒng)來結(jié)賬
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)
文章來源:http://www.zghlxwxcb.cn/news/detail-464594.html
https://blog.csdn.net/Jay_Xio/article/details/130463604
到了這里,關(guān)于ChatGPT提示詞工程(七):Chatbot聊天機器人的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!