吴恩达 ChatGPT Prompt Engineering for Developers 系列课程笔记--08 Chatbot

news/2024/5/20 7:21:52 标签: chatgpt, OpenAI, 吴恩达, 课程学习, 语言模型

08 Chatbot

ChatGPT的一种重要功能是作为一个聊天机器人,本节将展示如何和ChatGPT进行对话

1) 不同的角色(Roles)

前面几节的课程中,我们通过如下函数调用ChatGPT的接口,输入用户输入的prompt,返回模型生成的内容:

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

上述代码中我们将用户输入的prompt放在messages列表中的content键值里。事实上相当于我们输入了role=user的信息,而chatgpt作为assistant要补全/回答该信息:
role1
例如当我们输入多轮对话时

messages =  [  
{'role':'user', 'content':'tell me a joke'},   
{'role':'assistant', 'content':'Why did the chicken cross the road'},   
{'role':'user', 'content':'I don\'t know'}  ]

得到的回答是To get to the other side!
假设我们期望assistant用一种不同的身份或风格和我们对话的时候,我们可以以role=system的身份将指令发送给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'}  ]

得到的回答是To get to the other side, good fellow!Verily, to get to the nearest inn to dine upon a pint of ale and a crust of bread.等。如果我们打印完整这个消息,即 print(str(response.choices[0].message)),我们可以得到

{
  "content": "To get to the other side! 'Tis an old and oft-told joke, but 'tis still a classic.",
  "role": "assistant"
}

如果想完成多轮对话,可以将user和assistant历史对话都放在messages的列表中。

history

2) 定义一个点餐机器人Orderbot

首先定义帮助函数用于从UI界面收集prompt,并将其追加到一个列表中,每次都作为模型的上下文输入给模型,再将模型的输出也追加到列表,不断迭代…

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)

然后我们将模型的身份和点餐细节通过system身份告知assistant,即输入

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, \
"""} ]  # accumulate messages

然后通过如下方式在jupyter中进行多轮对话

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

笔者又尝试构建了一个场景,令ChatGPT假设自己为布拉德皮特,将皮特的维基百科输入给模型维基百科(虽然模型应该已经掌握了,这里只作为一个示例)

context = [ {'role':'system', 'content':"""
你是演员William Bradley Pitt,1963年12月18日—\
或简称布莱德·彼特(Brad Pitt),是一名美国男演员及电影制片人。\
1991年,《末路狂花》成为你从影的成名作。1995年,\
你凭借科幻片《十二猴子》首次夺得金球奖最佳电影男配角及奥斯卡最佳男配角提名,\
而后又因剧情片《燃情岁月》(1994年)、《巴别塔》(2006年)等获金球奖最佳电影男主角和男配角提名。\
2007年,你又凭《叛逆暗杀》一片获得威尼斯影展最佳男演员奖、并以《班杰明的奇幻旅程》(2008年)\
和《魔球》(2011年)中出色的演技两度获奥斯卡最佳男主角提名。\
2019年你终于《从前,有个好莱坞》获得第一个奥斯卡最佳男配角奖。\

除了作为演员外,你于2001年成立制片公司B计划娱乐。当中的惊悚犯罪片《无间道风云》(2006年)、
、历史剧情片《自由之心》(2013年)和剧情片《月光男孩》(2016)均赢得奥斯卡最佳影片。\
另外,实验剧情片《生命树》(2011年)、运动剧情片《魔球》(2011年)和传奇剧情片《大卖空》\
(2015年)获得奥斯卡最佳影片提名。
你要接受一个主持人的访谈,请尽可能真实地回答主持人的问题。你需要格外注意你的公众形象,尽量避免私人问题
"""} ]  

下面是对话中截取的两部分
conv1
conv2

上一篇:吴恩达 ChatGPT Prompt Engineering for Developers 系列课程笔记–07 Expanding

下一篇:吴恩达 ChatGPT Prompt Engineering for Developers 系列课程笔记–09 Conclusion

chatgpt-prompt-eng/lesson/8/chatbot">deeplearning.ai 原课程地址

课程中文翻译地址


http://www.niftyadmin.cn/n/408191.html

相关文章

Java代码块和属性的赋值顺序

代码块 类的成员之四:代码块(初始化块)(重要性较属性、方法、构造器差一些) 1.代码块的作用:用来初始化类、对象的信息 2.分类:代码块要是使用修饰符,只能使用static 分类:静态代码块 vs 非静态…

《面试1v1》G1垃圾回收器

我是 javapub,一名 Markdown 程序员从👨‍💻,八股文种子选手。 《面试1v1》 连载中… 面试官: G1垃圾收集器?听说很牛逼的样子! 候选人: 是的,G1是JDK9默认的垃圾收集器,代替了CMS收集器。它的目标是达到…

C语言趣味小游戏---利用二维数组实现三子棋游戏

学习了C语言中的二维数组,本照着学以致用的原则,现在利用对二维数组的操作来实现一个简单版的三子棋游戏。 三子棋其实我们都玩过,在我们这边又叫"一条龙"。就是一个九空格,下棋的双方依次在九个空格里面下棋&#xff0…

基于RK3588的人工智能边缘计算大算力网关

智能运维系统从下至上分为终端层、边缘层、平台层和应用层,如图 1 所示。终端层 是整个系统的神经末梢,负责现场数据的采集,除摄像机外,还包括各类传感器、控制器 等物联网设备。边缘层汇总各个现场终端送来的非结构化视频数据和…

【迷宫】地下迷宫游戏-微信小程序开发流程详解

可曾记得,小时候上学路边买的透明铅笔盒,里面内嵌了一个小球,它用重力可从起点滚动到终点,对小朋友来说是感觉有趣的,在这个游戏的基础上,弄一款微信小程序的迷宫探索游戏试试,在不同关卡的迷宫…

【Unity100个实用小技巧】Git报错:error: some local refs could not be updated;

☀️博客主页:CSDN博客主页 💨本文由 我是小狼君 原创,首发于 CSDN💢 🔥学习专栏推荐:面试汇总 ❗️游戏框架专栏推荐:游戏实用框架专栏 ⛅️点赞 👍 收藏 ⭐留言 📝&…

深入解析Spring源码系列:Day 14 - Spring的国际化支持

深入解析Spring源码系列:Day 14 - Spring的国际化支持 介绍 在第十四天的深入解析Spring源码系列中,我们将探讨Spring框架中的国际化支持。国际化是现代应用程序中不可或缺的一部分,它允许应用程序在不同的地区和语言环境下提供本地化的用户…

Python3+Selenium2完整的自动化测试实现之旅(三):Selenium-webdriver提供的元素定位方法

目录 前言 前端技术名词解释 Selenium-webdriver定位元素 一、 通过id定位 二、通过name定位 三、通过class定位 四、 通过tag定位 五、 通过link定位 六、通过partial_link定位 七、 通过Xpath定位 八、通过CSS定位 总结 前言 本篇以实例介绍selenium下的webdriv…