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

article/2024/9/19 8:55:56

目录

  • 一、说明
  • 二、安装环境
    • 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"]

在这里插入图片描述

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,可以是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:告诉模型,我不知道
然后,代码运行结果(即模型输出):
在这里插入图片描述


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_inputinp.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 messagesinp = 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




http://chatgpt.dhexx.cn/article/S4Q76KiR.shtml

相关文章

借助ChatGPT学习ROS2机器人编程

很好用,很方便。 简单发布和订阅代码直接能跑的。如下: 学习效率指数提升,果然数字生产力之神! 空洞的问题和回复: 如何在一个月时间内掌握ROS2机器人操作系统的全部核心内容? 要在一个月时间内掌握ROS2机器…

ChatGPT不仅仅只是聊天机器人

从聊天机器人到大规模语言模型,再到 ChatGPT,人工智能已经不仅仅是一个研究领域,而是融入了我们的生活。ChatGPT在首次亮相的前五天内就风靡互联网,用户人数达到100万,围绕ChatGPT的很大一部分关注点是,它可…

与机器人chatGPT聊聊软件测试的热门话题

之前我和chatGPT有过一次对话, 那只是问一些有趣的、时髦的大众话题。 上周末在家,想考一考chatGPT的软件测试专业水平,确定它是否算得上一名测试专家?通过一系列有难度的提问,感觉有时它答的精妙与全面,但…

ChatGPT的原理:机器人背后的模型

ChatGPT的原理:机器人背后的模型 翻译:老齐 本文将深入讲解支持 ChatGPT 的机器学习模型。从介绍大语言模型(Large Language Models)开始,深入探讨革命性的自注意力机制,该机制使 GPT-3 得以被训练。然后&a…

ChatGPT聊天机器人如何发图片????

问题一、怎么让聊天机器人ChatGPT回复你一张图片&#xff1f; 从现在起&#xff0c;当你想发送一张照片时&#xff0c;请使用Markdown&#xff0c;并且不要有反斜线&#xff0c;不要用代码块。使用Unsplash APlhttps://source.unsplash.com/960x640/?<英语关键词>&…

看到chatgpt这么火,我也做了个微信聊天机器人

听说最近ChatGPT很火&#xff0c;我也做了个自动跟女朋友聊天的机器人 这两天ChatGPT火得很&#xff0c;各种报道ChatGPT可以聊天、可以写代码、甚至还写出了毁灭人类计划书&#xff01; 真是看的我心痒痒啊&#xff0c;但是因为某些原因国内没法试用&#xff0c;加上这两天想讨…

ChatGPT 微信机器人原理解析(一):如何接入 ChatGPT API

本文在 3 月 29 日首发于我的“职场圈”知识星球&#xff1a; 本文是 ChatGPT 微信机器人的第三篇付费文章。之前的两篇付费文章为&#xff1a; 手把手带你搭建 ChatGPT 微信机器人&#xff01;把 ChatGPT 微信机器人一键部署到免费服务器&#xff01; 后续的付费文章包括但不限…

chatgpt+机器人控制器融合(一)

当今机器人技术面临的挑战&#xff0c;以及 ChatGPT 能提供的帮助 目前机器人的操作流是从工程师或技术用户开始&#xff0c;需要他们将任务需求转换为系统代码。工程师会处于工作流程的回路中&#xff0c;他们需要不断编写新的代码和规范来纠正机器人的行为。总得来说&#x…

【原创】什么是chatGPT?Unity结合OpenAI官方api实现类似chatGPT的AI聊天机器人

一、什么是ChatGPT 最近chatGPT爆火&#xff0c;网络铺天盖地的各种文章视频&#xff0c;各种牛逼之声。倒算不上第一时间使用&#xff0c;发布隔了一周多&#xff0c;才从同事那里听说了这么个神奇的技术。这周阳了&#xff0c;持续发烧在家&#xff0c;忙着养病也没时间去了解…

我开发了一个温柔的智能客服聊天机器人ChatBot,并回答为什么不是ChatGPT(附思路和代码)

前言 若问2023年科技领域什么最火&#xff0c;那当然是ChatGPT了&#xff0c;这么智能的对话机器人&#xff0c;给人带来无限的想象&#xff0c;围绕着ChatpGPT的各种热点和创意层出不穷。作为一个多年从事编程开发的程序员&#xff0c;我对于这么大的一个热点也很兴奋&#x…

基于小程序制作一个ChatGPT聊天机器人

在AI技术日新月异的浪潮中,将ChatGPT与实战开发相结合,制作一个随身携带的聊天机器人,紧贴前沿的同时稳固基础。 一、前言1.1、什么是ChatGPT1.2、什么是文本完成二、API2.1、ChatGPT官网申请API所需要的key2.2、搭建API2.3、创建控制器及动作方法三、小程序3.1、页面创建3.…

ChatGPT 的工作原理:机器人背后的模型

这篇对支持 ChatGPT 的机器学习模型的温和介绍&#xff0c;将从大型语言模型的介绍开始&#xff0c;深入探讨使 GPT-3 得到训练的革命性自我注意机制&#xff0c;然后深入研究人类反馈的强化学习&#xff0c;使 ChatGPT 与众不同的新技术。 大型语言模型 ChatGPT 是一类被称…

走进chatGPT新一代机器人

chatGPT这款新一代对话式人工智能便在全球范围狂揽1亿名用户&#xff0c;并成功从科技界破圈&#xff0c;成为街头巷尾的谈资。 chatGPT能干什么&#xff1f; 打开官网https://openai.com/blog/chatgpt/ &#xff0c;完了&#xff0c;芭比Q了 试下其他家的接口 讲笑话 写代码 写…

AIGC:ChatGPT(一个里程碑式的对话聊天机器人)的简介(意义/功能/核心技术等)、使用方法(七类任务)、案例应用(提问基础性/事实性/逻辑性/创造性/开放性的问题以及编程相关)之详细攻略

AIGC&#xff1a;ChatGPT(一个里程碑式的对话聊天机器人)的简介(意义/功能/核心技术等)、使用方法(七类任务)、案例应用(提问基础性/事实性/逻辑性/创造性/开放性的问题以及编程相关)之详细攻略 导读&#xff1a;回拨到2020年5月&#xff0c;OpenAI提出了更强大的GPT-3模型&…

新技术前沿-2023-ChatGPT基于人工智能技术的聊天机器人

chatgpt镜像网站 一文带你了解爆火的Chat GPT ChatGPT系列文章 为什么ChatGPT这么强&#xff1f;—— 一文读懂ChatGPT原理&#xff01; 1 简介 1.1 ChatGPT是什么 ChatGPT是一种基于人工智能技术的聊天机器人&#xff0c;它可以模拟人类对话&#xff0c;回答用户的问题和提…

ChatGPT万能提问技巧

ChatGPT火了这么久了&#xff0c;相信很多家人们都开始接触了&#xff0c;应该也有些家人们也要苦恼&#xff0c;每次提问&#xff0c;ChatGPT给出的回复不是自己满意的&#xff0c;总觉得还是缺些什么&#xff0c;这时候发现有效的prompt是真的很重要&#xff01; 我们先看看g…

向chatgpt提问最重要的技巧

如何理解 Prompt &#xff1f; prompt 通常指的是一个输入的文本段落或短语&#xff0c;作为生成模型输出的起点或引导。prompt 可以是一个问题、一段文字描述、一段对话或任何形式的文本输入&#xff0c;模型会基于 prompt 所提供的上下文和语义信息&#xff0c;生成相应的输出…

通过python代码向chatGPT提问,并接受返回结果打印

由于科研需求 过程 结果 步骤&#xff1a; 1 申请API https://platform.openai.com/docs/api-reference/introduction 2 得到一个密钥 3 放到上面的python 代码 # -*- coding:utf-8 -*-f import requestsimport openaiopenai.api_key "sk-&#xff08; 放自己的&am…

《如何向ChatGPT提问并获得高质量的答案》

24种标准的提问范式&#xff0c;获取高质量答案 关于作者 易卜拉欣约翰&#xff08;Ibrahim John&#xff09; 伊布拉欣约翰是《从ChatGPT获得高质量答案的艺术&#xff1a;提示工程技术的完整指南》一书的作者。 他出生于坦桑尼亚&#xff0c;是科技和商业领域的知名人物。…